PHP 会话类 [英] PHP Session Class

查看:41
本文介绍了PHP 会话类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照 wikihow 教程构建了一个安全的会话管理系统:http://www.wikihow.com/Create-a-Secure-Session-Managment-System-in-PHP-and-MySQL

I followed the wikihow tutorial for building a secure session management system : http://www.wikihow.com/Create-a-Secure-Session-Managment-System-in-PHP-and-MySQL

它工作正常.

现在,我遇到了一个问题,当 2 个连续的 ajax 被这样调用时,会话 id cookie _s 会发生变化(和登录信息):

Now, I have a problem that session id cookie _s change (and log in information) when 2 successive ajax are called like this :

<input type="button" value="go" id="mybutton" />
<script>
        $("#mybutton").click( function() {
            $.get("ajax1.php");
            $.get("ajax2.php");
        });

</script>

ajax1.php 和 ajax2.php 都只需要会话类文件

where both ajax1.php and ajax2.php have just require the session class file

<?php
require('session.class.php');
$session = new session();
$session->start_session('_s', false);
?>

单击按钮后,_s cookie 中存储的会话 ID 将更改为新的.我像这样在两个ajax之间添加了一个警报

upon clicking the button, the session id stored in _s cookie changes to a new one. I added an alert between the two ajax like this

<input type="button" value="go" id="mybutton" />
<script>
        $("#mybutton").click( function() {
            $.get("ajax1.php");
            alert("anything");
            $.get("ajax2.php");
        });

</script>

通过分离两个 ajax 调用,会话 ID 没有改变.

by separating the two ajax calls, the session id didn't change.

实际代码不是这样的,而是很多单独的按钮都有ajax,但是碰巧用户在响应之前连续点击按钮.

the actual code isn't like this ,, but rather many separate buttons each have it's ajax,, but it happens that a user clicks buttons successively before response.

<input type="button" value="go" id="mybutton" />
<script>
        $("#mybutton").click( function() {
            $.get("ajax1.php");
        });

</script>
<input type="button" value="go" id="mybutton2" />
<script>
        $("#mybutton2").click( function() {
            $.get("ajax2.php");
        });

</script>

有什么想法吗??

推荐答案

当您进行第一次 AJAX 调用时,服务器会启动一个会话并将会话 ID 在 cookie 中发送回浏览器.为了让第二个 AJAX 调用在同一个会话中,它必须将该 cookie 发送回服务器.

When you make the first AJAX call, the server starts a session and sends the session ID back to the browser in a cookie. In order for the second AJAX call to be in the same session, it has to send that cookie back to the server.

但是您不会在发送第二个电话之前等待对第一个电话的响应.所以cookie没有收到,第二次调用也不能发送.任何依赖于 AJAX 调用结果的操作都必须在其回调函数中完成.所以你应该这样做:

But you're not waiting for the response to the first call before you send the second call. So the cookie hasn't been received, and it can't be sent with the second call. Anything that depends on the result of an AJAX call has to be done in its callback function. So you should do:

$.get('ajax1.php', function() {
    $.get('ajax2.php');
});

这篇关于PHP 会话类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆