PHP php db sessions
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP php db sessions相关的知识,希望对你有一定的参考价值。
// DB Managed Sessions using PHP and MySQL.
// Written by: Greg Donald <gdonald@.gmailcom>
// $Id: session.php 6 2005-12-26 01:22:57Z destiney $
// MySQL database connection parameters:
$GLOBALS[ 'dbhost' ] = 'localhost';
$GLOBALS[ 'dbuser' ] = 'root';
$GLOBALS[ 'dbpasswd' ] = 'changeme';
$GLOBALS[ 'dbname' ] = 'dbsessions';
// Session lifetime
$GLOBALS[ 'online_expire' ] = 900;
// Default sessions table:
$GLOBALS[ 'tb_sessions' ] = 'sessions';
// Use transparent sessions:
ini_set( 'session.use_trans_sid', 1 );
///////////////////////////////////////////////////////////////////////
// Below here should not require any changes
error_reporting( E_ALL );
$GLOBALS[ 'sdbh' ] = '';
function sess_open( $save_path, $session_name )
{
if (! $GLOBALS[ 'sdbh' ] = mysql_pconnect( $GLOBALS[ 'dbhost' ],
$GLOBALS[ 'dbuser' ],
$GLOBALS[ 'dbpasswd' ] ) ){
die( mysql_error() );
}
if( !mysql_select_db( $GLOBALS[ 'dbname' ], $GLOBALS[ 'sdbh' ] ) )
{
die( mysql_error() );
}
return true;
}
function sess_close()
{
return mysql_close( $GLOBALS[ 'sdbh' ] );
}
function sess_read( $key )
{
$key = mysql_real_escape_string( $key );
$sql = "
SELECT `data`
FROM `" . $GLOBALS[ 'tb_sessions' ] . "`
WHERE `id` = '$key'
AND `expire` > UNIX_TIMESTAMP()
";
$query = mysql_query( $sql ) or die( mysql_error() );
if( mysql_num_rows( $query ) )
{
return mysql_result( $query, 0, 'data' );
}
return '';
}
function sess_write( $key, $val )
{
$key = mysql_real_escape_string( $key );
$value = mysql_real_escape_string( $val );
$sql = "
REPLACE INTO `" . $GLOBALS[ 'tb_sessions' ] . "` (
`id`,
`data`,
`expire`
) VALUES (
'$key',
'$value',
UNIX_TIMESTAMP() + " . $GLOBALS[ 'online_expire' ] . "
)
";
return mysql_query( $sql ) or die( mysql_error() );
}
function sess_destroy( $key )
{
$key = mysql_real_escape_string( $key );
$sql = "
DELETE FROM `" . $GLOBALS[ 'tb_sessions' ] . "`
WHERE `id` = '$key'
";
return mysql_query( $sql ) or die( mysql_error() );
}
function sess_gc()
{
$sql = "
DELETE FROM `" . $GLOBALS[ 'tb_sessions' ] . "`
WHERE `expire` < UNIX_TIMESTAMP() - " . $GLOBALS[ 'online_expire' ] . "
";
$query = mysql_query( $sql ) or die( mysql_error() );
return mysql_affected_rows();
}
session_set_save_handler( 'sess_open',
'sess_close',
'sess_read',
'sess_write',
'sess_destroy',
'sess_gc' );
session_start();
$sn = session_name();
$sid = session_id();
以上是关于PHP php db sessions的主要内容,如果未能解决你的问题,请参考以下文章