note 54885 added to function.setcookie
| From: | lharkleroad at gmail dot com | Date: | Mon, 18 Jul 2005 22:12:57 +0000 |
| Subject: | note 54885 added to function.setcookie | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-92132@lists.php.net to get a copy of this message | ||
In response to Snupha's note, one way to get the desired result on the server side, and
therefore work around the user's local date and time, is to store your session information in a
sessions table in your database.
For example:
1. Create a sessions table in your favorite db with a 32 char sessionid column, modifed and created
datetime columns, and any other columns that you want to associate with the session such as a
userid.
2. In your PHP script, create a variable containing the 32 char unique ID for your session, and
store the result in a sessionid cookie that expires at the end of the browser session, and also
insert it in a new row in your sessions table.
3. When your scripts check for the existance of the sessionid cookie, if a value is returned, they
would check against the sessions table in your database to find the matching ID, updating the
modified datetime for that record to keep the session active. If the server time elapsed since the
record was modified exceeds your timeout constraints, you can force the user to reauthenticate and
issue them a new session ID if desired.
4. If you require a login to some part of your site, you can also update the session record with the
userid to link the session to the user.
I decided to use this method on a recent project where both PHP and ColdFusion needed to access the
same datasource for different applications. The result is a language-independent session in which
multiple web servers on the same domain (but potentially different sub-domains) can know when the
user has logged in and allow them to continue the session on the other server. Otherwise, using the
sessions built in to PHP and ColdFusion would require separate logins when switching between
applications programmed in the two languages.
Going a little further with this, you can then report stats on the sessions table for the average
session length by comparing the created and modified columns, or get a session history for a
particular user.
I hope this helps a few people. Have fun!
----
Manual Page -- http://www.php.net/manual/en/function.setcookie.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+54885
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54885&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54885&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54885&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54885&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54885&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54885&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+54885&report=yes
Search -- http://master.php.net/manage/user-notes.php