我正在尝试从类函数中设置会话变量的值,并稍后在同一类的不同函数中比较该会话值。这些函数都不在__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];
这是 Class 内的函数,应该每十秒进行一次比较(通过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
是迄今为止唯一不起作用的事情。