$_SESSION 变量和 id 不会在本地主机上的页面之间转移 [英] $_SESSION variables and id do not carry over between pages on localhost

查看:35
本文介绍了$_SESSION 变量和 id 不会在本地主机上的页面之间转移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个简单的注册页面,当用户的信息得到验证时,就在重定向之前,他们的会话 ID 会根据我设置的令牌系统进行更改.在注册页面上,id 是正确的,我还有一个手动设置的可变 UID.虽然问题是在下一页,id不同,变量未定义.

I made a simple sign up page and when the user's information is validated, right before redirecting, their session id is changed according to a token system I have set up. On the register page, the id is the correct on and I also have a variable UID that I set manually. Although the problem is that on the next page, The id is different, and the variable is undefined.

$_SESSION['UID'] = $id;
session_id($sessID);

session_write_close();
header("Location: /website/landing.php");
exit();

Cookies 已启用,Sessions 有一个目录C:/xampp/tmp"并且它被写入,我从那里的注册页面看到具有正确 id 的文件.这是在本地主机上运行的,它是一个 https 连接.我已经按照我告诉的方式设置了所有内容(通过许多其他 stackoverflow 答案)它应该是,但在下一页:

Cookies are enabled, Sessions have a directory "C:/xampp/tmp" and it is written to, I see files with the correct id from the register page there. This is running on localhost and it's an https connection. I've set up everything the way I'm told (through many other stackoverflow answers) it should be , and yet on the next page:

<?php 
  session_start();

  echo session_id()."\n";
  echo $_SESSION['UID'];

  session_unset();
  session_destroy();
?>

<!DOCTYPE html>
<html>
<head>
  <title>Logged in</title>
</head>
<body>
<h1>Congratulations, you have successfully logged in!</h1>



<a href="logout.php" title="">Log Out</a>

</body>
</html>

在这里,当我回显会话 ID 时,它是一个新的,并且每次都保持不变,除非我从 Firefox 设置中删除它,但即便如此,新的也不是我想要的用户特定的.

Here when I echo the session id, it's a new one and it stays the same everytime unless I delete it from the Firefox settings, but even then, the new one is also not the user specific one I wanted.

这是什么原因?我觉得这与重定向有关.

What is the cause of this? I feel like it has something to do with the redirection.

推荐答案

删除行:

session_unset();
session_destroy();

因为它们会在每次加载页面时破坏会话.从而导致一个新的会话.

As they destroy the session on every page load. Thus causing a new session.

我建议查看 session_destroy() 的文档 用于与注销相关的其他操作.

I suggest reviewing the docs for session_destroy() for additional actions associated with logout.

这篇关于$_SESSION 变量和 id 不会在本地主机上的页面之间转移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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