2015-07-01 96 views
0

我在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 

回答

0

您有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