PHP Classes

File: example2.php

Recommend this page to a friend!
  Classes of Radovan Janjic   PHP MySQL Session Handler   example2.php   Download  
File: example2.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP MySQL Session Handler
Save and retrieve PHP session data in MySQL
Author: By
Last change:
Date: 11 years ago
Size: 1,117 bytes
 

Contents

Class file image Download
<?php

/** PHP MySQL Session Handler Example - Use same db connection for sessions and other coding ...
 *
 * PHP version required (4 >= 4.0.2, 5)
 *
 */

include "MySQL_wrapper.class.php";
include
"MySQL_Session_Handler.class.php";

define('MYSQL_HOST', 'localhost');
define('MYSQL_USERNAME', 'root');
define('MYSQL_PASSWORD', '');
define('MYSQL_DATABASE', 'users');

$db = new MySQL_wrapper(MYSQL_HOST, MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_DATABASE);

// Create object and start session
$GLOBALS['MYSQL_SESSION'] = new MySQL_Session_Handler($db, 'sessions');

//*
$_SESSION['asdf1'] = 'asdf';
$_SESSION['asdf2'] = 'asdf';
$_SESSION['asdf3'] = 'asdf';
$_SESSION['asdf4'] = 'asdf';
$_SESSION['asdf5'] = 'Array ( [asdf] => asdf [asdf1] => asdf [asdf2] => asdf [asdf3] => asdf [asdf4] => asdf [asdf5] => asdfa )';
//*/

echo '<pre>';

echo
'Your session:', PHP_EOL, print_r($_SESSION, TRUE);

// Do queries and stuff with same connection ...
echo 'Your session id db:', PHP_EOL, print_r($db->fetchQueryToArray('SELECT * FROM `sessions`;'), TRUE);

// Hey, check your database ;-)