Jump to content

Recommended Posts

I have a problem with session start. It messes up my site a bit too ******. This is a hybrid CMS/SPA site that fetches pages via slugs. Pages fetched via
slugs fetch their content and data from the database. The problem is:

Check if session is already active before starting.
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
Index.php is the page that opens everything, page_home.php is one of the files that is included in the CMS. page_home.php is the main file, and the file 
in the CMS that opens and navigates the CMS/SPA
I thought that as long as I had:
if (session_status() === PHP_SESSION_NONE) {
session_start();
}

... Then I could have session_start on multiple pages, but it crashes and breaks my site. It looks like a mess. The reason I want the session_start 
control is because it seems like a single session_start is not able to keep the session going throughout the website. Can anyone tell me if they have 
encountered this before, and tell me what I am doing wrong? I am almost out of hair as I am scratching my head so much about this problem!

 

Edited by LeonLatex
Link to comment
https://forums.phpfreaks.com/topic/332407-session_start-problem/
Share on other sites

It's been a while since I've dealt with sessions manually (been in Laravel land for the last 7 or so years), but according to the docs:

Quote

session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.

As far as I can remember as long as you're piping everything through your front loader, a call to session_start() in that file should be sufficient. If you're using index.php as the front loader on some routes and page.php on others, try adding the session_start() call to page.php?

You should not be trying to programmatically determine whether to call session_start.  You should simply be calling it at the start of your index.php file.

if it "loads" everything else, there is no need to have session_start calls anywhere but the index.php.  The session will already be available to any of the include/required scripts.  Remember that $_SERVER is a superglobal, and is always available globally.

  • Like 1

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.