AJAX脚本停止记住会话(AJAX script stopped remembering session)

编程入门 行业动态 更新时间:2024-10-25 02:20:17
AJAX脚本停止记住会话(AJAX script stopped remembering session)

我回来的时候我发布了这个问题 。 我用AJAX脚本更新了这个问题,该脚本工作了大约1周。 基本上我可以使用AJAX脚本中的session_start() ,然后我可以访问我需要的会话变​​量。

这真的很奇怪,但我在周末之后来了,今天早上这个剧本不再适用了。 这很简单,这里:

<?php session_start(); $ajax_connection = mysql_connect('10.X.X.X',$_SESSION['username'],$_SESSION['password']); $result_set = array(); while ($result_set[] = mysql_fetch_assoc($results)){ // do nothing } echo json_encode($results); ?>

上周这个工作完美无缺,现在在我的错误日志中,我得到了Undefined index: username和Undefined index: password warnings。 当然,MySQL连接尚未建立。 因此,此脚本未与原始连接运行相同的会话。 我使用error_log(session_id())来检查父页面和AJAX脚本的ID,确定它们是不同的。 当我重新加载页面并再次尝试时,页面和AJAX脚本的ID分别保持不变,但它们应该是相同的ID,而不是2个不同的ID。

无论如何,在经过一周多的工作之后,有没有人知道为什么这不再适用了?

I little while back I posted this question. I have updated that question with the AJAX script that worked for about 1 week. Basically I can use session_start() from the AJAX script, and then I could access the session variables that I needed.

It is really strange, but I came in after the weekend, and this morning this script does not work anymore. It is very simple, here:

<?php session_start(); $ajax_connection = mysql_connect('10.X.X.X',$_SESSION['username'],$_SESSION['password']); $result_set = array(); while ($result_set[] = mysql_fetch_assoc($results)){ // do nothing } echo json_encode($results); ?>

Last week this worked flawlessly, and now in my error log i get the Undefined index: username and Undefined index: password warnings. And of course the MySQL connection is not established. So this script is not running the same session as the original connection. I used error_log(session_id()) to check the IDs of the parent page and the AJAX script, and sure enough they are different. When i reload the page and try every again, the IDs stay the same for the page and the AJAX script respectively, but they should be the same ID, not 2 different ones.

Anyway, does anyone have any idea why this wouldn't be working anymore, after working well for over a week?

最满意答案

这是你可以使用的类。 它是单身的,以确保你只将它实例化一次。 用它来实现它

$db = Db::getInstance();

然后

$db->connect();

这是一种使用Dbase连接的更安全的方法(注意我使用了PDO,但是如果你真的需要继续使用mysql_函数,你仍然可以修改它)。

class Db { private static $instance = null; private $db = null; private $host = ''; private $name = ''; private $username = ''; private $password = ''; private function __construct() { $this->host = 'yourHost'; $this->name = 'yourDbName'; $this->username = 'yourUserName'; $this->password = 'youPassword'; } public static function getInstance() { if(is_null(self::$instance)) { self::$instance = new Db(); } return self::$instance; } public function connect() { try { $this->db = new PDO('mysql:host=' . $this->host . ';dbname=' . $this->name, $this->username, $this->password); } catch(Exception $e) { throw new Exception('Connection error: either the database is unavailable or connection infos are not valid. Please contact the webmaster.'); } } }

Here's the kind of Class you can use. It is in singleton to make sure you instanciate it only once. Instanciate it with

$db = Db::getInstance();

then

$db->connect();

this is a much safer way to use Dbase connections (Note that I used PDO, but if you really need to keep using mysql_ functions, you can still modify it).

class Db { private static $instance = null; private $db = null; private $host = ''; private $name = ''; private $username = ''; private $password = ''; private function __construct() { $this->host = 'yourHost'; $this->name = 'yourDbName'; $this->username = 'yourUserName'; $this->password = 'youPassword'; } public static function getInstance() { if(is_null(self::$instance)) { self::$instance = new Db(); } return self::$instance; } public function connect() { try { $this->db = new PDO('mysql:host=' . $this->host . ';dbname=' . $this->name, $this->username, $this->password); } catch(Exception $e) { throw new Exception('Connection error: either the database is unavailable or connection infos are not valid. Please contact the webmaster.'); } } }

更多推荐

本文发布于:2023-08-07 10:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1463810.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   script   AJAX   session   remembering

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!