B. James B. James - 1 year ago 62
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!

$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]'");

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


Answer Source

Purely as an example use this:

        GroupID 1 = Standard User
        GroupID 2 = Special User
        GroupID 3 = Power User


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

        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");
        // 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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download