Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
<?php
$SESS_DBHOST = "localhost"; /* stores database server hostname */
$SESS_DBNAME = "test"; /* store database name */
$SESS_DBTABLE = "test"; /* stores table name */
$SESS_DBUSER = "test"; /* stores database user */
$SESS_DBPASS = "test"; /* stores database password */
$SESS_LIFE = ini_get("session.gc_maxlifetime");
/* This is the function that will be registered as the "open" function of our
sessions. It creates a persistent database connection to the database if one
does not exist. If the connection has already been opened, PHP will return
handle of the previously opened handle. See the PHP online manual listing
for mysql_pconnect for more information.
*/
function sess_open($save_path, $session_name)
{
global $SESS_DBHOST, $SESS_DBNAME, $SESS_DBUSER, $SESS_DBPASS, $SESS_DBH;
$SESS_DBH = mysql_pconnect($SESS_DBHOST, $SESS_DBUSER, $SESS_DBPASS);
if ($SESS_DBH == FALSE)
{
print "Can't connect to $SESS_DBHOST as $SESS_DBUSER";
print "MySQL Error: ";
print mysql_error();
die;
}
if (! mysql_select_db($SESS_DBNAME, $SESS_DBH))
{
print "Unable to select database $SESS_DBNAME";
die;
}
return true;
}
/* This function will be registered to close session handling. Normally,
this means finishing all writes and closing the session store on the
filesystem. This function does nothing. We are using persistent database
connections so we never close the connection.
*/
function sess_close()
{
return true;
}
/* This function will retrieve session variables for a particular session key. Each
time a session is read from the database, this function "freshens" the session
so that it will not be removed by garbage collection. PHP handles de-serializing
the data for you.
*/
function sess_read($key)
{
global $SESS_DBH, $SESS_LIFE, $SESS_DBTABLE;
$qry = "SELECT value FROM $SESS_DBTABLE WHERE sesskey = '$key' AND expiry > " . time();
$qid = mysql_query($qry, $SESS_DBH);
if (list($value) = mysql_fetch_row($qid))
{
$expiry = time() + $SESS_LIFE;
$qry = "UPDATE $SESS_DBTABLE SET expiry = $expiry WHERE sesskey = '$key'";
$qid = mysql_query($qry, $SESS_DBH);
return $value;
}
return "";
}
/* This function stores the session data for a particular key. If the key
already exists in the table, it updates the key's value and expiry time.
PHP serializes the session data and sends it in as a string.
*/
function sess_write($key, $val)
{
global $SESS_DBH, $SESS_LIFE, $SESS_DBTABLE;
$expiry = time() + $SESS_LIFE;
$value = addslashes($val);
$qry = "INSERT INTO $SESS_DBTABLE VALUES ('$key', $expiry, '$value')";
$qid = mysql_query($qry, $SESS_DBH);
if (! $qid)
{
$qry = "UPDATE $SESS_DBTABLE SET expiry = $expiry, value = '$value' WHERE sesskey = '$key' AND expiry > " . time();
$qid = mysql_query($qry, $SESS_DBH);
}
return $qid;
}
/* This destroys a key by deleting its record from the table
*/
function sess_destroy($key)
{
global $SESS_DBH;
$qry = "DELETE FROM $SESS_DBTABLE WHERE sesskey = '$key'";
$qid = mysql_query($qry, $SESS_DBH);
return $qid;
}
/* This is the function which collects garbage. The probability that PHP
will run this function is set in the php.ini directive
"session.gc_probability"
*/
function sess_gc ($maxlifetime)
{
global $SESS_DBH, $SESS_LIFE;
$qry = "DELETE FROM $SESS_DBTABLE WHERE expiry < " . time();
$qid = mysql_query($qry, $SESS_DBH);
return mysql_affected_rows($SESS_DBH);
}
/* This registers the above functions as the handlers for the various operations
on session data.
*/
session_set_save_handler
(
"sess_open",
"sess_close",
"sess_read",
"sess_write",
"sess_destroy",
"sess_gc"
);
session_cache_limiter('public');
session_start();
?>