views:

365

answers:

2

Is it possible to check for a session with out starting one?

The reason that I ask is, the app I am developing has an integrated admin interface. So when an admin is logged in they browse the same pages as the users to make their edits. Fields and options are shown based on the users privs.

This is causing two problems.

One is Because a session is being started, I can not enable browser caching features as the headers being sent are always:

Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0

I am using smarty to output the templates, and can't implement the:

$smarty->cache_modified_check = true;

to send a 304 not modified because a session has already been started. Using the above smarty param would be the perfect solution to browser caching for me.

Two is because every person using the site is starting a session the session directory gets filled with unneeded sessions.

I could just destroy the session if the user is not logged in, but then every single page load, the user would be creating and deleting a session. Is that bad practice?

So if I could just check to see if an active session exists without starting one all my problems would be solved. Any ideas? Doesn't the browser send the session cookie when requesting the page?

Something Ideally like this:

if (session_exists) {
 session_start();
 $users->priv = $_SESSION['priv'];
}
else {
 $users->priv = guest;
}

--------------- In response to Tony Miller ---------------

When using session_id(), you have to already have a session started for it to return an id.

session_start();
echo session_id($_SESSION);

or you can set an id for the session before calling session start

session_id("adfasdf");
session_start();
echo session_id($_SESSION);

//prints "adfasdf"

Neither of these help me. Unless I am missing something.

+10  A: 

You'd be wanting session_id(), which returns empty string if no session is defined.

Tony Miller
Could you please elaborate on this? It looks like there are two uses for session_id. 1. session_id($_SESSION) returns the id of the session, which has to be called after session_start(). 2. calling session_id("asdf") before session_start() sets the id of the session to "asdf".I'm not sure how either one of these help me. How will I know the current browsers session if I give it a new id before calling session_start()?Thanks
Actually, session_id() returns the current session id - which will be empty string ('') if there is none. I don't think session_is($_SESSION) is a valid use.
K Prime
i've edited my question for you.
session_is($_SESSION) is invalid. It will try to set the session id to whatever is there in $_SESSION - most probably it will be a string with value 'Array'. It is a huge security threat.
mixdev
Calling session_id() before session_start() doesn't return anything, even if a POTENTIAL session is available (i.e. even if PHPSESSID is set to something).
nezroy
+1  A: 

You could check if the PHPSESSID cookie is set (the PHPSESSID name may have another name, depending on your server settings, check ini.session.name).

But if all you fear is poluting your session dir, you can adjust session.gc_probability, session.gc_divisor and session.gc_maxlifetime to make them disappear faster.

Arkh
This seems to be the ticket. Checking if $_REQUEST['PHPSESSID'] is set before calling session_start() allows you to know if a session is "available".
nezroy