Hello everyone! I am a student and still a noob in PHP. Well, I'm trying to figure out what is the best method for a log in page system that provide the ability for an Administrator to log in using the same log in form for normal users but get redirected to a administrator page if the system recognize as Admin and normal users redirected to users page. Any idea? Any help would be greatly appreciated! :) Here is my log in code for users -->
<?php
//Start session
session_start();
//Include database connection details
require_once('voting_connect.php');
//Array to store validation errors
$errmsg_arr = array();
//Validation error flag
$errflag = false;
//Connect to mysql server
include('voting_connect.php');
{
//Select database
$db = mysql_select_db($db);
if(!$db) {
die("Unable to select database");
}
//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
//Sanitize the POST values
$s_ID = clean($_POST['s_ID']);
$s_Password = clean($_POST['s_Password']);
//Input Validations
if($s_ID == '') {
$errmsg_arr[] = 'ID number missing';
$errflag = true;
}
if($s_Password == '') {
$errmsg_arr[] = 'Password missing';
$errflag = true;
}
//If there are input validations, redirect back to the login form
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: login.php");
exit();
}
//Create query
$qry="SELECT * FROM students WHERE s_ID='$s_ID' AND s_Password='$s_Password'";
$result=mysql_query($qry);
//Check whether the query was successful or not
if($result) {
if(mysql_num_rows($result) == 1) {
//Login Successful
session_regenerate_id();
$member = mysql_fetch_assoc($result);
$_SESSION['SESS_MEMBER_ID'] = $member['s_ID'];
$_SESSION['SESS_FIRST_NAME'] = $member['s_FirstName'];
$_SESSION['SESS_LAST_NAME'] = $member['s_LastName'];
session_write_close();
header("location: vote.php");
exit();
}else {
//Login failed
header("location: logfail.php");
exit();
}
}else {
die("Query failed");
}
}
?>