会话变量不起作用php [英] Session variables not working php

查看:26
本文介绍了会话变量不起作用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().请参阅this 了解如何转关闭.
  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天全站免登陆