2

I have two simple pages(test.php, test2.php).

test.php

<?php
session_start();
$_SESSION['username'] = "wria";
echo '<a href = "test2.php">click to another page</a> <br>';

if($_SESSION['username']) {
echo $_SESSION['username'];
}
?>

test2.php

<?php
session_start();
var_dump($_SESSION);
if($_SESSION['username']) {
echo $_SESSION['username'];
}
?>

in the output, when I open test.php, after a refresh enter image description here

but when I go to test2.php, the output appears:

enter image description here

nothing showing.

Note: this file is on real hosting not local.

Wria Mohammed
  • 1,433
  • 18
  • 23

4 Answers4

2

if you add this code: error_reporting(E_ALL); ini_set('display_errors', 1); to the top for test2.php, you will see your error. if you see (No such file or directory) it means your saved session is unavailable, you can change it for another place. or probably you do not have permission for the folder that session will save on it.

Dosti
  • 63
  • 1
  • 8
1

I think the problem comes from your server configuration. Have you tried it in a local environement ? Here's is some steps to help you debug this which comes from PHP session lost after redirect

First, carry out these usual checks:

  1. Make sure session_start(); is called before any sessions are being called. So a safe bet would be to put it at the beginning of your page, immediately after the opening
  2. After the header redirect, end the current script using exit(); (Others have also suggested session_write_close(); and session_regenerate_id(true), you can try those as well, but I'd use exit();)
  3. Make sure cookies are enabled in the browser you are using to test it on.
  4. Ensure register_globals is off, you can check this on the php.ini file and also using phpinfo().
  5. Make sure you didn't delete or empty the session
  6. Make sure the key in your $_SESSION superglobal array is not overwritten anywhere
  7. Make sure you redirect to the same domain. So redirecting from a www.yourdomain.com to yourdomain.com doesn't carry the session forward.
  8. Make sure your file extension is .php (it happens!)

You may also have to configure a session_save_path like it was said in the post i linked.

Thomas
  • 108
  • 8
1

your path to save session is unavailable. You have to change it for an available place. You can change in php.ini

0

Likely you do not persist a session id in any way and the new session is being opened each time.

Please have a look at the https://www.php.net/manual/en/session.idpassing.php

There are a few ways it can be 'solved', using cookies, GET parameter etc.

matiit
  • 7,969
  • 5
  • 41
  • 65