Implement session function in PHP3 (one, Session function library: SESSION.INC.PHP3) (translate)

Source: Internet
Author: User
Tags eval hash session id variables split strlen table name variable
session| function <?php
if (!isset ($__session_inc__)) {
$__session_inc__=1;
Require ("cookie.inc.php3");
# -------------------------------------------------------------------
# Session Management v1.0 21.6.1998
# (c) Wild Karl-Heinz <kh.wild@wicom.at>
#
# This Include handle session based variable handling
#
# Please feel and use it. If You do it more functional
# It would is nice to send me a copy.
#
# Don ' t Forget-mysql_connect!
#
# The database structure
# Table structure for table ' session '
#
# CREATE TABLE Session (
# ID Int (one) DEFAULT ' 0 ' not NULL auto_increment,
# SID varchar DEFAULT ' not NULL,
# val Blob,
# times Timestamp (14),
# PRIMARY KEY (ID),
# KEY sid (SID),
# UNIQUE sid_2 (SID)
#  );
#
# You'll miss here a cron job to delete the old sessions from DB
# -------------------------------------------------------------------

Note the CREATE TABLE statement that is commented out above.
You need to execute this statement on the database you are using,
The table name can also not be a session, then you need to set the following $sess_table variable.

Here you need to set the library name, and the table name.
However, it is generally recommended that you use the session as the table name
$sess _db = ' dbname ';
$sess _table = ' Session ';

# ----------------------------------------------------
# Session_checkid-Check, set, and return Session-id
# Parameters ...: Cookie save time (in minutes)
# can also not be set to indicate that this cookie is valid only in the current session
# This is actually like the duration of the session in ASP.
# return value ...: a unique session-id (stored as cookies)
# ----------------------------------------------------
function Session_checkid ($min)
{
Global $sess _sid;

if (! $sess _sid) {
$sess _sid = uniqid (SC); To get a unique random number
/*
if ($min > 0) {
Setcookie ("Sess_sid", $sess _sid, Time () + ($min *60), "/", "", 0);
}
else {
Setcookie ("Sess_sid", $sess _sid, "", "/", "", 0);
}
This is the original code, there will be an error. So in addition, a better function is used.
function library: cookie.inc.php3
*/
Jssetcookie ("Sess_sid", $sess _sid, $min);
return (false);
}
else {
return (true);
}
}

# ----------------------------------------------------------
# Str2arr-Converts a string into a session array
# parameters ...: string
# return value ...: Global array (in fact, session)
#本函数用途: Converts a string into a session array
#如 "session[username]=yourid&session[userpass]=12345"
#将会被转换成下面的数组
# session[username]= "Yourid"
# session[userpass]= "12345"
#请注意函数split (), each (), list (), eval () usage.
# ----------------------------------------------------------
function Str2arr ($ts)
{
Global $session;

$vals = Split ("&", $ts);
while (the list ($key, $val) = each ($vals)) {
List ($name, $wert) = Split ("=", $val);
if ($val) eval ("\$ $name = \" $wert \; ");
}
}

# ----------------------------------------------------------
# Session_read ()-takes data from the session table and converts it into a session array
# parameters ...: None
# return value ...: If read out data, return True, otherwise return false
#注意 ...: The function of Str2arr () is used.
# ----------------------------------------------------------
function Session_read ()
{
# Hash Array to keep session-variables
Global $session;
Global $sess _sid, $sess _db, $sess _table, $sess _error;

$sel = "Select Val from $sess _table where sid = ' $sess _sid '";
$res = Mysql_db_query ($sess _db, $sel);
if (Mysql_numrows ($res)) {
$val = mysql_result ($res, 0, "Val");
Str2arr ($val);
Mysql_free_result ($res);
return (true);
}
else {
return (false);
$sess _error = Mysql_error ();
}
}

# ------------------------------------------------------
# Split_array ()-Converts the session array into a string
# Parameters ...: array
# return value ...: array-converted String
#
# to Rasmus (this guy seems to be the inventor of PHP)
# Note: Converts the session array into a string
#如session [username]= "Yourid"
# session[userpass]= "12345"
#将会被转换成 "session[username]=yourid&session[userpass]=12345"
#同时该函数考虑到了数组的某个元素也是数据的情况
#这个函数被设计成一个递归函数
# ------------------------------------------------------
function Split_array ($arr, $a = "", $b = "", $c = "")
{
while (the list ($key, $val) = each ($arr)) {
if (Is_array ($val)) {
$ts. = Split_array ($arr [$key],
(strlen ($a) $a: $key),
(Strlen ($b) $b: (strlen ($a)? $key: ")),
(Strlen ($c) $c: (Strlen ($b) ($key: ""));
}
else {
$ts. = "Session";
$ts. = $a? "[$a]": ";
$ts. = $b? "[$b]": ";
$ts. = $c? "[$c]": ";
$ts. = "[$key]= $val &";
}
}
return ($TS);
}

# ---------------------------------------------------
# Session_write-Converts the session array to a string and then saves it to the session table
# parameters.: None
# return Value ...: Returns True if the deposit is normal, or false
# ---------------------------------------------------
function Session_write ()
{
# Hash Array to keep session-variables
Global $session;

Global $sess _sid, $sess _db, $sess _table;
Global $sess _error;

# If you like to delete a Session-cookie
# You must check it before writting
# array

if (! $sess _sid) {session_checkid (0);}

$ts = Split_array ($session);
if ($ts > "") {$ts = substr ($ts, 0, strlen ($ts)-1);
$res = Mysql_db_query ($sess _db, "Select * from session where SID = ' $sess _s '");
if (mysql_numrows ($res) = = 0) {
$sel = "Insert into $sess _table (IDs, Sid, Val, Times)";
$sel. = "VALUES (0, ' $sess _sid ', ' $ts ', NULL)";
}
else {
$sel = "Update $sess _table Set val = ' $ts ',";
$sel. = "Times = NULL where sid = ' $sess _sid '";
}
if (!mysql_db_query ($sess _db, $sel)) {
$sess _error = Mysql_error ();
return (false);
}
else {return (true);}
}

# ---------------------------------------------
# Session_del-Clears all current sessions
# and delete records in the session table that are related to the current session
# parameters ...: a random session ID
# return value ...: none
# ---------------------------------------------
function Session_del ()
{
Global $session, $sess _db, $sess _table, $sess _sid;

$sel = "Delete from $sess _table where sid = ' $sess _sid '";
if (!mysql_db_query ($sess _db, $sel)) {
$sess _error = Mysql_error ();
}
$sess _sid = ';
}
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.