Cookie无法正常运行

时间:2015-07-01 00:36:03

标签: php cookies

我在PHP中有一个cookie,如果它存在,页面应该转到用户页面而不要求登录,但它不起作用。它给了我警告:

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.

允许使用cookie,我的cookie确实存在,我检查了它。

这是我的登录代码(它在cookie之前工作):

<?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();
?>

类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);
}

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>

还有index.php:

<?php

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

1 个答案:

答案 0 :(得分:0)

你有2个问题。第一个是您无法访问与您设置的完全相同的HTTP请求的cookie,必须重新加载该页面才能再次访问它。

第二个问题是您无法在不启动会话的情况下设置会话。你想要使用会话的每一个页面,你必须开始会话,这就像把它放在php脚本的顶部一样简单:

session_start();

另一个错误,您的函数buscarUsers()执行以下操作:

return $usr;
mysql_close($conexao);

现在mysql_close($conexao);永远不会运行,因为您之前会返回$usr

您应该在创建脚本时打开错误报告,以帮助调试您的内容:

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