PHP会话转到用户特定页面

时间:2015-12-08 23:55:09

标签: php html session login

我正在尝试创建一个用户登录的页面,然后将其转到个性化页面。我遇到的问题是,当用户仍然登录时,如果他们输入通用URL,他们仍然登录并且他们的个性化页面是可见的。 (类似于当你登录Facebook并且如果你输入www.facebook.com它会直接进入你的饲料)我尝试使用会话,但我没有运气。

<?php 
if(isset($_REQUEST['user']) != true) {
?>
<html>
    <head>
            <title>Welcome</title>
    </head>
</html>
<body bgcolor="white">
<h1>Welcome</h1><br>


 If you have an existing account, log in here:<br>
    <form name="loginForm" action="test.php" method="get">
        User name: <input type="text" name="user" /><br>
        Password: <input type="password" name="pass" /></br>
        <input type="submit" value="Login" />
    </form>
    <br>
    <hr>
    <br>
    Otherwise, if you'd like to create an account, please fill out the following form:<br>
    <form name="createAccountForm" action="test.php" method="get">
        User name: <input type="text" name="user" /><br>
        Password: <input type="password" name="pass" /><br>
        First name: <input type="text" name="fname" /><br>
        Last name: <input type="text" name="lname" /><br>
        <input type="hidden" name="create" value="true">
        <input type="submit" value="Create Account" />
    </form>

</body>
</html>
<?php
}
else if(isset($_REQUEST['user']) == true) {
session_start();
if(!isset($_SESSION['uname']))
{
  header('location:test.php?redirect='.$_SERVER['REQUEST_URI']);
  exit;
}

// personalized page code
}

1 个答案:

答案 0 :(得分:1)

编辑::首先为您自己的现有代码提供解决方案。应该工作正常。

<?php 
session_start();
if(isset($_REQUEST['user'])) {

if(isset($_SESSION['uname']))
{
  header('location:test.php?redirect='.$_SERVER['REQUEST_URI']);
  exit;
}

// personalized page code
} else {

?>
<html>
    <head>
            <title>Welcome</title>
    </head>
</html>
<body bgcolor="white">
<h1>Welcome</h1><br>


 If you have an existing account, log in here:<br>
    <form name="loginForm" action="test.php" method="get">
        User name: <input type="text" name="user" /><br>
        Password: <input type="password" name="pass" /></br>
        <input type="submit" value="Login" />
    </form>
    <br>
    <hr>
    <br>
    Otherwise, if you'd like to create an account, please fill out the following form:<br>
    <form name="createAccountForm" action="test.php" method="get">
        User name: <input type="text" name="user" /><br>
        Password: <input type="password" name="pass" /><br>
        First name: <input type="text" name="fname" /><br>
        Last name: <input type="text" name="lname" /><br>
        <input type="hidden" name="create" value="true">
        <input type="submit" value="Create Account" />
    </form>

</body>
</html>
<?php
}
?>

这是我自己的登录解决方案(剥离了一些通用)它还包括数据库的PDO查询代码和使用php的password_hash函数检查密码。我将指出与您的问题特别相关的代码:

假设您正在构建登录页面,并希望将用户发送到与其状态相关的站点的其他部分。我认为整个脚本是相关的。您可以轻松地交换和更改会话变量值的结果。

    <?php
        error_reporting(E_ALL);
        ini_set("display_errors", 1);
        //start the session before sending any other output
        session_start();
        require('dbconn.php');
    // checks if a session eid has been set, if so, send them to the usercp.
        if(isset($_SESSION['eid'])){ header("Location: usercp.php"); } else {

        try{
        //build a login page
        $loginpage ="<html><head><title>Portal Login</title></head><body>";
         $loginpage.="<div align=\"center\" id=\"box\">";
         $loginpage.="<table><tr><td><img src=\"images/login.jpg\" /></td></tr>";
         $loginpage.="<tr><td><div align=\"center\">";
         $loginpage.="<font face=\"Courier New, Courier, monospace\">Please enter your email<br />  address and password.</font><br />";
         $loginpage.="<br /><form action=\"\" method=\"post\" name=\"login\" ><div align=\"right\">";
         $loginpage.="<font face=\"Courier New, Courier, monospace\">Email:</font><input type=\"text\" size=\"40\" name=\"email\" />";
         $loginpage.="<br /><br /><font face=\"Courier New, Courier, monospace\">Password:</font><input type=\"password\" size =\"40\" name=\"password\" />";
         $loginpage.="<br /></div><br /><input type=\"reset\" value=\"Reset\" />  ";
         $loginpage.="&nbsp;&nbsp;<input name=\"submit\" type=\"submit\" value=\"Login!\" />";
         $loginpage.="</form></div></td></tr></table></div></body></html>";  

        //checks if somebody is trying to login
        if(isset($_POST['submit'])) 
        //checks that the username and password have both been filled out if not, show the login page
            {   if(!$_POST['email'] || !$_POST['password'])
               {
                   echo $loginpage;
               echo "Please enter your login details";
               } else {  //otherwise search the database for the email address
                        $db = NEW pdo($dsn, $db_user, $db_pass);
                        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                        $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
                        $email = $_POST['email'];
                        $password = $_POST['password'];
                        $check = $db->prepare("SELECT * FROM employees WHERE email = :email");
                        $check->bindParam(":email", $email);
                        $check->execute();
                       //unset the session variables
                        unset($_SESSION['eid']);
                        unset($_SESSION['email']);
                        unset($_SESSION['userlevel']);
                        unset($_SESSION['fname']);
                       //check if the password hash matches php's hash of the password
                        if(($row = $check->fetch()) && (password_verify($password,$row['password']))) {
                       // set the session variables
                        $_SESSION['eid'] = $row['eid'];   
                        $_SESSION['email'] = $row['email'];
                        $_SESSION['userlevel'] = $row['userlevel'];
                        $_SESSION['fname'] = $row['fname'];
                        // if the user's userlevel is higher than 1 give them the option of the admin page
                        if($row['userlevel'] > "1")  { 
                            echo "<center><a href='usercp.php'><h1>User Panel</h1></a><br><br><a href='admin/admincp.php'><h1>Admin Panel</h1></a></center>";

                                } else {   //otherwise send them straight to the usercp
                                    header("Location: usercp.php");
                                }


                        } else {  //if the email is not found or password is incorrect, show the loginpage again
                                echo $loginpage;
                                            echo "Login details incorrect, please contact your manager.";

                        }

               }

            } else {  //if nobody has logged in already, or tried to log in just now, show the login page

                         echo $loginpage;

            }
        //pdo error reporting code
        } catch (PDOException $e) {
    throw $e;
}

        }
        ?>