Cookie无法正常工作

Cookie无法正常工作

问题描述:

我在PHP中有一个cookie,如果它存在,页面应该转到用户页面而不要求登录,但它不起作用。这是安宁我的警告: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确实存在,我检查它。

这里我登录的代码(这是之前的饼干工作):

<?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 

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

第二个问题是,如果不启动会话,您将无法设置会话。要在使用会话每一个页面,你就必须开始对话,这是因为把这个在你的PHP脚本的顶部那样简单:

session_start(); 

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

return $usr; 
mysql_close($conexao); 

现在mysql_close($conexao);永远不会跑,因为你回来$usr之前。

创建脚本时,帮助调试你的东西,你应该把错误报告:

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

我开始的会议在'include_once“../ usersFunctions.php”;',并在应我把饼干访问它?我关闭并打开浏览器后不应该工作吗? – anuseranother

+0

@anuseranother你看过检查你的cookie是否被设置?通过在PHP中执行'print_r($ _ COOKIE);'或检查您的“检查元素” - >“资源”方式。 – Darren

+0

这是printin'我的cookie的价值,这是usr的名称。 – anuseranother