PHP Multi-user Login with Session -
i have 7 user levels. redirected depending on input (for example input credentials of admin, redirected admin page) , same goes other 6. problem have after logging in, if change url (localhost/admin/home.php) (localhost/employee/home.php) can access employee's page. want have restrictions on that. or maybe error says "unauthorized user. access denied." that. here's code.
index.php
<form action="checklog.php" method="post"> <h1>log in</h1> <p> <label for="username" class="uname" > email or username </label> <input id="username" name="username" required="required" type="text" placeholder="myusername " minlength="2" /> </p> <p> <label for="password" class="youpasswd"> password </label> <input id="password" name="password" required="required" type="password" placeholder="eg. x8df!90eo" minlength="2" /> </p> <input type="submit" name="submit" value="login"> </form> <?php // display error messages if(isset($_get['err'])){ if ($_get['err']==1){ echo "invalid credentials.";} else if($_get['err']==5){ echo "successfully logged out";} else if ($_get['err']==2){ echo "you're trying access unauthorized page."; } } ?> </body> checklog.php (this process credentials.)
<?php require_once("db.php"); function check_input($r){ $r=trim($r); $r=strip_tags($r); $r=stripslashes($r); $r=htmlentities($r); $r=mysql_real_escape_string($r); return $r; } if (isset($_post['username'],$_post['password'])){ $u=check_input($_post['username']); $p=md5(check_input($_post['password'])); try{ $db=get_db(); $stmt=$db->prepare("select * users username=? && password=?"); $stmt->execute(array($u,$p)); $r=$stmt->fetch(pdo::fetch_assoc); if($r){ session_start(); $access_level=$r['access_level']; $_session['username']=$r['username']; $_session['access_level']=$access_level; if ($access_level==0){ header("location: admin/home.php"); } if($access_level==1){ header("location: user/home.php"); } if($access_level==2){ header("location: businesshead/home.php"); } if($access_level==3){ header("location: scm/home.php"); } if($access_level==4){ header("location: finance/home.php"); } if($access_level==5){ header("location: gm/home.php"); } if($access_level==6){ header("location: scma/home.php"); } } else{ header("location:index.php?err=1"); } } catch(pdoexception $e){ die("database error: ".$e->getmessage()); } } else{ header("location:index.php"); } ?> and lets assume admin page (admin.php)
<!doctype html> <body> welcome! </body> </html> thank in advance!
you have check session on every page. put related code on top of every page like
admin page
<?php session_start(); if($_session['type'] != 0){ echo "unauthorized user. access denied." die; // stop further execution } ?> user page
<?php session_start(); if($_session['type'] != 1){ echo "unauthorized user. access denied." die; // stop further execution } ?>
Comments
Post a Comment