RE: [PHP-DB] php sessions using mysql (or any db)
| From: | Adam Nelson | Date: | Mon, 18 Nov 2002 16:58:53 +0000 |
| Subject: | RE: [PHP-DB] php sessions using mysql (or any db) | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-23443@lists.php.net to get a copy of this message | ||
Thanks for the clarification with the returns. I made the changes
however and it still doesn't work. This code comes pretty much straight
off the presses from http://www.onlamp.com/lpt/a/832
What I have is quite modified from what is on there as it is quite buggy
to begin with. Anyway, It still doesn't work. In perl, this would have
been done already. I can't be the first person to try running sessions
on a database. Does anyone have a session.inc file that would be
appropriate for me. I feel like it should just be in the php base
files.
> -----Original Message-----
> From: Rasmus Lerdorf [mailto:rasmus@php.net]
> Sent: Friday, November 15, 2002 4:59 PM
> To: Adam Nelson
> Cc: php-db@lists.php.net
> Subject: Re: [PHP-DB] php sessions using mysql (or any db)
>
>
> > CREATE TABLE
SessionsTable (
> > SID varchar(32) NOT NULL default '',
>
> This doesn't need to be a varchar. The sid will always be 32
> chars, so
> make it a char(32)
>
This is just an artifact of InnoDB/Mysql. I don't know the specifics,
but I think char is no longer used by InnoDB (ie. it is just an alias
for varchar). Anyway, I put in char, the ddl pops out as varchar.
> > expiration int(11) NOT NULL default '0',
>
> I would suggest using a "timestamp" type here so MySQL will handle
> updating it for you automatically.
timestamp would change with every update. This is a field to describe
when the record should expire
>
> > function mysql_session_open($session_path, $session_name) {
> >
> > mysql_pconnect("localhost", "root", "")
> > or die("Can't connect to MySQL server! ");
> >
> > mysql_select_db("globalDB")
> > or die("Can't select MySQL sessions database");
> >
> > } // end mysql_session_open()
>
> You need to return true; at the end of this.
>
true.
> > function mysql_session_close() {
> >
> > return 1;
>
> No, use return true;
>
> > function mysql_session_select($SID) {
> >
> > GLOBAL $sess_db;
> > GLOBAL $sess_table;
> >
> > $query = "SELECT value FROM $sess_table
> > WHERE SID = '$SID' AND
> > expiration > ". time();
> >
> > $result = mysql_query($query);
> >
> > } // end mysql_session_select()
>
> Uh, you need to return the actual value here or an empty string on an
> error.
>
> > function mysql_session_write($SID,$value) {
> >
> > GLOBAL $sess_db;
> > GLOBAL $sess_table;
> > GLOBAL $lifetime;
> >
> > $expiration = time() + $lifetime;
> >
> > $query = "INSERT INTO $sess_table
> > VALUES('$SID', '$expiration', '$value')";
> >
> > $result = mysql_query($query);
> >
> > if (! $result) :
> >
> > $query = "UPDATE $sess_table SET
> > expiration = '$expiration',
> > value = '$value' WHERE
> > SID = '$SID' AND expiration >". time();
> > $result = mysql_query($query);
> >
> > endif;
> >
> > } // end mysql_session_write()
>
> Again, you *must* return true; on a sucessful write.
>
> > function mysql_session_destroy($sessionID) {
> >
> > GLOBAL $sess_table;
> >
> > $query = "DELETE FROM $sess_table
> > WHERE SID = '$sessionID'";
> > $result = mysql_query($query);
> >
> > } // end mysql_session_destroy()
>
> return true;
>
> -Rasmus
>
>
>
>