search
HomeBackend DevelopmentPHP Tutorial跨页面读取数据库session失败

要疯了。。三天了。。一个问题就是搞不定。不知道问谁去。
问题如下:
我在set_session_test.php中注册的session无法再get_session_php中读取,也就是只能读取当天页面的session。。。为神马???求神人解释。高分求救。



数据库结构如下:

conf.php

<?phpdefine("DBHOST", "localhost");define("DBUSER", "root");define("DBPSW", "");define("DBNAME", "savesession");define("PAGENUM",10);


final_session.php
<?php$gb_DBname = "savesession";$gb_DBuser = "root";$gb_DBpass = "";$gb_DBHOSTname = "localhost";$SESS_DBH = "";$SESS_LIFE = get_cfg_var("session.gc_maxlifetime");function sess_open($save_path, $session_name){	global $gb_DBname,$gb_DBuser,$gb_DBpass,$gb_DBHOSTname,$SESS_DBH;	if(!$SESS_DBH = mysql_pconnect($gb_DBHOSTname,$gb_DBuser,$gb_DBpass)){		echo "<li>Mysql Error:".mysql_errno()."</li>";		die();	}		if(!mysql_select_db($gb_DBname)){		echo "<li>Mysql Error:".mysql_errno()."</li>";		die();	}		return true;}function sess_close(){	return true;}function sess_read($key){	global $SESS_DBH,$SESS_LIFE;		$qry = "SELECT * FROM db_session WHERE sesskey = '$key' and expiry > ".time();	$qid = mysql_query($qry, $SESS_DBH);			if(list($value) = mysql_fetch_row($qid)){		return $value;	}		return false;}function sess_write($key, $val){	global $SESS_DBH,$SESS_LIFE;		$expiry = time()+$SESS_LIFE;	$value = $val;	$qry = "INSERT INTO db_session VALUES('$key', $expiry, '$value')";	//echo $qry;	$qid = mysql_query($qry,$SESS_DBH);	if(!$qid){		$qry = "UPDATE db_session SET expiry=$expiry,value='$value' WHERE sesskey='$key' and expiry>".time();		$qid = mysql_query($qry);	}	return $qid;}function sess_destory($key){	global $SESS_DBH;		$qry = "DELETE FROM db_session WHERE sesskey = '$key'";	$qid = mysql_query($qry, $SESS_DBH);	return $qid;}function sess_gc($maxlifetime){	global $SESS_DBH;		$qry = "DELETE FROM db_session WHERE expiry<".time();	$qid = mysql_query($qry, $SESS_DBH);	return mysql_affected_rows($SESS_DBH);}session_module_name();session_set_save_handler("sess_open", "sess_close", "sess_read", "sess_write", "sess_destory", "sess_gc");


set_session_test.php
<?phpinclude_once 'final_session.php';session_start();$_SESSION['message'] = "Can I help you?";$_SESSION['message1'] = "No, Thank ";$_SESSION['Tom'] = "Tom";echo $_SESSION['message'];echo $_SESSION['message1'];echo $_SESSION['Tom'];echo "<a href=\"get_session_test.php\">显示SESSION</a>";?>


get_session_test.php
<?phpinclude_once 'final_session.php';session_start();$_SESSION['test'] = "test";echo $_SESSION['test'];$_SESSION['test2'] = "test22222222222";echo $_SESSION['test2'];echo $_SESSION['message'];echo $_SESSION['message1'];echo $_SESSION['Tom'];?>


回复讨论(解决方案)

session_set_save_handler()
注意:使用本函数前,先要配置php.ini文件,session.save_hadler=user ,否则,session_set_save_handler()不会生效。

session_set_save_handler()
注意:使用本函数前,先要配置php.ini文件,session.save_hadler=user ,否则,session_set_save_handler()不会生效。

有试过。。也用ini_set()函数设置了。。结果依然是这样的。。。数据库中是有session写入的。。

if(list($value) = mysql_fetch_row($qid)){
????

你查询的是全部字段,为什么只取出第一个?
那不是 sessionid 吗?

把sess_read里的

$qry = "SELECT * FROM db_session WHERE sesskey = '$key' and expiry > ".time();

换成
$qry = "SELECT value FROM db_session WHERE sesskey = '$key' and expiry > ".time();

就可以了。 

你试用cookie看可以解决你的问题吗。

楼主$_SESSION['message'] = "Can I help you?";
输出变量看看。echo $_SESSION['message'];

.......
试了了一下 楼主的代码 ...

依然木有正解。

难道木有高人了?

这比我那个页内传值的问题高深多了,我只不过想把表单输入的内容都存储下来 ,想什么时候用旧什么时候用,这个功能比我那个全多了。。。时刻关注。

还有一处

function sess_write($key, $val){    global $SESS_DBH,$SESS_LIFE;        $expiry = time()+$SESS_LIFE;    $value = $val;    $qry = "REPLACE INTO db_session VALUES('$key', $expiry, '$value')";    $qid = mysql_query($qry,$SESS_DBH);}

好的。。我去试下。。但感觉应该不是这个问题。。因为session已经成功写入。。是读取的问题。

还有一处
PHP code
function sess_write($key, $val){
    global $SESS_DBH,$SESS_LIFE;
    
    $expiry = time()+$SESS_LIFE;
    $value = $val;
    $qry = "REPLACE INTO db_session VALUES('$key', $expiry, ……

还是不行。。。不过学会了REPLACE的用法。。受教了。

等待高人解决。。另外本人贴张图上来。好让大家明白究竟什么样的存储结构。


好的。。我去试下。。但感觉应该不是这个问题。。因为session已经成功写入。。是读取的问题。

引用 11 楼  的回复:

还有一处
PHP code
function sess_write($key, $val){
global $SESS_DBH,$SESS_LIFE;

$expiry = time()+$SESS_LIFE;
$value = $val;
$qr……

加分悬赏。提供正解者。加分100.

SESSIONID($key)是保存在COOKIE里的,有没有设置它的保存时间呢?
能确保再次访问的时候能拿到跟前次访问相同的$key?

怎么还没搞好呢?
这是我改写的你的代码。
因为我不喜欢用全局变量改成了常量,另外改了一下库名和表名

<?phpdefine("DBHOST", "localhost");define("DBUSER", "root");define("DBPSW", "");define("DBNAME", "test");define("PAGENUM",10);define("SESS_LIFE", get_cfg_var("session.gc_maxlifetime"));function sess_open($save_path, $session_name){    if(! $conn = mysql_pconnect(DBHOST, DBUSER, DBPSW)){        echo "<li>Mysql Error:".mysql_errno()."</li>";        die();    }    define("SESS_DBH", $conn);    if(!mysql_select_db(DBNAME, SESS_DBH)){        echo "<li>Mysql Error:".mysql_errno()."</li>";        die();    }    return true;}function sess_close(){    return true;}function sess_read($key){    $qry = "SELECT * FROM sessions WHERE sesskey = '$key' and expiry > ".time();    $qid = mysql_query($qry, SESS_DBH) or die(mysql_error());        $r = mysql_fetch_assoc($qid);    return $r['value'];}function sess_write($key, $val){    $expiry = time()+SESS_LIFE;    $value = $val;    $qry = "REPLACE INTO sessions VALUES('$key', $expiry, '$value')";    $qid = mysql_query($qry, SESS_DBH);}function sess_destory($key){    $qry = "DELETE FROM sessions WHERE sesskey = '$key'";    $qid = mysql_query($qry, SESS_DBH);    return $qid;}function sess_gc($maxlifetime){    $qry = "DELETE FROM sessions WHERE expiry<".time();    $qid = mysql_query($qry, SESS_DBH);    return mysql_affected_rows(SESS_DBH);}//session_module_name();session_set_save_handler("sess_open", "sess_close", "sess_read", "sess_write", "sess_destory", "sess_gc");session_start();

sess_read里的  

  if(list($value) = mysql_fetch_row($qid)){        return $value;    }

错了,只需要返回session的序列化串,也就是表内的value字段,改成
    if($value = mysql_fetch_row($qid)){        return $value[2];    }

严重感谢。。。代码测试可以正常运行。。但我还是想知道是神马毛病。
怎么还没搞好呢?
这是我改写的你的代码。
因为我不喜欢用全局变量改成了常量,另外改了一下库名和表名
PHP code
define("DBHOST", "localhost");
define("DBUSER", "root");
define("DBPSW", "");
define("DBNAME", "test");
define("PAGENUM",10);
defi……

那段代码木有问题。。测试过。。换了结果还是一样的。
sess_read里的  
PHP code
  if(list($value) = mysql_fetch_row($qid)){
        return $value;
    }

错了,只需要返回session的序列化串,也就是表内的value字段,改成
PHP code
    if($value = mysql_fetch_row($qid)){
        re……

100分送你了。。。稍等我看下能不能找出什么毛病。。这两天就结贴。
怎么还没搞好呢?
这是我改写的你的代码。
因为我不喜欢用全局变量改成了常量,另外改了一下库名和表名
PHP code
define("DBHOST", "localhost");
define("DBUSER", "root");
define("DBPSW", "");
define("DBNAME", "test");
define("PAGENUM",10);
defi……

因为你的表中的sesskey列没有设为主键或惟一约束,所以在每次在session_start();会产生一个一样的SID,把数据库表sesskey列设置为主键或惟一键即可解决。

注:你多刷新几次你的测试代码,然后去数据库查一下会发现有多个重复的SID

我的数据库是有设主键的。。你说的情况我明白。但不是这种情况。
我有个回帖里面也提到了这个问题。
数据库的写入是没有问题的。。可以正常插入。

但是你设的主键是不是(sesskey和expiry),而不是sesskey吧???

不是组合主键。。。主键只有sesskey....
但是你设的主键是不是(sesskey和expiry),而不是sesskey吧???

问题可能出在:

function sess_read($key){
    $qry = "SELECT * FROM sessions WHERE sesskey = '$key' and expiry > ".time();

你把$qry打印出来,看看能不能取到?

sess_read里的  

数据库读出来后 只能返回value字段里面的数据! 不能返回整条数据

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
11 Best PHP URL Shortener Scripts (Free and Premium)11 Best PHP URL Shortener Scripts (Free and Premium)Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

Build a React App With a Laravel Back End: Part 2, ReactBuild a React App With a Laravel Back End: Part 2, ReactMar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation SurveyAnnouncement of 2025 PHP Situation SurveyMar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

Notifications in LaravelNotifications in LaravelMar 04, 2025 am 09:22 AM

In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications ov

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.