如何创建基于用户类型重定向的登录页面

时间:2018-09-13 15:40:14

标签: php mysql

嗨,我有一个php mysql登录页面,一旦用户通过验证,该页面就可以正常工作(目前非常基本)。登录后,在登录页面顶部回显用户名和用户类型。

我现在很努力,要根据那里的用户类型将它们定向到不同的页面。

我添加了一个redirect.php,它根据usertyoe进行重定向。

到目前为止,这是我的文件。

    //config.php
    <?php
    ob_start();
    session_start();

    //timezone
    date_default_timezone_set('Asia/Bahrain');

    //database
    define('DBHOST','localhost');
    define('DBUSER','phpmyadmin');
    define('DBPASS','pupitadmin');
    define('DBNAME','user_register');

    // address
    define('DIR','https://mysite/');
    define('SITEEMAIL','myemail@gmail.com');

    try {

        //create PDO connection
        $db = new PDO("mysql:host=".DBHOST.";charset=utf8mb4;dbname=".DBNAME, DBUSER, DBPASS);
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

    } catch(PDOException $e) {
        //show error
        echo '<p class="bg-danger">'.$e->getMessage().'</p>';
        exit;
    }

    //include the user class, pass in the database connection
    include('classes/user.php');
    $user = new User($db);
    ?>



   //index.php

     <?php

    require_once('includes/config.php');


    //process login form if submitted
    if(isset($_POST['submit'])){

        if (!isset($_POST['username'])) $error[] = "Please fill out all fields";
        if (!isset($_POST['password'])) $error[] = "Please fill out all fields";

        $username = $_POST['username'];
        if ( $user->isValidUsername($username)){
            if (!isset($_POST['password'])){
                $error[] = 'A password must be entered';
            }
            $password = $_POST['password'];

            if($user->login($username,$password)){
                $_SESSION['username'] = $username;

    if($user->login($username,$password)){
        $_SESSION['username'] = $username;

        header('Location: redirect.php');
        exit;

    } else {
        $error[] = 'Wrong username or password.';
    }
}

}

    $title = 'Login';

    ?>

    <div class="container">

        <div class="row">

            <div class="col-xs-12 col-sm-8 col-md-6 col-sm-offset-2 col-md-offset-3">
                <form role="form" method="post" action="" autocomplete="off">
                    <h2>Login</h2>

                    <?php
                    //check for any errors
                    if(isset($error)){
                        foreach($error as $error){
                            echo '<p class="bg-danger">'.$error.'</p>';
                        }
                    }

                    ?>

                    <div class="form-group">
                        <input type="text" name="username" id="username" class="form-control input-lg" placeholder="User Name" value="<?php if(isset($error)){ echo htmlspecialchars($_POST['username'], ENT_QUOTES); } ?>" tabindex="1">
                    </div>

                    <div class="form-group">
                        <input type="password" name="password" id="password" class="form-control input-lg" placeholder="Password" tabindex="3">
                    </div>

                    <div class="row">
                        <div class="col-xs-6 col-md-6"><input type="submit" name="submit" value="Login" class="btn btn-primary btn-block btn-lg" tabindex="5"></div>
                    </div>
                </form>
            </div>
        </div>
    </div>


    //userpage.php
    <?php
    include('password.php');
    class User extends Password{

        private $_db;

        function __construct($db){
            parent::__construct();

            $this->_db = $db;
        }

        private function get_user_hash($username){

            try {
                $stmt = $this->_db->prepare('SELECT password, username, memberID, usertype FROM members WHERE username = :username AND active="Yes" ');
                $stmt->execute(array('username' => $username));

                return $stmt->fetch();

            } catch(PDOException $e) {
                echo '<p class="bg-danger">'.$e->getMessage().'</p>';
            }
        }

        public function isValidUsername($username){
            if (strlen($username) < 3) return false;
            if (strlen($username) > 17) return false;
            if (!ctype_alnum($username)) return false;
            return true;
        }

        public function login($username,$password){
            if (!$this->isValidUsername($username)) return false;
            if (strlen($password) < 3) return false;

            $row = $this->get_user_hash($username);

            if($this->password_verify($password,$row['password']) == 1){

                $_SESSION['loggedin'] = true;
                $_SESSION['username'] = $row['username'];
                $_SESSION['memberID'] = $row['memberID'];
                $_SESSION['usertype'] = $row['usertype'];
                return true;
            }
        }

        public function logout(){
            session_destroy();
        }

        public function is_logged_in(){
            if(isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true){
                return true;
            }
        }



    //adminpage
    <?php require('includes/config.php'); 

    $title = 'Admin Page';
    ?>

    <div class="container">

        <div class="row">

            <div class="col-xs-12 col-sm-8 col-md-6 col-sm-offset-2 col-md-offset-3">

                    <h1> <?php echo htmlspecialchars($_SESSION['username'], ENT_QUOTES); 
                    echo " - ";
                    echo htmlspecialchars($_SESSION['usertype'], ENT_QUOTES);
                    ?></h1>
                    <p><a href='logout.php'>Logout</a></p>
                    <hr>

            </div>
        </div>
    </div>


// redirect.php
<?php require('includes/config.php'); 


ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
$title = 'Admin Page';
if(isset($_SESSION['usertype'])=== 'user') {
             header('Location: userpage.php');
}
else {


            header('Location: adminpage.php');
            exit;
}

?>

{{1}}

1 个答案:

答案 0 :(得分:0)

您引用了$ usertype,但就代码显示而言,该点从未定义过

<script type="text/javascript">

    //declare global variables 
    var w = 600, h = 200, padding = 30, margin = 30;

    //import the .csv data
    d3.csv("WHO_familyplanningdata.csv", function(data) {
            console.log(data)});

    //create the SVG
    var svg = d3.select("body")
            .append("svg")
            .attr("width", w)
            .attr("height", h);

    //import the .csv data
    d3.csv("WHO_familyplanningdata.csv", function(data) {
            console.log(data); 

    //clean up data
    var percent = parseInt(data.percentage)

    //create rects, and add them to the SVG 
    svg.selectAll("rect")
            .data(data)
            .enter()
            .append("rect")
            .attr("x", function(d,i) {
                    return i * (w / data.length);
            }) //distributes them across the x axis 
            .attr("y", 0)
            .attr("width", (w / data.length) /2)
            .attr("height", function(d, i) { 
                    return percent;
            });

 }); //end of the data wrapping 
   </script>