会话变量无法正常工作的PHP [英] Session variables not working php

查看:81
本文介绍了会话变量无法正常工作的PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的登录页面的代码,其中登录脚本检查用户的真实性,然后使用标题功能重定向到收件箱页面.

Here are the code of my login page where the login script checks for the authenticity of the user and then redirects to inbox page using header function.

<?php
session_start();

include_once('config.php');
$user=htmlentities(stripslashes($_POST['username']));
$password=htmlentities(stripslashes($_POST['password']));
// Some query processing on database    

if(($id_user_fetched<=$id_max_fetched) && ($id_user_fetched!=0)){
$_SESSION['loggedIn'] = 'yes';
    header("Location:http://xyz/inbox.php?u=$id_user_fetched");
    //echo 'Login Successful';
    }else{
        echo 'Invalid Login';
        echo'<br /> <a href="index.html">Click here to try again</a>';
        }
}else{
    echo mysqli_error("Login Credentials Incorrect!");
    }
?>

inbox.php页面如下:

The inbox.php page looks like this:

<?php
session_start(); 
echo 'SESSION ='.$_SESSION['loggedIn'];
if($_SESSION['loggedIn'] != 'yes'){
echo $message = 'you must log in to see this page.';
//header('location:login.php');
}
 //REST OF THE CODE

?>

现在有了上面的代码,inbox.php始终显示输出: SESSION =您必须登录才能看到此页面. 这意味着没有设置会话变量,或者inbox.php无法检索会话变量.我要去哪里错了?

Now with the above code, the inbox.php always shows the output: SESSION=you must log in to see this page. Which means that either the session variable is not being setup or the inbox.php is unable to retrieve the session variable. Where am i going wrong?

推荐答案

  1. 确保在调用任何会话之前调用了session_start();.因此,安全的选择是将其放在页面的开头,紧接着在打开<?php标记之后,然后放在其他任何地方.另外,请确保在打开<?php标记之前没有空格/制表符.
  2. header重定向之后,使用exit();结束当前脚本(其他人也建议使用session_write_close();session_regenerate_id(true),您也可以尝试使用它们,但我会使用exit();).
  3. 确保在用于测试它的浏览器中启用了cookie.
  4. 确保已关闭register_globals,您可以在php.ini文件上也可以使用phpinfo()进行检查.请参阅,以了解如何打开它关闭.
  5. 确保您没有删除或清空会话.
  6. 确保$_SESSION超全局数组中的键不会在任何地方被覆盖.
  7. 确保您重定向到相同的域.因此,从www.yourdomain.com重定向到yourdomain.com不会继续进行会话.
  8. 确保您的文件扩展名是.php(确实如此!).
  1. Make sure session_start(); is called before any sessions are being called. So a safe bet would be to put it at the beginning of your page, immediately after the opening <?php tag before anything else. Also ensure there are no whitespaces/tabs before the opening <?php tag.
  2. After the header redirect, end the current script using exit(); (Others have also suggested session_write_close(); and session_regenerate_id(true), you can try those as well, but I'd use exit();).
  3. Make sure cookies are enabled in the browser you are using to test it on.
  4. Ensure register_globals is off, you can check this on the php.ini file and also using phpinfo(). Refer to this as to how to turn it off.
  5. Make sure you didn't delete or empty the session.
  6. Make sure the key in your $_SESSION superglobal array is not overwritten anywhere.
  7. Make sure you redirect to the same domain. So redirecting from a www.yourdomain.com to yourdomain.com doesn't carry the session forward.
  8. Make sure your file extension is .php (it happens!).

重定向后PHP会话丢失

这篇关于会话变量无法正常工作的PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆