简体   繁体   English

Cookies在PHP中无法正常工作以进行重定向

[英]Cookies not working properly in PHP to redirect

I seem to have an issue with my code. 我的代码似乎有问题。 It doesn't redirect if cookies are not there, and it doesn't include my header.php if the cookie is there. 如果不存在cookie,它就不会重定向;如果存在cookie,它就不会包含我的header.php。 I made a test cookie script and I can confirm if the cookie is/or not present. 我做了一个测试cookie脚本,可以确认cookie是否存在。

CODE: 码:

//checks cookies to make sure they are logged in 
    if(isset($_COOKIE["user"]))
{
$username = $_COOKIE["user"]; 
$pass = $_COOKIE["password"];
$check = mysql_query("SELECT * FROM members WHERE email = '$username'") or die(mysql_error()); 
$info = mysql_fetch_array( $check );

//if the cookie has the wrong password, they are taken to the login page 
if ($pass != $info['password']) 
    {
    header("Location: login.php"); 
    exit();
    } 
else //otherwise they are shown the admin area   
    { 
    include 'header.php';
    }
}

Apart from the bunch of bad practices in your script, what I suppose is causing the malfunction is this: 除了脚本中的许多不良做法外,我认为导致故障的原因还包括:

$info = mysql_fetch_array( $check );
if ($pass != $info['password']) 

If you throw an eye at the manual this is what it says 如果您关注手册,它就是它的意思

Return Values 返回值

Returns an array of strings that corresponds to the fetched row, or FALSE if there are no more rows... 返回与获取的行相对应的字符串数组 ;如果没有更多行,则返回FALSE。

So the code should look like 所以代码应该看起来像

$info = mysql_fetch_array( $check );
if ($pass != $info[0]['password']) 

Or in php gte 5.4 或者在PHP GTE 5.4中

$info = mysql_fetch_array( $check )[0];
if ($pass != $info['password'])

You should make sure you have display_errors set to E_ALL so you can debug simple errors like that yourself easier. 您应该确保已将display_errors设置为E_ALL以便可以轻松调试一些简单的错误。

Mysql functions are deprecated. 不建议使用MySQL函数。 Mysqli or PDO, along with prepared statements, should be used. 应该使用Mysqli或PDO,以及准备好的语句。 Example: 例:

//checks cookies to make sure they are logged in 
if(isset($_COOKIE["user"]) && isset($_COOKIE["password"])) {    
  $password = $_COOKIE["password"];
  /* Create the prepared statement */
  if ($stmt = $mysqli->prepare("SELECT password FROM members WHERE email = ?")) {

    /*bind parameters */
    $stmt->bind_param("s", $_COOKIE['user']);

    /* Execute the prepared Statement */
    $stmt->execute();

    /* Bind results to variables */
    $stmt->bind_result($dbPassword);

    /* fetch values */
    $stmt->fetch();

    if($password != $dbPassword) {
    //password didn't match, boot 'em
      header('Location:fail.php');
      exit();
    }
    //password matched, let 'em in
    else {
      header('Location:pass.php');
      exit();
    }
}
//no cookies set, boot 'em
else {
    header('Location:fail.php');
    exit();
}

Side note: don't store plain text passwords. 注意事项:不要存储纯文本密码。 You should be hashing the password, and adding salt. 您应该对密码进行哈希处理,然后添加盐。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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