Aim
A simple login page that only check the user credentials if it correct as in database lets the user to login and check on other pages if the already the sessoin is provide let user to do any operation in pages else will redirect them out to login page for authentication.
the connection is set and working fine:
the session has started:
for checking the input and echo variables simple checking function:
function Check_Param($val){
$value1=addslashes($val);
$string1=htmlspecialchars($value1);
$string2=strip_tags($string1);
return $string2;
}
now the user will authenticate trough the form and runs this function to check if he can login the pages and pass the $_session['username'] AND $_session['password'] AND $_session['level'] to next page and store to the browser tell getting the destroy command.
public function auth($name = '', $password = '', $level=''){
$sql = "SELECT COUNT(*) FROM dab_users WHERE `name`=:name AND `password`=:password AND `level`=:level ";
$result = $this->conn->prepare($sql);
$passme = hash_value(Check_Param($this->password));
$result->execute(array(
":name" => Check_Param($_POST['name']),
":password" => Check_Param($_POST['passwrod'),
":level" => Check_Param($_POST['level'])
));
$num = $result->fetchColumn();
if($num==1){
$_SESSION['level'] = $_POST['level'];
$_SESSION['is_log'] = 1;
header("location:home/index.php");
}else {
$result->execute(array(
":name" => Check_Param(''),
":password" => Check_Param(''),
":level" => Check_Param('')));
echo "wrong password and user";
}
}
this works OK and i can get access to the pages after giving the correct password on the view of index page which after this authentication the user can get access here is the way i have manage to restrict unknown user or the blank session or correct session for this page.
index.php:
<?php
if(!empty($_SESSION['is_log'] && !$_SESSION['level'] == 101){
header("location:../login.php");
} else {
?>
<h1>header + content + footer</h1>
<?php } ?>
Question: here after submit the form with correct user name and password and level which are in database i get login to the index page, and on the echoing session i see the level and is_log which on aut class is been set. and the (if) on the index page also works and redirect user if not login...
- is this secure?
- is this enough for session restriction?
- is this fine session control ?
- Am i doing right the session controlling?
- any good suggestion or tutorial?