0

嗨,我想让一个页面只对登录用户开放。我搜索了这个,但我发现的所有结果都没有帮助所以我需要知道我应该在哪里添加代码。这是我的 login.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
    }

}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<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>

我需要放在session_start();我拥有的每一页吗?如果不是,我应该把它放在哪里?我应该放在哪里:

session_start();
if(!isset($_SESSION['id'])) {
    header("location: index.php"); 
    die();
}

tanq 如果我需要为你们添加更多细节,请告诉我。

4

1 回答 1

0

我认为您必须制作一个通用头文件并放置诸如 session_start(); 之类的内容。和 ob_flush(); 在该文件中并将其包含在所有页面中。因此您可以在任何地方访问它,而无需重写代码。重写相同的代码是坏习惯

于 2015-07-18T21:15:45.520 回答