1. Functions related to user permissions.
These functions are used in the next page design.
Mainly include:
function User_is_login ()
function Database_contains_author ($email, $password)
function Get_md5_password ($password)
function User_has_role ($role)
1.1 User_is_login ()
functionUser_is_login () {if(isset($_post[' Action ']) and$_post[' action '] = = ' Login '){ if(!isset($_post[' Email ']) Or$_post[' password '] = = ' or!isset($_post[' Password ']) Or$_post[' password '] = = '){ $GLOBALS[' loginError '] = ' please fill in User name and password. '; return false; } if(Database_contains_author ($_post[' Email '],$_post[' Password '])){ Session_Start(); $_session[' loggedin '] =true; $_session[' email '] =$_post[' Email ']; $_session[' password '] =$_post[' Password ']; return true; } Else { Session_Start(); unset($_session[' Loggedin ']); unset($_session[' Email ']); unset($_session[' Password ']); return false; } } if(isset($_post[' Action ']) and$_post[' action '] = = ' logout '){ Session_Start(); unset($_session[' Loggedin ']); unset($_session[' Email ']); unset($_session[' Password ']); Header(' Location: '.$_post[' Goto ']); Exit(); } Session_Start(); if(isset($_session[' Loggedin '])){ returnDatabase_contains_author ($_session[' Email '],$_session[' Password ']); } return false;}
1.2 Database_contains_author ($email, $password)
functionDatabase_contains_author ($email,$password) { $password= Get_md5_password ($password); include' Db.inc.php '; Try { $sql= ' SELECT count (*) from author where email =: Email and password =:p assword '; $s=$pdo->prepare ($sql); $s->bindvalue (': Email ',$email); $s->bindvalue (':p assword ',$password); $s-execute (); } Catch(pdoexception$e) { $error= ' Query author faild ... '.$e-GetMessage (); include' Error.html.php '; Exit(); } $row=$s-fetch (); return $row[0] > 0;}
1.3 Get_md5_password ($password)
function Get_md5_password ($password) { returnMD5($password . ' IJDb ');}
1.4 User_has_role ($role)
functionUser_has_role ($role) { include' Db.inc.php '; Try { $sql=SELECT * from Authorrole INNER join author on author.id = Authorrole.authorid INNER Join role On authorrole.roleid = role.id WHERE author.email =: Email and role.id =: Roleid‘; $s=$pdo->prepare ($sql); $s->bindvalue (': Email ',$_session[' Email ']); $s->bindvalue (': Roleid ',$role); $s-execute (); } Catch(pdoexception$e) { $error= ' Query authorrole faild...does not has right ... '.$e-GetMessage (); include' Error.html.php '; Exit(); } $row=$s-fetch (); return $row[0] > 0;}
PHP and MySQL Web development from novice to master, 5th day-Add helper function