PHP和MySQL Web開發從新手到高手,第5天-添加輔助函數

來源:互聯網
上載者:User

標籤:

1. 使用者權限相關的函數.

  這些函數在接下來的頁面設計中會用到.

  主要包括:

  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()

function user_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‘])){        return database_contains_author($_SESSION[‘email‘], $_SESSION[‘password‘]);    }        return false;}

1.2 database_contains_author($email, $password)

function database_contains_author($email, $password) {    $password = get_md5_password($password);    include ‘db.inc.php‘;        try {        $sql = ‘select count(*) from author where email = :email and password = :password‘;        $s = $pdo->prepare($sql);        $s->bindValue(‘:email‘, $email);        $s->bindValue(‘:password‘, $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){    return md5($password.‘ijdb‘);}

1.4 user_has_role($role)

function user_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 have right...‘.$e->getMessage();        include ‘error.html.php‘;        exit();    }        $row = $s->fetch();        return $row[0] > 0;}

PHP和MySQL Web開發從新手到高手,第5天-添加輔助函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.