在服务器上而不是在客户端上设置和获取 PHP Session
我在很大程度上依赖会话来获取不同计算机的实时更新时遇到问题。场景如下:
[计算机 A] 管理页面: 如果单击按钮,某些文本将显示在站点的每个页面上,无论谁的计算机访问它。
[计算机B]客户端页面: 我必须实时查看页面上的文本,而无需在管理员单击按钮时加载它。
这是我的代码:
管理页面:admin.php
$("#newFight").click(function(){
var newFight = "newFight="+1;
$.ajax({
type: "post",
url: "../includes/newfight.php",
data: newFight,
cache: false,
success: function(html){
if(html == 0){
alert("Open a Game First");
}
else if(html == 1){
$("#newFight").attr('disabled', 'disabled');
// Real time update of fight sequence
$("td#fightId").load('../includes/getfight_id.php');
$("td#fightHead").html("<em>Fight is on.</em>");
}
}
});
});
管理页面:newfight.php
session_start();
require_once('dbconn.php');
if(isset($_POST['newFight'])){
if($_POST['newFight'] == $_SESSION['gameActive']){
// Make a new fight
echo $msg = 1;
$query = "INSERT INTO fights(fight_game_id) VALUES(".$_SESSION['game_id'].")";
$execQuery = mysql_query($query);
$last_id = mysql_insert_id();
$_SESSION['fight_id'] = $last_id;
$_SESSION['fight_active'] = 1;
}
if($_POST['newFight'] != $_SESSION['gameActive']){
$msg = 0;
echo $msg;
}
}
?>
这里的过程是这样的,如果单击一个按钮,jQuery通过AJAX将处理它并设置会话 ex: $_SESSION['button_clicked'] = 1;供用户参考,因此用户的页面可以使用setInterval(client's)实时更新。
这是客户端页面,每当管理员单击按钮时,该页面就会获得实时更新。
客户端页面:client.php
$(function(){
var auto_refresh = setInterval(
function (){
$("td#fightHeadUser").load('../includes/fightheaduser.php');
}, 1000);
});
客户端页面:fightheaduser.php
session_start();
if(isset($_SESSION['fight_active']) && $_SESSION['fight_active'] == 1){
echo "<em>Fight is on.</em>";
}
if(!isset($_SESSION['fight_active']) && $_SESSION['fight_active'] != 1){
echo "<em>No fight yet.</em>";
}
所以我的问题是..它仅在一台计算机上运行,所以当我打开两个浏览器(管理员和客户端)时,然后单击按钮,我可以看到客户端的页面实时更新,但是,当另一台计算机通过 LAN 访问客户端的页面时,它对它们不起作用。所以我怀疑这是因为会话仅存储在我的电脑上,这就是它无法在其他电脑上运行的原因。
你认为我必须做什么?如果您可以建议我一个好的但不同的解决方案,那么我很乐意尝试。
抱歉我的问题很长。 TIA。
I'm having a problem on depending much on sessions to get a real time update to different computers. So here's the scenario:
[Computer A] Admin page:
If button is clicked, certain text will display on every page of the site, regardless of whose computer's accessing it.
[Computer B] Client page:
I must see the text on my page real time, without loading it whenever the admin clicks the button.
Here's my code:
Admin page: admin.php
$("#newFight").click(function(){
var newFight = "newFight="+1;
$.ajax({
type: "post",
url: "../includes/newfight.php",
data: newFight,
cache: false,
success: function(html){
if(html == 0){
alert("Open a Game First");
}
else if(html == 1){
$("#newFight").attr('disabled', 'disabled');
// Real time update of fight sequence
$("td#fightId").load('../includes/getfight_id.php');
$("td#fightHead").html("<em>Fight is on.</em>");
}
}
});
});
Admin page: newfight.php
session_start();
require_once('dbconn.php');
if(isset($_POST['newFight'])){
if($_POST['newFight'] == $_SESSION['gameActive']){
// Make a new fight
echo $msg = 1;
$query = "INSERT INTO fights(fight_game_id) VALUES(".$_SESSION['game_id'].")";
$execQuery = mysql_query($query);
$last_id = mysql_insert_id();
$_SESSION['fight_id'] = $last_id;
$_SESSION['fight_active'] = 1;
}
if($_POST['newFight'] != $_SESSION['gameActive']){
$msg = 0;
echo $msg;
}
}
?>
The process here is like this, if a button is clicked, jQuery through AJAX will process it and sets a session ex: $_SESSION['button_clicked'] = 1; for user's reference, so the user's page can be updated real time using setInterval(client's).
And here's the client page that's suppose to get the real time update whenever the admin click the button.
Client page: client.php
$(function(){
var auto_refresh = setInterval(
function (){
$("td#fightHeadUser").load('../includes/fightheaduser.php');
}, 1000);
});
Client page: fightheaduser.php
session_start();
if(isset($_SESSION['fight_active']) && $_SESSION['fight_active'] == 1){
echo "<em>Fight is on.</em>";
}
if(!isset($_SESSION['fight_active']) && $_SESSION['fight_active'] != 1){
echo "<em>No fight yet.</em>";
}
So my problem is.. it runs only on one computer, so when I open two browsers(admin and client), and clicked the button, I can see that the client's page are updated real time, BUT, when a different computer access the client's page via LAN, it's not working on them. So I suspect that it is because sessions are only stored on my pc that's why it's not woking on other pc.
What do you think I have to do? If you can suggest me a good but different solution, then I would love to try it.
Sorry for my very long question.
TIA.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您无法使用会话来解决此问题,因为每个用户(连接)都会获得另一个会话,并且无法从管理会话中获取数据。
您可以使用环境变量。每个连接都可以读取这个。
I think you can not work with sessions to solve this problem as every user (connection) gets another session and can not get the data from the admin session.
you can use environment variables. every connection can read this one.
我假设客户想观看或打架时必须登录?为什么不使用数据库?所以你可以注册哪个正在播放,你可以观看历史记录等等。
I assume a client has to be login when he want to watch or has a fight? Why not using a db? So u can register which is on, and you can watch history etc. etc.