lara336223's avatar

Sessions Not persisting on page change !

Hello, I have been trying to get this to work , but not got it going :( I have been adding , this ... session_start() $_SESSION['user']['email'] = $email In my Script in .store.php & then doing a header() to / But the $_SESSION does Not persist ! Also in the Browser it is in the cookies , but Not in the sessions Data fields. I have been working on this for 2 evenings now . Is it something to do with my routes.php etc ? Or have I done something wrong ? Any help please :) Thank-You.

0 likes
4 replies
LaryAI's avatar
Level 58

It sounds like you're having trouble with PHP sessions not persisting across page loads. Here are a few things you should check to troubleshoot this issue:

  1. Session Start: Ensure that session_start() is called at the beginning of every PHP script where you want to access or set session variables. It must be called before any output is sent to the browser.

  2. Cookies: Check that cookies are enabled in your browser since PHP sessions typically use a cookie called PHPSESSID to maintain the session state across different pages.

  3. Session Configuration: Verify your php.ini file for the session configuration settings. Look for session.save_path to ensure that the server has write permissions to that directory. Also, check the session.cookie_lifetime and session.gc_maxlifetime to make sure sessions are not expiring too soon.

  4. Output Buffering: If you're sending a header() after output has been sent to the browser, it will not work. Ensure that there is no HTML or whitespace before session_start() or before sending headers.

  5. Path and Domain: If you're setting cookies with specific paths or domains, ensure that the session cookie's path and domain match your site's URL.

Here's a basic example of how to use sessions:

<?php
// Start the session
session_start();

// Set session variables
$_SESSION['user']['email'] = $email;

// Redirect to home page
header('Location: /');
exit();
?>

Make sure that you call exit() after header() to stop the script from running.

If you're still having issues, you might want to add some debugging information to your script to check the session's status:

<?php
session_start();

// Set a session variable
$_SESSION['user']['email'] = $email;

// Debugging: Dump the session to see if it's set correctly
var_dump($_SESSION);

// Redirect to home page
header('Location: /');
exit();
?>

Remember to remove the debugging code once you've resolved the issue.

If none of these suggestions solve your problem, it could be related to your specific application setup, such as the routing in routes.php. Make sure that your routes are not unintentionally destroying the session or that there isn't any middleware that could be interfering with the session.

KuKa's avatar
  • Make sure session_start(); is called in / too.
  • After the header redirect, end the script with exit();
  • Make sure cookies are enabled in the browser.
  • Make sure you redirect to the same domain.
  • 404 called due to a missing favicon.ico in Chrome and Firefox only can cause the problem.

   

Or... you can also use the Laravel session global helper function.

   

To set the session data, use:

session(['key' => 'value']);

To retrieve the session data, use:

$value = session('key');
//output: value
1 like
Snapey's avatar

Is this plain PHP application (not Laravel)

If Laravel then you should not be using the $_SESSION global

lara336223's avatar

@Snapey Yes, I have been going through the PHP for beginners . Wanted to learn all the PHP I could, so tired all types. Got it working now. Built a Class to send the Sessions through Middleware/ Routes / get /post etc. I have had a lot of fun :) Learning as I go. Thank-You All for your help :)

Please or to participate in this conversation.