"SELECT * FROM plus_signup WHERE userid='$userid' AND password = '$password'"
plus_signup is the table name, userid and password are the field names. This SQL statement will return us one record if member userid and password is there in our table. So we can use one if condition to process the script if the usrid and password is correct and is there in the table.
$userid=$_POST['userid'];
$password=$_POST['password'];
Now let us check the database with our query.
$count=$dbo->prepare("select password,mem_id,userid from plus_signup where userid=:userid");
Here is the code to do all this for us.
$count=$dbo->prepare("select password,mem_id,userid from plus_signup where userid=:userid");
$count->bindParam(":userid",$userid,PDO::PARAM_STR);
$count->execute();
$row = $count->fetch(PDO::FETCH_OBJ);
if($row->password==md5($password)){
echo " Inside ";
// Start session n redirect to last page
$_SESSION['id']=session_id();
$_SESSION['userid']=$row->userid;
$_SESSION['mem_id']=$row->mem_id;
//echo " Inside session ". $_SESSION['userid'];
$msg=" welcome $_SESSION[userid] loging successfully , please wait ... ";
echo $msg;
echo "<script language='JavaScript' type='text/JavaScript'>
<!--
window.location='welcome.php';
//-->
</script>
";
}else{
$msg = " Login failed, tray again ... <br><INPUT TYPE='button' VALUE='Back' onClick='history.go(-1);'>";
}
echo $msg;
In the above code if the validation is correct then we are creating session variables
$_SESSION['id']=session_id();
$_SESSION['userid']=$row->userid;
The else condition if userid or password is not correct will display the message and ask the member to try again.
$_SESSION['role']=$row->role;
Different areas of the site we can keep and while checking the session status we can allow or reject the access to the user to the page by reading the session role value.
<?Php
if(!(isset($_SESSION['userid']) and strlen($_SESSION['userid']) > 2)){
echo "Please <a href=g_login.php>login</a> to use this page ";
exit;
}else{
if($_SESSION['role'] != 'admin'){
echo "You are not authorized to use this page ";
exit;
}else{
echo "Welcome $_SESSION[userid] | <a href='logout.php'>Logout</a>|
<a href='change.php'>Change Password</a>";
}
}
?>