在PHP3中实现SESSION的功能_PHP
SESSION函数库: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
#
# Please feel free and use it. If you make it more functional
# it would be nice to send me a copy.
#
# Don’t forget - Mysql_connect !
#
# The database structure
# Table structure for table ’session’
#
# CREATE TABLE session (
# id int(11) DEFAULT ’0’ NOT NULL auto_increment,
# sid varchar(20) 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
# -------------------------------------------------------------------
// 请注意上面被注释掉的CREATE TABLE语句,
// 你需要在你所使用的数据库上执行这条语句,
// 表名也可以不是session,那么就需要设置下面的$sess_table变量了。
// 此处你需要设置库名,和表名。
// 不过一般建议就使用session作为表名
$sess_db = ’dbname’;
$sess_table = ’session’;
# ----------------------------------------------------
# Session_CheckID - 检查、设置并返回 Session-ID
# 参数......: cookie保存时间(以分钟计)
# 也可不设置表示这个 cookie 只在当前session 有效
# 这其实就象ASP中SESSION的时效一样。
# 返回值....: 一个唯一的Session-ID (作为cookie存储)
# ----------------------------------------------------
function Session_CheckID( $min )
{
global $sess_sid;
if( !$sess_sid ) {
$sess_sid = uniqid( SC ); //取得一个唯一的随机数
/*
if( $min > 0 ) {
SetCookie("sess_sid", $sess_sid, time() ($min*60), "/", "", 0 );
}
else {
SetCookie("sess_sid", $sess_sid, "", "/", "", 0 );
}
上面是原先的代码,会出错。所以另外用了一个更好的函数。
函数库:cookie.inc.php3
*/
jssetcookie("sess_sid",$sess_sid,$min);
return( false );
}
else {
return( true );
}
}
# ----------------------------------------------------------
# str2arr - 将字符串转换成session数组
# 参数.....: string
# 返回值...: 全局数组(其实就是session)
#本函数用途:将字符串转换成session数组
#如"session[username]=yourid&session[userpass]=12345"
#将会被转换成下面的数组
# session[username]="yourid"
# session[userpass]="12345"
#请注意函数split(),each(),list(),eval()的用法。
# ----------------------------------------------------------
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() - 从SESSION表中取数据,转换成session数组
# 参数........: 无
# 返回值......: 如果读出数据,返回 true ,否则返回 false
#注意.........: 用到了str2arr()这个函数
# ----------------------------------------------------------
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() - 将session数组转换成字符串
# 参数.......: 数组
# 返回值.....: 数组转换得来的字符串
#
# Thanks to Rasmus (这人好象是PHP的发明人)
# 注意:将session数组转换成字符串
#如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 - 将session数组转换成字符串,再存到session表中
# 参数.: 无
# 返回值...: 如果存入正常返回 true ,否则返回 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 - 清除当前所有的session
# 并删除session表中和当前session有关的记录
# 参数.....: 一个随机的session id
# 返回值...: 无
# ---------------------------------------------
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 = ’’;
}
}
?>
原作者:不详

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Both vivox100s and x100 mobile phones are representative models in vivo's mobile phone product line. They respectively represent vivo's high-end technology level in different time periods. Therefore, the two mobile phones have certain differences in design, performance and functions. This article will conduct a detailed comparison between these two mobile phones in terms of performance comparison and function analysis to help consumers better choose the mobile phone that suits them. First, let’s look at the performance comparison between vivox100s and x100. vivox100s is equipped with the latest

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

With the rapid development of the Internet, the concept of self-media has become deeply rooted in people's hearts. So, what exactly is self-media? What are its main features and functions? Next, we will explore these issues one by one. 1. What exactly is self-media? We-media, as the name suggests, means you are the media. It refers to an information carrier through which individuals or teams can independently create, edit, publish and disseminate content through the Internet platform. Different from traditional media, such as newspapers, television, radio, etc., self-media is more interactive and personalized, allowing everyone to become a producer and disseminator of information. 2. What are the main features and functions of self-media? 1. Low threshold: The rise of self-media has lowered the threshold for entering the media industry. Cumbersome equipment and professional teams are no longer needed.

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

As Xiaohongshu becomes popular among young people, more and more people are beginning to use this platform to share various aspects of their experiences and life insights. How to effectively manage multiple Xiaohongshu accounts has become a key issue. In this article, we will discuss some of the features of Xiaohongshu account management software and explore how to better manage your Xiaohongshu account. As social media grows, many people find themselves needing to manage multiple social accounts. This is also a challenge for Xiaohongshu users. Some Xiaohongshu account management software can help users manage multiple accounts more easily, including automatic content publishing, scheduled publishing, data analysis and other functions. Through these tools, users can manage their accounts more efficiently and increase their account exposure and attention. In addition, Xiaohongshu account management software has

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

PHP Tips: Quickly implement the function of returning to the previous page. In web development, we often encounter the need to implement the function of returning to the previous page. Such operations can improve the user experience and make it easier for users to navigate between web pages. In PHP, we can achieve this function through some simple code. This article will introduce how to quickly implement the function of returning to the previous page and provide specific PHP code examples. In PHP, we can use $_SERVER['HTTP_REFERER'] to get the URL of the previous page

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.
