2017-01-25 8 views
0

ここに私のユーザーのログインコードです。ログイン時にフィールド値の1つを渡す必要があります

<?php 

require("Conn.php"); 
require("MySQLDao.php"); 
$email = htmlentities($_POST["email"]); 
$password = htmlentities($_POST["password"]); 
$returnValue = array(); 

if(empty($email) || empty($password)) 
{ 
    $returnValue["status"] = "error"; 
    $returnValue["message"] = "Missing required field"; 
    echo json_encode($returnValue); 
    return; 
} 

$secure_password = md5($password); 

$dao = new MySQLDao(); 
$dao->openConnection(); 
$userDetails = $dao->getUserDetailsWithPassword($email,$secure_password); 

if(!empty($userDetails)) 
{ 
    $returnValue["status"] = "Success"; 
    $returnValue["message"] = "User is Logged in"; 
    echo json_encode($returnValue); 
} else { 

    $returnValue["status"] = "error"; 
    $returnValue["message"] = "User is not found"; 
    echo json_encode($returnValue); 
} 

$dao->closeConnection(); 

?> 

と私のSQLコードは以下の通りです:

<?php 
class MySQLDao { 
    var $dbhost = null; 
    var $dbuser = null; 
    var $dbpass = null; 
    var $conn = null; 
    var $dbname = null; 
    var $result = null; 

    function __construct() { 
     $this->dbhost = Conn::$dbhost; 
     $this->dbuser = Conn::$dbuser; 
     $this->dbpass = Conn::$dbpass; 
     $this->dbname = Conn::$dbname; 
    } 


    // function to open connection 

    public function openConnection() { 
     $this->conn = new mysqli($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname); 
     if (mysqli_connect_errno()) 
     echo new Exception("Could not establish connection with database"); 
    } 

    // function to return the connection 

    public function getConnection() { 
     return $this->conn; 
    } 

    // function to close the connection 

    public function closeConnection() { 
     if ($this->conn != null) 
     $this->conn->close(); 
    } 

    // function to get user email 

    public function getUserDetails($email) 
    { 
     $returnValue = array(); 
     $sql = "select * from ap_users where user_email='" . $email . "'"; 

     $result = $this->conn->query($sql); 
     if ($result != null && (mysqli_num_rows($result) >= 1)) { 
      $row = $result->fetch_array(MYSQLI_ASSOC); 
      if (!empty($row)) { 
       $returnValue = $row; 
      }  
     } 
     return $returnValue; 
    } 

    // get user details using email and password 

    public function getUserDetailsWithPassword($email, $userPassword) 
    { 
     $returnValue = array(); 
     $sql = "select id,user_email from ap_users where user_email='" . $email . "' and user_password='" .$userPassword . "'"; 

     $result = $this->conn->query($sql); 
     if ($result != null && (mysqli_num_rows($result) >= 1)) { 
      $row = $result->fetch_array(MYSQLI_ASSOC); 
      if (!empty($row)) { 
       $returnValue = $row; 
      } 
     } 
     return $returnValue; 
    } 

    // register user with all fields 

    public function registerUser($email, $password, $username, $fname, $lname, $mobile, $roleid) 
    { 
     $sql = "insert into ap_users set user_email=?, user_password=?, user_username=?, user_fname=?, user_lname=?, user_mobile=?, user_roleid=?"; 
     $statement = $this->conn->prepare($sql); 

     if (!$statement) 
      throw new Exception($statement->error); 

     $statement->bind_param("sssssss", $email, $password, $username, $fname, $lname, $mobile, $roleid); 
     $returnValue = $statement->execute(); 

     return $returnValue; 
    } 

} 
?> 

現在ログイン時に私が取得しています「成功」とステータスとメッセージごとに値を「ユーザーがログインしています」。 しかし、私はログインメッセージでユーザーの "roleid"をプッシュしたい、助けてください!

+0

分かりやすいコードのインデントが良い考えです。コードを読むのに役立ちます。もっと重要なのは**あなたのコードをデバッグするのに役立ちます** [コーディング標準を見てください](http://www.php-fig.org/psr/psr-2/ )あなた自身の利益のために。あなたはこのコード を数週間/数ヶ月で修正するように頼まれるかもしれません。そして、あなたは私に最後に感謝します。 – RiggsFolly

+1

自分のパスワードハッシュを__rollしないでください。 PHPは['password_hash()'](http://php.net/manual/en/function.password-hash.php) と['password_verify()'](http://php.net/manual/ en/function.password-verify.php)それらを使用してください。 ここにいくつかの[パスワードに関する良いアイデア]があります(https://www.owasp.org/index.php/Password_Storage_Cheat_Sheet) 5.5より前のバージョンのPHPを使用している場合は、[互換パックがあります](https ://github.com/ircmaxell/password_compat) – RiggsFolly

+1

*** [MD5パスワードハッシュ](http://security.stackexchange.com/questions/19906/is-md5-considered-insecure)を使用しないでください。 **。ハッシュする前に[パスワードを逃さないでください](http://stackoverflow.com/q/36628418/1011527)を確認するか、他のクレンジングメカニズムを使用してください。パスワードを変更すると、パスワードが変更され、不要な追加のコーディングが発生します。 –

答えて

0

roleidは同じ表の "user_roleid"列です。あなたのgetUserDetailsWithPassword-方法

$ SQL = "ID、USER_EMAIL、ap_users USER_EMAILからuser_roleidを選択..."

変更コードと持ってください。コメント(パスワードとハッシング)の他のアドバイスを見てください!

+0

とどのようにroleid値が表示されます! –

+0

私はあなたのgetUserDetailsWithPassword-メソッドの権利を理解していれば、$ userDetailsはキー 'user_roleid'になります。 jsonに渡すことができます。例: $ returnValue ['user_roleId'] = $ userDetails ['user_roleid'] – BenRoob

+0

これは機能しました。ありがとう –

関連する問題