身份验证不会从查询返回有效结果?

时间:2016-12-07 01:24:05

标签: php mysql authentication phpmyadmin

我正在编写代码来询问登录信息&有效,如果有效则跳转到另一页。但为什么它总是留在登录页面? 的index.php:

<?php
 session_start();
 require('../model/database.php');
 require('../model/admin_db.php');

 $action = filter_input(INPUT_POST, 'action');

 if ($action === NULL) {

    $action = filter_input(INPUT_GET, 'action');

    if ($action === NULL) {
      $action = '';
    }
 }

 if(!isset($_SESSION['is_valid_admin'])){
   $action = 'get_admin';
 }
 switch($action){
    case 'get_admin':
        $username = filter_input(INPUT_POST, 'username');
        $password = filter_input(INPUT_POST, 'password');
        echo $username;
        echo $password;
        echo is_valid_admin_login($username, $password);

        if (is_valid_admin_login($username, $password)){
            $_SESSION['is_valid_admin']=true;
            include ('admin_menu.php');
        }
        else {
            $login_message = 'you must login to view this page.';
            include ('adminlogin.php');
        }

     break;

admin_db.php:

<?php
function is_valid_admin_login($username, $password) {
   global $db;
   $password = sha1($username . $password);
   $query = 'SELECT username FROM administrators
            WHERE username = :username 
                            AND password = :password';

   $statement = $db->prepare($query);
   $statement->bindValue(':username', $username);
   $statement->bindValue(':password', $password);
   $statement->execute();
   $valid = ($statement->rowCount() == 1);
   $statement->closeCursor();
   return $valid;
}
?>

连接数据库没有问题。

1 个答案:

答案 0 :(得分:0)

您在include ('adminlogin.php');声明后使用if else。所以每次打开adminlogin.php

要重定向页面,您也可以使用Header