我正在尝试从一个类函数中设置会话变量的值,并在稍后在同一个类的不同函数中比较该会话值。这些函数都不在__construct()函数中。
问题似乎在于,这种比较永远不会返回true。
<?php
session_start();
$class = new Class();
?>
blah blah blah
<script type="text/javascript">
// Ajax function to call PHP that creates new Class isntance and executes a Class->function();
setInterval('checkNew()', 10000);
</script>
blah blah blah
// Another Ajax Function that calls creates a new Class isntance and executes the initial Class->function();
<body onload="getMessages();">
blah blah blah下一个函数摘录是从<body onload=""> ajax调用的脚本运行的。这是为了稍后在不同的函数中比较这个$_SESSION['last_sms']。
// Check the now() stamp of the most recent message loaded
$recent_msg_query = "SELECT date_received FROM messages ORDER BY date_received DESC LIMIT 1";
$recent_statement = $this->db_handle->prepare($recent_msg_query);
$recent_statement->execute();
$most_recent = $recent_statement->fetch(PDO::FETCH_NUM);
$_SESSION['last_sms'] = $most_recent[0];这是类内部的函数,它应该每隔十秒(通过setInterval('checkNew()', 10000);)进行一次比较
public function checkNew()
{
// Check the now() stamp of the most recent message loaded and compares it
// to a stored now() stamp.
$recent_msg_query = "SELECT date_received FROM messages ORDER BY date_received DESC LIMIT 1";
$recent_statement = $this->db_handle->prepare($recent_msg_query);
$recent_statement->execute();
$most_recent = $recent_statement->fetch(PDO::FETCH_NUM);
if (!isset($_SESSION['last_sms'])) {
$_SESSION['last_sms'] = $most_recent[0];
}
if ($_SESSION['last_sms'] !== $most_recent[0]) {
echo "New message(s) available, refresh.";
} else {
echo "No new messages yet.";
}
}当我发送新消息时,页面仍然显示“尚无新消息”。你们能帮我弄清楚这条路走到哪了吗?到目前为止,这个$_SESSION stuff是唯一不能工作的东西。
发布于 2012-10-10 04:15:52
session_start()不仅仅充当会话发起者。它必须在每个试图读取或写入$_SESSION的脚本上调用。由于AJAX处理程序脚本与生成调用它们的页面的主PHP脚本分离,并且它们作为完全独立的PHP脚本运行,因此还必须在AJAX处理PHP脚本上调用session_start()。
https://stackoverflow.com/questions/12807608
复制相似问题