0
IM使用一个登录页面:当我更改页面时,如何让我的用户登录? PHP
<?php
session_start();
include_once 'dbconnect.php';
if(isset($_SESSION['user'])!="")
{
header("Location: home.php");
}
if(isset($_POST['btn-login']))
{
$email = mysql_real_escape_string($_POST['email']);
$upass = mysql_real_escape_string($_POST['pass']);
$res=mysql_query("SELECT * FROM users WHERE email='$email'");
$row=mysql_fetch_array($res);
if($row['password']==md5($upass))
{
$_SESSION['user'] = $row['user_id'];
header("Location: home.php");
}
else
{
?>
<script>alert('wrong details');</script>
<?php
}
}
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ورود</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<center>
<div id="login-form">
<form method="post">
<table align="center" width="30%" border="0">
<tr>
<td><input type="text" name="email" placeholder="Your Email" required /></td>
</tr>
<tr>
<td><input type="password" name="pass" placeholder="Your Password" required /></td>
</tr>
<tr>
<td><button type="submit" name="btn-login">ورود</button></td>
</tr>
<tr>
<td><a href="register.php">ثبت نام</a></td>
</tr>
</table>
</form>
</div>
</center>
</body>
</html>
,如果我输入正确的用户名和密码,我去欢迎页面,但登录只是在该网页IM,当我去到另一个网页IM像一个客人用户再次。
我该如何解决它,并保持登录在每一个地方?
在每个页面中用户'session_start()'! –
确保您已在其他页面的顶部添加* session_start(); *。如果您将通用函数(如db连接,session_start等)添加到单独的页面并将其包含在任何需要的地方,这将是一个好习惯。 –
你在哪里检查是否设置了$ _SESSION ['user']'并从db中加载用户?当你记录用户头部重定向时,显式调用'session_write_close'也不会伤害到它。 – Rufinus