标题重定向后,PHP会话索引是否未定义?

前端之家收集整理的这篇文章主要介绍了标题重定向后,PHP会话索引是否未定义?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经挣扎了好几个小时,但我无法让它发挥作用.当我重定向到另一个 PHP页面时,我的所有会话变量都为null.我在xampp服务器上.

session.PHP文件

  1. <?PHP
  2. session_start();
  3. if(isset($_POST['submitted']))
  4. {
  5. $_SESSION['first_name'] = "MAX";
  6. var_dump($_SESSION);
  7. header("Location: http://localhost:8080/secure login/session2.PHP");
  8. die();
  9. }
  10. ?>
  11. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  12. <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
  13. <head> <Meta http-equiv="Content-Type" content="text/html; charset=ISO-8859- 1" />
  14. <title>You Logged In</title>
  15. </head>
  16. <body>
  17. <form action="session.PHP" method="post">
  18. <div align="center"><input type="submit" name="submit" value="Login" /></div>
  19. <input type="hidden" name="submitted" value="TRUE" />
  20. </form>
  21. </body>
  22. </html>

session2.PHP

  1. <?PHP
  2. session_start();
  3. ?>
  4.  
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  6. <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
  7. <head> <Meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
  8. <title>You Logged In</title>
  9. </head>
  10. <body>
  11. <div id="main">
  12. <?PHP
  13. echo '<pre>' . print_r($_SESSION,TRUE) . '</pre>';
  14. echo 'You are welcome to session2.PHP <br></br>';
  15. if (isset($_SESSION['first_name']))
  16. {
  17. echo $_SESSION['first_name'] . "<br></br>";
  18. }
  19. else
  20. {
  21. echo "Your session doesn't exist. I hate PHP <br></br>";
  22. echo $_SESSION['first_name'];
  23. }
  24. ?>
  25. </div>
  26. </body>
  27. </html>

会话不保存,输出为;

  1. Array
  2. (
  3. )
  4. You are welcome to session2.PHP
  5. Your session doesn't exist. I hate PHP
  6. Notice: Undefined index: first_name in C:\xampp\htdocs\secure login\session2.PHP on line 28

我尝试过其他一些事情,例如将会话变量从xampp / tmp保存到另一个目录,但这并没有解决问题.我有一个程序,当我进行重定向时,我需要让用户登录,但这已经阻止了我超过一天.

更新:

目录之间的空间不是问题,它暂时解决了问题,但那是因为新目录还没有缓存.无论如何,再过几天,我调试并意识到我在我的localhost上运行了两个程序.两者都在使用会话,因此如果终止会话,它也会终止另一个会话,因为localhost就像一个域名,并且只存在一个会话.特别是,我的其他程序的logout.PHP并没有破坏会话,而是因为你必须删除浏览器缓存而不是它的混乱.我正在清空会话数组,破坏会话,并销毁cookie,这是问题所以我无法再次登录.我所要做的就是只破坏会话;

见 – > Killing off Global Session Variable as a logout button

好像你有问题,因为你有一个名称安全登录的空间
  1. localhost:8080/secure%20login/session.PHP

因此,请尝试使用下划线secure_login更改名称,并更改您的代码

  1. <?PHP
  2. session_start();
  3. if(isset($_POST['submitted']))
  4. {
  5. $_SESSION['first_name'] = "MAX";
  6. var_dump($_SESSION);
  7. header("Location: http://localhost:8080/secure_login/session2.PHP");
  8. die();
  9. }
  10. ?>

猜你在找的PHP相关文章