Return to Snippet

Revision: 1211
at September 26, 2006 06:16 by gdonald


Updated Code
// DB Managed Sessions using PHP and MySQL.
// Written by: Greg Donald <[email protected]>
// $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();

Revision: 1210
at September 26, 2006 06:00 by gdonald


Updated Code
// DB Managed Sessions using PHP and MySQL.
// Written by: Greg Donald <[email protected]>
// $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();

Revision: 1209
at September 26, 2006 05:58 by gdonald


Initial Code
// DB Managed Sessions using PHP and MySQL.
// Written by: Greg Donald <[email protected]>
// $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();

Initial URL

                                

Initial Description

                                

Initial Title
php db sessions

Initial Tags
php

Initial Language
PHP