简体   繁体   English

会话注册不起作用

[英]Session register doesn't work

I'm trying to make a login page. 我正在尝试制作一个登录页面。 When I try to register the username as a session it should redirect me to another page, where it checks if that session is not registered. 当我尝试将用户名注册为会话时,应将我重定向到另一个页面,在该页面中检查该会话是否未注册。 if it isn't, it redirects me back to the login page. 如果不是,它会将我重定向回登录页面。 well, i think it fails on the second page. 好吧,我认为它在第二页上失败了。

Login.php: login.php中:

if($count==1){$_SESSION['user'] = $username;$_SESSION['pass'] = $password;header("location:Login_Success.php");}
else{echo "<p style='color:red'>Wrong username or password!</p>";}

login_success.php: login_success.php:

session_start();
if(!isset($_SESSION['username'], $_SESSION['password'])){
header("location:login.php");}

The error: IT DOES NOT REGISTER THE SESSION! 错误:它不注册会议! I know that because I replaced the header("location:url"); 我知道,因为我替换了header("location:url"); with an echo "What the--?!" echo "What the--?!" , and it displayed "What the--?!". ,并显示“ What the-- ?!”。

THE ANSWER 答案

The session's value wasn't right. 会话的值不正确。 Here's the new code: 这是新的代码:

Login.php: login.php中:

if($count==1){$_SESSION['user'] = "username" //Here was the problem
or die(mysql_error());header("location: Login_Success.php");}
else{echo "<p style='color:red;margin-left:150px;'>Wrong username or password!</p>";}

Login_Success.php: Login_Success.php:

session_start();
if($_SESSION['user']!="username")//"username" is here too
{header("location: login.php");}

add

session_start();

at the begining of your pages. 在页面的开头。 even before doctype or any html code. 甚至在doctype或任何html代码之前。
in fact before any printable code. 实际上在任何可打印代码之前。

You are missing session_start(); 您缺少session_start(); on the begining of your code. 在代码的开头。

session_start(); //This will get you connected with your previous session

if($count==1){$_SESSION['user'] = $username;$_SESSION['pass'] = $password;header("location:Login_Success.php");}
else{echo "<p style='color:red'>Wrong username or password!</p>";}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM