PHP:会话和 mysql_pconnect() 都不断启动新会话/进程
只是想提前为写了这么多文字表示歉意。问题是:我使用持久连接以 60 秒的 wait_timeout 连接到数据库,并将会话数据存储在 MySQL 表中。我遇到的问题是会话似乎不使用自己的行;每次页面刷新都会启动一个新会话,而不是使用旧会话。更重要的是,前面提到的持久连接不断启动新进程,而不是像他们应该的那样使用自己的进程。由于这两个问题似乎有相同的根源,所以我决定将它们放在一起。我的PHP代码如下:(
mysql_pconnect('localhost', 'root') or die ('无法连接:'.mysql_error()); mysql_set_charset('utf8');
mysql_select_db('azgoth') or die('无法选择数据库:' .mysql_error());
session_set_cookie_params(3600,'/','www.azgoth',FALSE,TRUE);
session_set_save_handler('_open','_close','_read','_write','_destroy','_clean');
function _open(){
return true;}
function _close(){
return true;}
function _read($id){
$id = mysql_real_escape_string($id);
if ($result = mysql_query("SELECT data FROM sess_en WHERE id='$id'")) {
if (mysql_num_rows($result)) {
$record = mysql_fetch_assoc($result);
return $record['data'];}}
return '';}
function _write($id, $data){
$access = $_SERVER['REQUEST_TIME'];
$id = mysql_real_escape_string($id);
$access = mysql_real_escape_string($access);
$data = mysql_real_escape_string($data);
return mysql_query("REPLACE INTO sess_en VALUES('$id', '$access', '$data')");}
function _destroy($id){
$id = mysql_real_escape_string($id);
return mysql_query("DELETE FROM sess_en WHERE id='$id'");}
function _clean($max){
$old = $_SERVER['REQUEST_TIME'] - $max;
$old = mysql_real_escape_string($old);
return mysql_query("DELETE FROM sess_en WHERE access<'$old'");}
session_start();
关于可能导致此问题的原因有什么想法吗?
编辑:我一开始以为这只是在我的脑海中,但我现在可以确认这一点:这个奇怪的东西不断随机出现:它通常会出现,但有时(很少,事实上)不会......
Just want to apologise in advance for writing so much text. Here is the problem: I use a persistent connection to connect to the database with a wait_timeout of 60 seconds and I store session data in a MySQL table. The problem I have is that the sessions just don't seem to use their own rows; each page refresh keeps starting a new session instead of using the old one. What is more, the persistent connections mentioned earlier keep starting new processes insead of using their own as they should. Since these two problems seem to have the same origin, I decided to put them here together. My PHP code is the following:
mysql_pconnect('localhost', 'root') or die('Could not connect: ' . mysql_error());
mysql_set_charset('utf8');
mysql_select_db('azgoth') or die('Could not choose DB: ' . mysql_error());
session_set_cookie_params(3600,'/','www.azgoth',FALSE,TRUE);
session_set_save_handler('_open','_close','_read','_write','_destroy','_clean');
function _open(){
return true;}
function _close(){
return true;}
function _read($id){
$id = mysql_real_escape_string($id);
if ($result = mysql_query("SELECT data FROM sess_en WHERE id='$id'")) {
if (mysql_num_rows($result)) {
$record = mysql_fetch_assoc($result);
return $record['data'];}}
return '';}
function _write($id, $data){
$access = $_SERVER['REQUEST_TIME'];
$id = mysql_real_escape_string($id);
$access = mysql_real_escape_string($access);
$data = mysql_real_escape_string($data);
return mysql_query("REPLACE INTO sess_en VALUES('$id', '$access', '$data')");}
function _destroy($id){
$id = mysql_real_escape_string($id);
return mysql_query("DELETE FROM sess_en WHERE id='$id'");}
function _clean($max){
$old = $_SERVER['REQUEST_TIME'] - $max;
$old = mysql_real_escape_string($old);
return mysql_query("DELETE FROM sess_en WHERE access<'$old'");}
session_start();
Any ideas on what could be causing this issue?
EDIT: I thought at first that it was just in my head, but I can now confirm this: this weird thing keeps appearing randomly: it usually does, but not sometimes (rarely, in fact) doesn't..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每次启动新会话的问题可能是由于您传递给
session_set_cookie_params()
的域参数所致。您已经通过了www.azgoth
,大概是因为您拥有多个顶级域 (TLD),并且您希望在所有顶级域之间共享 cookie。这是不允许的。根据您的设置,TLD 为azgoth
,这(当前)是不可能的,因此 cookie 将无效并且永远不会发送回服务器,因此每次都会启动一个新会话时间。持续存在的数据库问题可能与服务器配置有关。 PHP 手册在
mysql_pconnect()
的页面上指出:...和...
The new session starting every time problem is probably because of the domain parameter you have passed to
session_set_cookie_params()
. You have passedwww.azgoth
, presumably because you have more than one top-level domain (TLD) and you want the cookies to be shared across all of them. This is not allowed. With what you have set, the TLD isazgoth
, which is not (currently) possible, therefore the cookie will be invalid and will never be sent back to the server, ergo a new session will be started every time.The persistent DB problem is probably server configuration related. The PHP manual states, on the page for
mysql_pconnect()
:...and...