When a user login, he/she will be directed to the page based on his/her user type… I’ve already accomplished that.
Now, how to make the page recognize the user_id(auto-increment from user table) so that i can post some of the user information and so on?
function.php
[CODE]<?php
function checkUserType($type)
{
switch($type)
{
case ‘admin’:
$page = ‘admin.php’;
break;
case 'student':
$page = 'student.php';
break;
case 'lecturer':
$page = 'lecturer.php';
break;
case 'parents':
$page = 'parents.php';
break;
}
header('Location: '.$page); //You may need to edit the path to suit your folder structure.
exit();
}
function checkUserStatus($actual_page)
{
//First check to make sure the $_SESSION[‘type’] is set, if it’s not then they haven’t logged in so redirect back to login screen
if(!isset($_SESSION[‘type’]))
{ header(‘Location: index.php’); }
else
{
if($actual_page == $_SESSION[‘type’]){}
else{ checkUserType($_SESSION[‘type’]); }
}
}
?>[/CODE]
$check_user = "select * from user where username='$username' AND pass='$password'";
$run = mysql_query($check_user);
if(mysql_num_rows($run)>0){
$results = mysql_fetch_assoc($run);
$_SESSION['user_id']=$id;
$_SESSION['type'] = $results['type'];
checkUserType($results['type']);
}
else {
echo "<script>alert('Username or Password is incorrect!')</script>";
}
}
?>
[/CODE]
student.php
[CODE]<?php
session_start();
require_once(‘function.php’); //Set this to what ever page you include that holds all your functions so that we can use the checkUserStatus()
checkUserStatus(‘student’);
?>
Line 48 in index.php
[php]$_SESSION[‘user_id’]=$id;[/php]
So in student.php you could just do something like this
[php]
$studentId = !empty($_SESSION[‘user_id’]) ? $_SESSION[‘user_id’] : null;
$studentInfo = getStudentInfoFromId($studentId);[/php]
the function must of course fetch and return the data from the DB.
Be warned that your code is highly vulnerable to sql injection.
ok i will try.. so this function i must make it in the index.php?
and i dont need to edit something in the function.php right as its for detecting my user type?
Nope, you’ve got an example of a (vulnerable) query in index.php:
[php]$check_user = “select * from user where username=‘$username’ AND pass=‘$password’”;[/php]
trying to experiment… and still got error… anyway, am i near to achieve it?
[php]<?php
session_start();
require_once(‘function.php’); //Set this to what ever page you include that holds all your functions so that we can use the checkUserStatus()
checkUserStatus(‘student’);