0

所以我有这个登录页面

<?php
    require("common.php"); 
    $submitted_username = '';
    if(!empty($_POST))
    {
        $query = "
            SELECT
                id,
                username,
                password,
                salt,
                email
            FROM users
            WHERE
                username = :username
        ";
        $query_params = array(
            ':username' => $_POST['username']
        );

        try
        {
            $stmt = $db->prepare($query);
            $result = $stmt->execute($query_params);
        }
        catch(PDOException $ex)
        {
            die("Failed to run query: " . $ex->getMessage());
        }
        $login_ok = false;
        $row = $stmt->fetch();
        if($row)
        {
            $check_password = hash('sha256', $_POST['password'] . $row['salt']);
            for($round = 0; $round < 65536; $round++)
            {
                $check_password = hash('sha256', $check_password . $row['salt']);
            }

            if($check_password === $row['password'])
            {
                $login_ok = true;
            }
        }
        if($login_ok)
        {
            unset($row['salt']);
            unset($row['password']);
            $_SESSION['user'] = $row;
            header("Location: private.php");
            die("Redirecting to: private.php");
        }
        else
        {
            print("Login Failed.");
            $submitted_username = htmlentities($_POST['username'], ENT_QUOTES, 'UTF-8');
        }
    }

?>
<h1>Login</h1>
<form action="login.php" method="post">
    Username:<br />
    <input type="text" name="username" value="<?php echo $submitted_username; ?>" />
    <br /><br />
    Password:<br />
    <input type="password" name="password" value="" />
    <br /><br />
    <input type="submit" value="Login" />
</form>  

会话(common.php)

<?php

    // These variables define the connection information for your MySQL database
    $username = "root";
    $password = "";
    $host = "localhost";
    $dbname = "phplogin";


    $options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
    try
    {
        $db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options);
    }
    catch(PDOException $ex)
    {
        die("Failed to connect to the database: " . $ex->getMessage());
    }
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

    if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc())
    {
        function undo_magic_quotes_gpc(&$array)
        {
            foreach($array as &$value)
            {
                if(is_array($value))
                {
                    undo_magic_quotes_gpc($value);
                }
                else
                {
                    $value = stripslashes($value);
                }
            }
        }

        undo_magic_quotes_gpc($_POST);
        undo_magic_quotes_gpc($_GET);
        undo_magic_quotes_gpc($_COOKIE);
    }

    header('Content-Type: text/html; charset=utf-8');
    session_start();
?>  

我想要做的是,当登录用户的“id”为“5”时,它将被重定向到与代码中所述的 private.php 不同的另一个页面。如果它发现“id”等于五,我可以将它重定向到另一个页面吗?它有点像管理页面。

4

4 回答 4

1

是的,你可以做到。在你写的地方试试这个

 if($login_ok)
        {
            unset($row['salt']);
            unset($row['password']);
            $_SESSION['user'] = $row;
            if($row['id']==5)
             header("Location: yourpage.php");
            else
            header("Location: private.php");
        }
于 2013-03-25T08:31:49.170 回答
0
  1. 不要比较用户是管理员还是非管理员 using id,在你的数据库中添加一个字段say type,如果用户是adminlet typebe admin,如果不是 let it beuser

  2. 用户登录后,检查该type特定用户的数据库中的字段并根据该字段进行重定向。

考虑到这是重定向的代码:

    if($login_ok)
    {
        unset($row['salt']);
        unset($row['password']);
        $_SESSION['user'] = $row;
        if($row['type'] == "admin"){
           header("Location: private.php");
           die("Redirecting to: private.php");
        }
        else{
            header("Location: otherpage.php");
            die("Redirecting to: private.php");
        }

    }

优势 :

  1. 如果您删除用户或添加新用户,它不会影响您的编码部分。
  2. 您可以有多个管理员,但您仍然只需要检查一个条件(无需检查每个用户 ID)。
于 2013-03-25T08:33:28.347 回答
0
if($_SESSION['id']==5)
    header("Location: yourpage.php");
else
    header("Location: private.php");
exit;
于 2013-03-25T08:36:03.383 回答
0

你需要存储id然后session总是检查session

if(isset($_SESSION['id'])
{
    //retrieve data of user 5 from database 
}
else
{
    //redirect to login page
}
于 2013-03-25T08:31:14.420 回答