PHP and MySQL Web development from novice to master, 5th day-Add helper function

Source: Internet
Author: User
Tags getmessage php and php and mysql prepare

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.