[英]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. 我在PHP中有一个cookie,如果存在,则该页面应转到用户页面而不要求登录,但它不起作用。 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. 允许使用cookie,并且我的cookie确实存在,我检查了一下。
Here my login code(it was working before the 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();
?>
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: 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: 和index.php:
<?php
require_once("acessa.php");//calls the login code
require_once("view.php");//calls the html
You have 2 issues. 您有2期。 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.
第一个是您无法访问与您在其中设置的HTTP请求完全相同的Cookie,必须重新加载该页面才能再次访问它。
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:
您要在其上使用会话的每个页面,都必须在其上启动会话,这就像将其放在php脚本的顶部一样简单:
session_start();
One other error, your function buscarUsers()
does the following: 另一个错误是,函数
buscarUsers()
执行以下操作:
return $usr;
mysql_close($conexao);
Now the mysql_close($conexao);
现在,
mysql_close($conexao);
will never run, because you're returning $usr
before that. 将永远不会运行,因为在此之前您将返回
$usr
。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.