PHP +会话第一次不登录 [英] PHP + Session doesn't login at first time
问题描述
我的登录系统只有两次登录才能登录.第一次,它不会(自动)登录.
我有一个简单的表单,其中包含用户名和密码,并带有action="login.php"
,您可能会认为它具有用于验证登录名的完整代码,等等.然后,如果一切正常,那么:
My login system only logs in if I do it 2 times. In the first time, it doesn't log in (automatically).
I have a simple form with username and password, with action="login.php"
, which as you might think has the whole code for verify login, etc..and then, in the end, if everything is ok:
Cookie::set('page-main-login-cookie', serialize($arr), time()+60);
header('location: ../index.php');
在页面顶部的index.php
(包含调用login.php
的表单)中:
In the index.php
(which contains the form that calls login.php
), in the top of the page:
ob_start(); session_start(); include('cookies.php'); include('sessions.php');
在</html>
标记之后:
if(Cookie::Exists('page-main-login-cookie')){
$data = unserialize(stripslashes($_COOKIE['page-main-login-cookie']));
if($data['status'] == 1){
Cookie::set('email', $data['email'], time() + (86400 * 7));
Session::set('email', $data['email']);
echo "<script type='text/javascript'> page_redirect(); </script>";
}else{
echo "<script type='text/javascript'> page_error(); </script>";
}
Cookie::Delete('page-main-login-cookie', time() - 3600);
}
在撰写本主题时,我想知道问题是否出在javascript中. page_redirect()
的代码如下:
And while I'm writing this topic I wonder if the problem might be in the javascript. The code for page_redirect()
is the following:
function page_redirect(){
alert("Done!");
setTimeout(function(){
location.reload();
}, 1500);
}
到目前为止我尝试了什么?
What have I tried so far?
- 将
header('location: ../index.php');
更改为header('location: http://www.mysite.com');
- 在
Session::set('email', $data['email']);
之后添加了
session_write_close();
- Changed
header('location: ../index.php');
toheader('location: http://www.mysite.com');
- Added
session_write_close();
afterSession::set('email', $data['email']);
他们都没有工作.
推荐答案
可能,因为您的登录代码位于HTML的之后,因此好像您未登录即使脚本确实在脚本末尾登录了您.
Probably because your log in code comes after your html, so it appears like you are not logged in even though the script does log you in at the end of the script.
通常,您希望将PHP和HTML尽可能分开(在不同文件中),并首先执行所有PHP业务逻辑,然后输出所有HTML.
Typically you would want to keep your PHP and HTML as separate as possible (in different files), and do all of your PHP business logic first, then output all the HTML.
这篇关于PHP +会话第一次不登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!