B. James B. James - 1 month ago 5
PHP Question

How can I setup PHP Redirects after Login?

New to PHP Here, trying to figure out a couple of things. When users log into the system, I would like for the PHP to look at the Group ID and then run through a system of checks to find a match. Once the Group ID is matched, it will then redirect users to the appropriate webpage. How would I go about doing this with this piece of code?

These are the columns in my table:

Name, Email, Password, Group


Thanks in advance for your help. Looking forward to learning something new!

<?php
$con=mysqli_connect("localhost", "username", "password", "dbName");
//"localhost", "userName" is username; "Password" is password; "dbName" is DB_Name;
if (mysqli_connect_errno($con))
{
echo "MySql Error: " . mysqli_connect_error();
}

$query=mysqli_query($con,"SELECT * FROM user_reg WHERE UserName='$_POST[UserName]' && Password='$_POST[Password]'");
$count=mysqli_num_rows($query);
$row=mysqli_fetch_array($query);

if ($count==1)
{
session_start();
$_SESSION['UserName'] = $_POST['UserName'];
$_SESSION['Password'] = $_POST['Password'];
header("location: redirect.html");
}
else
{
echo "Invalid username or password";
}

mysqli_close($con);
?>

Answer

Purely as an example use this:

<?php
    /*
        GroupID 1 = Standard User
        GroupID 2 = Special User
        GroupID 3 = Power User

    */

    $GroupID = $_POST['GroupID']; // Assume 1 


    switch($GroupID){
        case 1: 
        // If GroupID = 1 Redirect to correct page
            header("Location: StandardPage.html");
        case 2:
        // If GroupID = 2 Redirect to correct page
            header("Location: SpecialUser.html");
        case 3:
        // If GroupID = 3 Redirect to correct page
            header("Location: Poweruser.html");
        default:
        // If GroupID does not match set user groups redirect to error page
            header("Location: Error.php");
    }


?>

Assume this as a cleaner if statement. Please Consult the manual and learn how to adapt this to your exact needs

as the lack of information in your question, I will not be more precise. This is a step in the right direction, but all this is of guess work. Good Luck

Comments