Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if there is a session without using session_start()

Tags:

php

I want to check if a person has an active session and redirect them to another page when they have one. However, I do not want to use session_start(), as that will place a cookie on the persons PC (I do not want to place cookies on peoples' PC when they're not logged in). Is there a way to check for an existing session, without placing a cookie on their PC?

like image 828
It's a me Applez Avatar asked May 11 '11 14:05

It's a me Applez


1 Answers

You can check for the existence of the session ID cookie, which the client would send back if it had been previous set elsewhere in your site:

if (isset($_COOKIE[session_name()])) {
   ... most likely there's a session available to be loaded ...
}

For added safety, you could then check for the existence of the session file (assuming you're using the default file-based handler) using session_save_path() and the session_name() to build up a path to pass into file_exists()

like image 74
Marc B Avatar answered Nov 11 '22 05:11

Marc B