So I'm trying to get a simple session based login working, and my sessions aren't being saved at all. I've tried many things, but nothing works! Please help!

所以我试图让一个简单的基于会话的登录工作,我的会话根本没有保存。我尝试了很多东西,但没有任何作用!请帮忙!

Edit: added start_session, but it is still empty

编辑:添加了start_session,但它仍然是空的

<?php 
 session_start();
 ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

...

...

$row = mysql_fetch_row($sql_result);

                            $_SESSION['id']= $row[0];
                            $_SESSION['uname'] = $row[1];
                            $_SESSION['pass'] = $row[2];
                            session_write_close(); <-- now removed, but the session is still empty


                            echo  $row[1] . "Login accepted.<br />";

This is the code to create the session. all the rows are accounted for, and the code is being called, but the session still turns out empty.

这是创建会话的代码。考虑了所有行,并且正在调用代码,但会话仍然是空的。

if(empty($_SESSION['uname'])) echo 'empty session';

Also, to note that firebug says 0 objects stored in sessionstorage.

另外,要注意firebug说会话存储中存储了0个对象。

4 个解决方案

#1


2

Remove session_write_close()

session_write_close() ends the session:

session_write_close()结束会话:

End the current session and store session data.

结束当前会话并存储会话数据。

Any subsequent calls to $_SESSION will not return any data as the session has been written and then closed.

对会话编写然后关闭后,对$ _SESSION的任何后续调用都不会返回任何数据。

Generally you would not call session_write_close() until just before you perform a header('Location: http://example.org') redirect. As this is not the case in your code you should remove it.

通常,在执行标题(“Location:http://example.org”)重定向之前,您不会调用session_write_close()。由于您的代码不是这种情况,因此您应将其删除。

Ensure you are calling session_start()

I am assuming that you are calling session_start() at the top of all your scripts.

我假设您在所有脚本的顶部调用session_start()。

Test your session

To test that your session is working then I suggest implementing the following test script:

要测试您的会话是否正常工作,我建议您实施以下测试脚本:

<?php
session_start();
$_SESSION['test'] = 'Hello';
echo $_SESSION['test'] . ' World!';
// you should get: Hello World!

This way you know that there is definitely something in your session array. In your sample code your are getting your data from a database, which might be returning empty values.

通过这种方式,您知道会话数组中肯定存在某些内容。在您的示例代码中,您将从数据库中获取数据,这可能会返回空值。

PHP session and HTML5 sessionstorage confusion

They are not one and the same thing. PHP's sessions are stored on the server whereas the browser stores HTML5 sessionstorage locally in the browser. The HTML5 sessionstorage is a client side store of information that only lasts for that session. See the w3c specification for more information.

它们不是同一个东西。 PHP的会话存储在服务器上,而浏览器在浏览器中本地存储HTML5会话存储。 HTML5会话存储是仅持续该会话的客户端信息存储。有关更多信息,请参阅w3c规范。

PHP's $_SESSION does not get stored or affect data in the HTML5 sessionstorage.

PHP的$ _SESSION不会存储或影响HTML5会话存储中的数据。

更多相关文章

  1. php二维码/26行代码制作php二维码phpqrcode加grafika水印
  2. php写app接口并返回json数据
  3. MySQL错误1046:未选择数据库
  4. php分页代码的问题,显示了两个当前页码,求大神解答
  5. 社交登录实现的数据库结构?
  6. PHP开发微信支付代码及支付通知处理
  7. Json调用JSON.parse:意外结束数据
  8. Symfony2 -从数据库中提取数据并以表单形式显示
  9. PHP接收到的$_POST数据里的邮箱符号@变成了%40

随机推荐

  1. 基于HTML5的iPad电子杂志横竖屏自适应方
  2. Delphi下发送Email的方法
  3. jQuery验证没有错误计数
  4. (转)让所有浏览器支持HTML5 video视频标签
  5. 不会出现Javascript context.closePath()
  6. 宽度切换jQuery中的Animate在FireFox中不
  7. JQuery从动态内容加载动态内容
  8. HTML5移动开发基础
  9. Jquery实现table行数的增加,删除,实现指定
  10. 使用phonegap包装html5网页为iOS app