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

共通のヘッダー ファイルを 1 つ作成し、session_start(); のようなものを配置する必要があると思います。およびob_flush(); そのファイルに追加し、すべてのページに含めます。そのため、どこからでもアクセスでき、コードを書き直す必要はありません。同じコードを書き直すのは悪癖

于 2015-07-18T21:15:45.520 に答える