|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2002-03-29 13:25 UTC] adam at adeptsoftware dot com
There is no way, that I can see, of knowing if a session exists before starting it. I am trying to destroy a session, but only if it already exists. If it doesn't, I basically have to create it then destroy it. The problem, besides this being lame, is the browser can pop up a box asking the user if they want to accept a session cookie, when I am not even trying to start a session. It might be better if session_destroy just didn't require the session to be started. PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 29 10:00:01 2025 UTC |
I've just run into this issue myself (or one of the issues mentioned here anyway, I wanted to only start a session if the user is already logged in, or at the point of logging in with a correct username/password, rather than starting a session for every unauthenticated page request). I've found one way round it which seems to work for me at least, which is to check whether the session cookie is set, like so: if(isset($_COOKIE[session_name()])){session_start();} (the session name, and therefore the name of the cookie, is normally PHPSESSID by default, and the value of that cookie should be the session ID if you need it) I then used the following code in the login page after successful verification of the username/password, before starting to set session variables: if(session_id()==''){session_start();} Obviously this method will only work with cookie-based sessions, I haven't looked into how exactly PHP handles URL-based sessions, but I expect it would just be a matter of checking $_GET as well as $_COOKIE. Also, this obviously won't help if you need to check whether a given session ID already exists on the server or not, which I think is a slightly different issue.I've just found a potentially exploitable hole which is opened up by not always starting the session (as described in my previous comment), if your server has register_globals turned on. So just in case, to close the hole and keep things properly secure, the first line of code in my previous comment should be changed to this: if(isset($_COOKIE[session_name()])){session_start();}else{unset($_SESSION);}