Euhhh. What a strange discussion.
When a client requests his first page, the webserver will check if this client has an active session. Either because the SID is in the querystring, or there is a sessioncookies set.
On the first page, the parser will automatically add the SID to each link since it doesn't knowif the client accepts cookies.
If the client accpes cookies, the SID is removed from the querystring and the sessioncookie (that only contains an encoded sessionID is used). If cookies are not accepted, the SID is dragged along by the querystring.
This is all done automatically.
I don't quite understand this
Quote:
|
What is the most secure way to use sessions? Currently when a user logs in a save a session variable ofthe username and use this in every subsequent page. This way though, anyone would be able to type the url and append the username to the url to gain access without logging in. I presume it is better to use session ID's somehow, itsjust that im not sure of the best way to do it.
|
Type the url and add the username? what has this got to do with sessions?
After you validated the login, you just set a flag --> set some value in a sessionvariable. like
session_start();
$_SESSION['loggedin']='yes';
and then on top of each page, you do a
session_start();
if (!$_SESSION['loggedin']) or ($_SESSION['loggedin']) !='yes')){
die ('not logged in');
}
Wether the sessionID is pulled from the querystring or cookie is not your concern. It depends mainly on the users cookie settings.
Users that use cookies are safer because that is more dificult to steal. When it is appended to the quertystring, it can be read + it is also appended to external links ... But you can not force the sessions to be cookie-based, unless you deny acces to users without cookiesupport.
The safest way is to also store the IP (for users with a stable IP --> not like AOL) + to combine it with a newly generated sessionID for each request (
http://www.php.net/manual/en/functio...enerate-id.php ) So with each request, you update your sesiontable. Stealing a session would only work if the hacker can request his first page before the user did.
<edit>posts crossed</edit>