无法验证对吗?为什么? PDO新手

时间:2014-10-26 15:42:32

标签: php validation oop pdo

当我有一个空字段或用户名错误或不匹配时,我似乎无法验证。请任何帮助或指点我会非常有帮助。我试过(空但是当我填写一个字段而另一个是空的时它似乎没有工作,它说所有字段都是空的。并且对于错误的凭据它根本不起作用。

的index.php

<?php
session_start();
include_once 'php/classes/class.user.php';

$user = new User();
$log = $_SESSION['uid'];

if ($user->get_session($log)){
    header("Location: profile.php?uid=".$log."");
}

if (isset($_REQUEST['submit'])) {
    extract($_REQUEST);
    $login = $user->check_login($emailusername, $password);

    if(!empty($login)){

         if($emailusername != $login){

            if($password != $login){
                if ($login) {
                // Registration Success
                $log_id = $_SESSION['uid'];
                header("location: profile.php?uid=".$log_id."");

                }
            }else
                echo "Incorrect Password";

        }else
            echo "Incorrect Email";

    }else
        echo "Fill in fields";
}
?>

USERS.PHP

<?php 
include "db_config.php";

class User{

    public $db;

    public function __construct(){
        $this->db = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);

        if(mysqli_connect_errno()) {
            echo "Error: Could not connect to database.";
                exit;
        }
    }

    /*** for login process ***/
    public function check_login($emailusername, $password){

        $password = md5($password);
        $sql2="SELECT uid from users WHERE uemail='$emailusername' or uname='$emailusername' and upass='$password'";

        //checking if the username is available in the table
        $result = mysqli_query($this->db,$sql2);
        $user_data = mysqli_fetch_array($result);
        $count_row = $result->num_rows;

        if ($count_row == 1) {
            // this login var will use for the session thing    
            session_start();
            $emaildb == $_SESSION['uemail'];
            $_SESSION['login'] = true;
            $_SESSION['uid'] = $user_data['uid'];
            return true;
        }
        else{
            return false;
        }
    }
    /*** for showing the username or fullname ***/

    public function get_fullname($uid){
    $sql = "SELECT * FROM users WHERE uid = $uid";
    $result = mysqli_query($this->db, $sql);
    $user_data = mysqli_fetch_array($result);

        echo $user_data['fullname'], "<br/>";
        echo $user_data['uemail'], "<br/>";
        echo $user_data['uid'], "<br/>";
    }

    public function check_user($uid){

        $sql5 = "SELECT * from users WHERE uid='$uid'";
        $result1 = mysqli_query($this->db, $sql5);
        $count_row1 = $result1->num_rows;

        return ($count_row1 == 1);

    }

    /*** starting the session ***/
    public function get_session(){
        return $_SESSION['login'];
    }

    public function user_logout() {
        $_SESSION['login'] = FALSE;
        session_destroy();
    }

}

2 个答案:

答案 0 :(得分:1)

根据你所拥有的,这就是你需要的。

session_start();

include_once 'php/classes/class.user.php';

$user   =   new User();
// You need a conditional incase this session isn't set
$log    =   (isset($_SESSION['uid']))? $_SESSION['uid']:false;

if($log !== false && $user->get_session($log)){
    header("Location: profile.php?uid=".$log."");
    exit;
}

if(isset($_POST['submit'])) {
    // This function should be validating your login so you don't need
    // any comparisons after the fact.
    $login = $user->check_login($_POST['email'], $_POST['password']);

    if($login !== false)
        header("location: profile.php?uid=".$log_id."");
        exit;
    else { 
            foreach($user->error as $kind => $err) {
                    echo '<h2>'.$kind.'</h2>'.'<p>'.$err.'</p>';
                }
        }
}

您的用户类:如果您愿意,可以将错误报告投放到此课程中。

class User{

    public $db;
    public $error;
    public function __construct(){
        $this->db = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);

        if(mysqli_connect_errno()) {
             $this->error['db'] =  "Error: Could not connect to database.";
             echo  $this->error['db'];
             exit;
        }
    }

    /*** for login process ***/
    public function check_login($emailusername='', $password=''){
            // Validate that your email is a real one
            if(filter_var($emailusername,FILTER_VALIDATE_EMAIL) !== false) {

                    $password   =   md5($password);
                    // --> You can prepare, bind, and execute your values here replacing what you have now....<--
                    $sql2       =   "SELECT uid from users WHERE uemail='$emailusername' or uname='$emailusername' and upass='$password'";

                    //checking if the username is available in the table
                    $result     =   mysqli_query($this->db,$sql2);
                    $user_data  =   mysqli_fetch_array($result);
                    $count_row  =   $result->num_rows;

                        if ($count_row == 1) {
                             $emaildb               ==  $_SESSION['uemail'];
                            // this login var will use for the session thing
                            $_SESSION['username']   =   $user_data['uemail'];
                        //  $_SESSION['uemail']     =   $user_data['uemail'];
                            $_SESSION['uid']        =   $user_data['uid'];
                            $_SESSION['login']      =   true;
                        }
                    else
                        $this->error['account'] = 'ERROR: Invalid Username/Password';
                }
            else
                $this->error['email'] = 'ERROR: Invalid Email Address';

            return  (!isset($_SESSION['uemail']))? false:true;
        }
    /*** for showing the username or fullname ***/

    public function get_fullname($uid){

            // --> You can prepare, bind, and execute your values here replacing what you have now....<--
            $sql = "SELECT * FROM users WHERE uid = $uid";
            $result = mysqli_query($this->db, $sql);
            $user_data = mysqli_fetch_array($result);

            echo $user_data['fullname'], "<br/>";
            echo $user_data['uemail'], "<br/>";
            echo $user_data['uid'], "<br/>";
        }

    public function check_user($uid){

        // --> You can prepare, bind, and execute your values here replacing what you have now....<--
        $sql5 = "SELECT * from users WHERE uid='$uid'";
        $result1 = mysqli_query($this->db, $sql5);
        $count_row1 = $result1->num_rows;

        return ($count_row1 == 1);

    }

    /*** starting the session ***/
    public function get_session(){
        return $_SESSION['login'];
    }

    public function user_logout() {
        $_SESSION['login'] = FALSE;
        session_destroy();
    }

}

答案 1 :(得分:-1)

$login是一个布尔变量,而$emailusername$password是字符串,为什么要比较它们。