javascript - 为什么 session ID 不保留在同一域中

标签 javascript php html

我注意到当我离开时我的 php session ID 发生了变化

https://www.mathtutortime.com/tutor/tutorhttps://www.mathtutortime.com/account/get_tutoring/lobby.html .

如果我从这里出发,效果很好

https://www.mathtutortime.com/account/https://www.mathtutortime.com/account/get_tutoring/lobby.html

我认为 session ID 不会保留在不同的域中。

我的代码为https://www.mathtutortime.com/tutor/tutor有我已经存储在

中的 session 信息

https://www.mathtutortime.com/tutorhttps://www.mathtutortime.com/tutor/tutor中的代码是:

<?php session_start(); ?>
<!DOCTYPE html>

<html>
    <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script>
        window.location.replace("https://www.mathtutortime.com/account/get_tutoring/lobby.html")
    </script>

    </head>


    <body>



    </body>

</html>

在obby.html,我有:

<?php session_start();
echo session_id();
if($_SESSION['loggedin'])
{
    echo "yelo";
    if(!$_SESSION['tutor'] && $_SESSION['minutes'] > 0)
    {

...

?>

现在,如果我在重定向后重新加载obby.html, session ID 似乎会重置回原来的状态。这意味着它在重新加载页面后起作用。我认为这是因为我能够克服

if($_SESSION['loggedin'])

lobby.html 中的部分,当我回显它时,我可以看到 session ID 发生变化。

因此,我认为 session 信息确实会持续存在,但只有当我使用 window.location.replace 等重定向时才会失效。但我不知道为什么。

感谢您的帮助。

最佳答案

改变这个

    <?php session_start();
?>
<!DOCTYPE html>

<html>

要成为这样,这样在 session_start() 之前就不会渲染空格(或任何其他字符)。

<?php session_start();
?><!DOCTYPE html>

<html>

你的 !doctype 前面也不应该有空格,但这不是问题。

关于javascript - 为什么 session ID 不保留在同一域中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60030537/

相关文章:

javascript - 打印页面调整

javascript - 获取数据 MySQL DB 并在 HTML 中显示

javascript - 无法在 Chrome 和 Opera 中从 PHP 接收 json 数据

javascript - 使用 d3.transition() 更改 CSS

html - 无法垂直居中 Bootstrap 3 媒体

javascript - 跨域 ajax 请求成功事件不会触发

javascript - 通过输入字段更新按钮文本

shuffle 和 array_rand 之间的 PHP 区别

javascript - PHP 在 javascript 中用于下拉菜单

php - 使用php在web中使用sh文件删除文件