function of the session in PHP3 (i) _php tutorial

Source: Internet
Author: User
Tags array to string setcookie
Session function Library: SESSION.INC.PHP3

if (!isset ($__session_inc__)) {
$__session_inc__=1;
Require ("cookie.inc.php3");
# -------------------------------------------------------------------
# Session Management v1.0 21.6.1998
# (c) Wild Karl Heinz
#
# This Include handle Session based variable handling
#
# Feel free 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 was commented out above,
You need to execute this statement on the database you are using,
The table name may not be a session, so you need to set the following $sess_table variable.

Here you need to set the library name, and the table name.
However, the general recommendation is to 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)
# also not set to indicate that this cookie is valid only at the current session
# This is just like the time of the session in ASP.
# return value ....: A unique Session-id (stored as a cookie)
# ----------------------------------------------------
function Session_checkid ($min)
{
Global $sess _sid;

if (! $sess _sid) {
$sess _sid = uniqid (SC); Get a unique random number
/*
if ($min > 0) {
Setcookie ("Sess_sid", $sess _sid, Time () + ($min *60), "/", "", 0);
}
else {
Setcookie ("Sess_sid", $sess _sid, "", "/", "", 0);
}
Above is the original code, error. So 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)
#本函数用途: Converting 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 (list ($key, $val) = each ($vals)) {
List ($name, $wert) = Split ("=", $val);
if ($val) eval ("$ $name =" $wert ";");
}
}

# ----------------------------------------------------------
# Session_read ()-takes data from the session table and transforms it into a session array
# parameter ...: none
# return Value ...: Returns TRUE if data is read, False otherwise
#注意 ...: the Str2arr () function 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 to a string
# parameter ...: array
# return value ...: array-derived string
#
# Thanks to Rasmus (this person seems to be the inventor of PHP)
# Note: Convert session array to string
#如session [username]= "Yourid"
# session[userpass]= "12345"
#将会被转换成 "session[username]=yourid&session[userpass]=12345"
#同时该函数考虑到了数组的某个元素也是数据的情况
#这个函数被设计成一个递归函数
# ------------------------------------------------------
function Split_array ($arr, $a = "", $b = "", $c = "")
{
while (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 to the session table
# parameter.: none
# return Value ...: Returns True if the deposit is normal, otherwise 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 the session
# 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 (ID, 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 session
# and delete records related to the current session in the session table
# parameter ...: 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 = ";
}
}
?>

Original Author: unknown

http://www.bkjia.com/PHPjc/316688.html www.bkjia.com true http://www.bkjia.com/PHPjc/316688.html techarticle Session function library: session.inc.php3 php if (!isset ($__session_inc__)) {$__session_inc__=1;//require (COOKIE.INC.PHP3); # -----------------------------------------------------------...

  • 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.