When a user login, he/she will be redirected to his/her page based on the user type. I’ve accomplished that.
Right now what i need is to make the page recognize the user_id(auto-increment in user table) of the logged in user.
function.php
<?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']); }
}
}
?>
<?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');
?>
<!DOCTYPE html>
<html>
<head>
<title> Student Page </title>
</head>
<body>
<h2 align='right'><a href='logout.php'>Logout</a></h2>
</body>
</html>
Please be aware that the mysql_* extension is now deprecated as of the current version of PHP and will very likely be removed from the next 5.x version and will likely not be in PHP 6.x (when it eventually is released). You should migrate over to either the mysqli_* extension or to PDO. PDO is a better choice as it doesn’t tie you down so much to a particular database server software.
Once you have migrated you should use Prepared Statements to prevent SQL Injection attacks. Have a read of this article from the PHP manual, it shows how to use prepared statements with PDO and also explains the principle.
Put var_dump($_SESSION); in your student.php and provide me with the output of that call. My guess is ‘user_id’ still isn’t set. Did you logout and then log back in after making the changes to the login piece?
Yes, otherwise, you have nothing to call (hence the error)
That’s really an organizational question that you could only answer. Being new to PHP, I’d be remiss not to suggest a few books that may help you with your endeavor into the language (and would teach you good practices in the process)