PHP $ _SESSION is empty after header redirection

I am losing data in $ _SESSION when I redirect the header. When I look through this with the debugger, I can see all my data in $ _SESSION before exiting ();

Login.php:

...

if($result == 1){       
    header("Location: /myaccount.php");
    session_write_close();
    exit();
} else {
    header("Location: /login.php?invalid=yes");
    exit();
} 

Then I set a breakpoint after the session_start () condition below and $ _SESSION is completely empty.

myaccount.php:

<?php
if(!isset($_SESSION['user_id'])) { session_start(); }

$docRoot = getenv("DOCUMENT_ROOT");
...

Where was my session held?

+3
source share
3 answers

Make sure you use the function session_start();before the if statement onmyaccount.php

+7
source

session_start() , ( ) $_SESSION, . session_start() .

+1

Yes, do not delete the message ... I had the EXACT same problem, and this post forced me to involuntarily press my palm to my forehead. And that fixed the problem (with my code, not my dumb one). Hooray!

+1
source

Source: https://habr.com/ru/post/1793647/


All Articles