Re: php sessions using mysql (or any db)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> 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)

>   `expiration` int(11) NOT NULL default '0',

I would suggest using a "timestamp" type here so MySQL will handle
updating it for you automatically.

> 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.

> 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


-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux