如何使用login use 2文件设置会话?主页>登录>首页
我的代码中有什么问题吗?
感谢。
home.php
<?php
session_start();
?>
<!DOCTYPE html>
<html><head><title></title></head>
<body>
<?php
if($_SESSION['account']){
print"login successful";
//do something
}
else{
print"login invalid";
print"
<form method=\"post\" action=\"login.php\">
account: <input type=\"text\" name=\"account\"><br>
password: <input type=\"text\" name=\"password\"><br>
<input type=\"submit\" value=\"login\">
</form>
";
}
?>
</body>
</html>
的login.php
$account = mysql_escape_string($_POST['account']);
$password = mysql_real_escape_string($_POST['password']);
if($account == 'myaccount' && $password == 'mypassword'){
session_start();
$_SESSION['account'] = $account;
$_SESSION['password'];
print"<meta http-equiv=\"refresh\" content=\"0;url=home.php\">";
header("location: home.php");
exit();
}
else{
print"<meta http-equiv=\"refresh\" content=\"0;url=home.php\">";
header("location: home.php");
exit();
}
答案 0 :(得分:1)
您没有在$_SESSION
阵列上存储任何数据。
尝试:
$_SESSION['account'] = true; // Or something else that evaluates to true and is relevant.
$_SESSION['password'] = 'please, anything but your password plaintext. think of the children';
这样,当您在home.php上测试登录操作的结果时:
if($_SESSION['account']){
print"login successful";
//do something
}
你可以满足条件。
答案 1 :(得分:1)
根据您的脚本:
$_SESSION['account'];
$_SESSION['password'];
应该是这样的:
$_SESSION['account'] = $account;
$_SESSION['password'] = $password;
但请注意,请勿在生产网站中使用此功能。这不会对用户进行身份验证。
您无法使用会话作为记录某个人并让他们登录的唯一方法。会话很容易被劫持。一个好的用户认证系统将:
加密密码
将用户信息存储在数据库中
收集会话信息并进行设置
现在,为了让用户登录,每次访问新页面时,您都将使用seesion id,passsword,用户ip和会话变量来验证用户身份并允许他们查看页面。基本上你会创建一个函数或类来以安全的方式处理它。
现在对于那些刚开始学习php的人来说,已经有很多很棒的登录脚本了。任何对其用户进行身份验证的好站点都以一个非常好的用户身份验证系统开始,然后在其中构建一个站点。
答案 2 :(得分:0)
更改行:
if($_SESSION['account']){
print"login successful";
}
通过
if(isset($_SESSION['account']) && $_SESSION['account'] == true){
print"login successful";
}