jeudi 5 mars 2020

if statement directing different users to different pages not working

I am trying to take different users to different pages based on their roleID, I had it working until I added in the sessions. Any he would be much appreciated as I am new to this .

<?php
        session_start(); // Starting Session
        $error=''; // Variable To Store Error Message
        if (isset($_POST['submit'])) {
            if (empty($_POST['username']) || empty($_POST['password'])) {
                $error = "Username or Password is invalid";
            }
            else
            {
                // Define $username and $password
                $username=$_POST['username'];
                $password=$_POST['password'];
                $table_role = $row['RoleID'];
                $_SESSION['login_user']=$username;
                // Establishing Connection with Server by passing server_name, user_id and password as a parameter
                $connection = mysql_connect("localhost", "root", "");
                // To protect MySQL injection for Security purpose
                $username = stripslashes($username);
                $password = stripslashes($password);
                $username = mysql_real_escape_string($username);
                $password = mysql_real_escape_string($password);
                // Selecting Database
                $db = mysql_select_db("horses", $connection);
                // SQL query to fetch information of registerd users and finds user match.
                $query = mysql_query("select * from users where password='$password' AND username='$username'", $connection);
                $rows = mysql_num_rows($query);
                if ($rows == 1) {
                    $_SESSION['login_user']=$username; // Initializing Session

                        if ($table_role == 1)
                            header ("location: CHomePage.php"); // takes the user to the Customer Homepage 
                        else if ($table_role == 2) 
                            header ("location: HomePage.php"); //tkes user to Staff Home Page   
                    } else {
                        $error = "Username or Password is invalid";
                    }
                    mysql_close($connection); // Closing Connection
                }
            }
        ?>

Aucun commentaire:

Enregistrer un commentaire