i have here the code for login and it works fine i also use session here but my problem now is that how can i suppose to make a user access level?.in my login database i have username, password and level, now admin=1 and guest=0. what i want to happen is that when i login as admin it will redirect me to signup.php and if i login as guest it will redirect me to index.php, please help me i'm working this for almost two weeks.. here is my code..
db.php
<?php
session_start();
$DB_host = "localhost";
$DB_user = "root";
$DB_pass = "";
$DB_name = "survey";
$charset = "utf8";
try
{
$DB_con = new PDO("mysql:host={$DB_host};dbname={$DB_name};charset={$charset}",$DB_user,$DB_pass);
$DB_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$DB_con->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
class.user.login.php
<?php
class crud
{
private $db;
function __construct($DB_con)
{
$this->db = $DB_con;
}
public function login($uname,$upass)
{
try
{
$stmt = $this->db->prepare("SELECT * FROM users WHERE user_name=:uname AND level=:level LIMIT 1");
$stmt->execute(array(':uname'=>$uname));
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);
if($stmt->rowCount() > 0)
{
if(password_verify($upass, $userRow['user_pass']))
{
$_SESSION['user_session'] = $userRow['user_id'];
return true;
}
else
{
return false;
}
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
public function is_loggedin()
{
if(isset($_SESSION['user_session']))
{
return true;
}
}
public function redirect($url)
{
header("Location: $url");
}
?>
****login.php****
<?php
include_once 'dbconfig.php';
if($crud->is_loggedin()!="")
{
$crud->redirect('insert.php');
}
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$uname = $_POST['user'];
$upass = $_POST['pass'];
if($crud->login($uname,$upass))
{
$crud->redirect('index.php');
}
else
{
echo "<script type='text/javascript'>alert('Invalid Username or Password');</script>";
}
}
?>
home.php
<?php
include_once 'dbconfig.php';
if(!$crud->is_loggedin())
{
$crud->redirect('home.php');
}
$ID = $_SESSION['user_session'];
$stmt = $DB_con->prepare("SELECT * FROM survey_section WHERE ID=:ID");
$stmt->execute(array(":ID"=>$ID));
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);
?>