简体   繁体   中英

Cookie doesn't work properly

I have a cookie in PHP, if it exists the page should go to the users page without asking for login, but it don't work. It's givin' me the warning:

The page isn't redirecting properly

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

    This problem can sometimes be caused by disabling or refusing to accept cookies.

The cookies are allowed and my cookie does exists, i checked it.

Here my login code(it was working before the cookies):

<?php

include_once '../usersDB.php';
include_once '../usersFunctions.php';

$conexao = new usuarios();

$mail = $_POST["con1"];
$pass = $_POST["con2"];

$usuario = $conexao->buscarUsers("select * from users where email = '{$mail}' and senha = '{$pass}'");

    if(isset($_POST['mantemLog']) && $_POST['mantemLog'] == "1"){
        setcookie("mantemUsr",$_POST["con1"],time()+60*60*24*30);
    }

    if($usuario != null || isset($_COOKIE['mantemUsr'])){
        $_SESSION["sucesso"] = "Usuario logado com sucesso";
        loggingUsr($mail);
    header("Location: slides.php");
        }else{
            $_SESSION["deny"] = "Usuario ou senha invalidos!";
            header("Location: view.php");
        }

    die();
?>

The function in the class usuarios:

function buscarUsers($query){
    $conexao = mysql_connect($this->host, $this->usuario, $this->senha);
    mysql_select_db($this->banco, $conexao);
    $result = mysql_query($query, $conexao);
    $usr = mysql_fetch_assoc($result);
    return $usr;
    mysql_close($conexao);
}

the html:

<div id="logFrm">
<h5>Por favor, insira o seu email<br />
    e senha para continuar.</h5>
<form action="acessa.php" method="POST">
    <label for="con1" class="lblLog">Email</label>
        <input type="text" name="con1" id="con1" />
            <br /><br />
    <label for="con2" class="lblLog">Senha</label>
        <input type="password" name="con2" id="con2" />
            <br /><br />
        <input type="submit" name="logBtn" id="logBtn" value="Logar" />
    <label for="chk">
        <input type="checkbox" name="mantemLog" id="mantemLog" value="1" />Manter logado</label>
</form>

And the index.php:

<?php

    require_once("acessa.php");//calls the login code
    require_once("view.php");//calls the html

You have 2 issues. The first one is that you can't access a cookie the exact same HTTP request that you set it in, the page has to be reloaded again before you'll be able to access it again.

The second issue is that you can't set a session without starting the session. Every single page that you want to use a session on, you'll have to start the session on, and that's as simple as putting this at the top of your php scripts:

session_start();

One other error, your function buscarUsers() does the following:

return $usr;
mysql_close($conexao);

Now the mysql_close($conexao); will never run, because you're returning $usr before that.

You should turn on error reporting when creating scripts, to help debug your stuff:

ini_set('display_errors', 1);
error_reporting(-1); // or E_ALL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM