重定向取决于UserID

时间:2013-03-25 08:26:14

标签: php mysql redirect

所以我有这个登录页面

<?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”等于5,我可以将它重定向到另一个页面。就像管理页面一样。

4 个答案:

答案 0 :(得分: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");
        }

答案 1 :(得分:0)

您需要将id存储在session中,然后只需检查session

if(isset($_SESSION['id'])
{
    //retrieve data of user 5 from database 
}
else
{
    //redirect to login page
}

答案 2 :(得分:0)

  1. 不要使用id来比较用户是管理员还是非管理员,在数据库中添加字段type,如果用户是admintype成为admin,如果不是user

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

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

        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)。

答案 3 :(得分:0)

if($_SESSION['id']==5)
    header("Location: yourpage.php");
else
    header("Location: private.php");
exit;