7

目前我正在为一个虚拟登录/注册页面做一个小项目,现在我想添加一个只有在你登录时才能访问的页面。所以问题是我如何创建会话或 cookie 并检索它们? 以及如何阻止未登录的用户。

我目前正在为 login.php 和 member_area.php 使用这些代码: Login.php:

<?php
    session_start();





    if(isSet($_POST['login'])) {
        include('db.php');

        $username = mysql_real_escape_string($_POST['username']);
        $password = sha1($_POST['password'] );

        $query = mysql_query("SELECT * FROM tab WHERE username='".addSlashes($username)."' AND password='".addSlashes($password)."'");
        $res = mysql_num_rows($query);

        if ($res == 1) {
            $_SESSION['username'] = $username;
            $_SESSION['password'] = $password;
            $_SESSION['userobj'] = mysql_fetch_assoc($query);

            header('Location: http://localhost/member_area.php');
            exit;
        } else {
            echo 'Data does not match <br /> RE-Enter Username and Password';
        }
    } else {

?>
    <html>
    <head><link rel="stylesheet" type="text/css" href="css.css"></head>
        <body>
                <div id="div1">

                <a href="index.php" id="home">Home</a>
                <a href="Login.php" id="login2">Login</a>
                <a href="register.php" id="register">Register</a> 


        </div>
            <table width="200" border="0" cellspacing="1" align="center">
                <form id="form1" method="post" action="login.php">
                <tr>
                    <td colspan="2"><h2>Members login</h2></td>
                </tr>
                <tr>
                    <td>Username: </td>
                    <td>
                        <input type="text" name="username" id="username"/>
                    </td>
                </tr>
                <tr>
                    <td>Password: </td>
                        <td><input type="password" name="password" id="password"/> </td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <input type="submit" name="login" id="login" value="login" />
                    </td>
                </tr>
                </form>
            </table>
            </body>
    </html>
    <?php
    }


?>

Member_area.php:

<?php

?>
<html>
<head><link rel="stylesheet" type="text/css" href="css.css"></head>
    <body>
            <div id="div1">

                <a href="index.php" id="home">Home</a>
                <a href="Login.php" id="login2">Login</a>
                <a href="register.php" id="register">Register</a> 


        </div>
        <form action="/Log_out.php" method="get">
            <input type="submit" name="submit" value="Log Out." action="http://localhost/Log_out.php" id="Logout">
        </form>
    </body>
</html>
<?php

?>

请注意,我对 PHP 完全陌生,所以如果可能的话,一些方向将代码放在哪里,并提供一些解释。

4

2 回答 2

17

在 Member_area.php 的顶部添加:

session_start();
if(!isset($_SESSION['username'])){
   header("Location:Login.php");
}

它检查会话是否设置,如果没有,它将用户重定向到登录页面。

于 2013-02-26T11:57:15.283 回答
1
 <?php
    if(!isset($_SESSION['username'])) {
    die("Please login");
}

?>
<html>
<head><link rel="stylesheet" type="text/css" href="css.css"></head>
    <body>
            <div id="div1">

                <a href="index.php" id="home">Home</a>
                <a href="Login.php" id="login2">Login</a>
                <a href="register.php" id="register">Register</a> 


        </div>
        <form action="/Log_out.php" method="get">
            <input type="submit" name="submit" value="Log Out." action="http://localhost/Log_out.php" id="Logout">
        </form>
    </body>
</html>
<?php

?>

应该是这样的:)

于 2013-02-26T11:58:13.440 回答