541

How to redirect into different page by user type in php and mysql

Question:

I want to redirect the users from the same log-in page.

folder structure...

root | --------/application/app.php <---------- for normal user | --------/administration/admin.php <------- for admin user | -------- index.php (Log-in page)

User Table

ID USER_NAME PASSWORD USER_TYPE 1 admin pass@admin admin 2 user pass@user user

If the login user's user type is admin then he/she will be redirected to /administration/admin.php else /application/app.php.

How can I do it ?

Regards,

Answer1:

After checking the username and password. You can check their usertype and save it to a variable in this case <strong>$user_type</strong>.

$user_type = $row['user_type'] //get the usertype of the user from table row in your database

if( $user == 1){ //check if user or password is correct from query if($user_type == "normal user"){ //check usertype header("Location:/application/app.php"); //if normal user redirect to app.php }else{ header("Location:/administration/admin.php"); //if admin user redirect to admin.php } }

Answer2:

Try something like this:

<?php $userType = $row['user_type']; if($userType == 'admin'){ header("Location: /administration/admin.php"); // This line triggers a redirect if the user_type is admin } else { header("Location: /application/app.php"); // This line triggers for other user_types } ?>

$row['user_type']; is simulating your SELECT from your database, I won't write the connection script for you, but this is just a guideline into what you need to look into. Also, look into PDO or MySQLi as MySQL is deprecated.

Recommend

  • How to eliminate Claims with ClaimStatus = 1 based on Activity Date. SQL Server 2012
  • What is the “Shell Namespace” way to create a new folder?
  • Jquery Ajax stream audio in binary format
  • Vue and Vuex: Handling dependent computed properties
  • Can an implemented class have methods NOT declared in its parent interface?
  • Using a taglet with Javadoc in Netbeans
  • Changing setPreferredIOBufferDuration at Runtime results in Core Audio Error -50
  • Retrieving Value from Row in Struts2 Table While using Displaytag
  • nodejs configuration using (*.p2f) file in eclipse
  • Unable to get the text inside the monaco editor using protractor
  • Access IPV6 with IPV4 [closed]
  • how do I array from a GET request function in viewdidload function in swift
  • How to list all Call logs from Log list including Sms and email logs also?
  • sql server back restore - login failed
  • Reveal Icon Button - Bootstrap 3
  • How to detect beginning of line, or: “The name 'getCharPositionInLine' does not exist in t
  • ODBC connection to an .accdb file
  • How can I run an Illustrator javascript on all files in a directory?
  • Visual Studio MSB3721 error when compiling a __device__ function call from another file
  • Using loops in Jasmine (with injected service)
  • How to convert days into months using datetime in Python3?
  • Find corners of a rotated rectangle
  • How can I filter an array of dictionaries in 'updateSearchResultsForSearchController' to s
  • Get name of days between two date in ios?
  • Building JavaFX 2.0 App on Mac, deploying on Windows
  • How to use array in autohotkey?
  • Generate and export point cloud from Project Tango
  • Year over Year Stats from a Crossfilter Dataset
  • JavaScript Regex to Match Boundaries of Words with diacritics
  • How to turn off notice reporting in xampp?
  • How to encrypt Connectionstring written in web.config from codebehind?
  • Computing the discrete fourier transform of audio data with FFTW
  • Using Service Component Runtime
  • Running R's aov() mixed effects model from Python using rpy2
  • Access to a Matlab gui from the web
  • convert json to excel in java
  • Create/delete users from text file using Bash script