javascript - Why isn't session id persisting in same domain
Get the solution ↓↓↓I am noticing that my php session id changes when I go from
https://www.mathtutortime.com/tutor/tutor to https://www.mathtutortime.com/account/get_tutoring/lobby.html.
It works just fine if I am going from
https://www.mathtutortime.com/account/ to https://www.mathtutortime.com/account/get_tutoring/lobby.html.
I thought session id's only aren't preserved on different domains.
my code at https://www.mathtutortime.com/tutor/tutor has session information that I stored already in
https://www.mathtutortime.com/tutor. The code in https://www.mathtutortime.com/tutor/tutor is:
<?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>
at lobby.html, I have:
<?php session_start();
echo session_id();
if($_SESSION['loggedin'])
{
echo "yelo";
if(!$_SESSION['tutor'] && $_SESSION['minutes'] > 0)
{
...
?>
Now, if I reload lobby.html after the redirect, the session id seems to reset back to what it was. Meaning it works after reloading the page. I think this because I am able to get past the
if($_SESSION['loggedin'])
section in lobby.html, and I can see the session ID change when I echo it out.
Because of this, I think the session information really does persist, but has a lapse only when I redirect using window.location.replace, etc. But I have no idea why.
Thanks for assistance.
Answer
Solution:
Change this
<?php session_start();
?>
<!DOCTYPE html>
<html>
To become this, so that there is no whitespace (or any other character) that gets rendered before session_start().
<?php session_start();
?><!DOCTYPE html>
<html>
Your !doctype should have no preceding whitespace too, though wouldnt be the problem.
Answer
Solution:
Really strange, now it works. I just reset my history/cookies. I did have session_start() on a new line, so that may have fixed it. But the fact that it was fixed only was revealed after deleting my cookies/history. Anyways, thanks for those that answered!.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: undefined array key php
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.