2016-10-05 45 views
1

在我的登錄頁面中,我只使用電話號碼和密碼字段進行登錄,此後,我正在使用電話號碼創建和存儲會話。如何在創建會話時獲取兩個字段?

insted的,我想呼應用戶名當前登錄到顯示在我的情況下,當前用戶becasue我目前只能夠顯示電話號碼登錄的用戶的。我怎麼做?

這裏是我的登錄腳本

<?php 
    // Starting Session 
    session_start(); 

    include "../script.php"; 

    $error=''; // Variable To Store Error Message 

    if (isset($_POST['signin'])) { 
    if (empty($_POST['signinphone']) || empty($_POST['signpassword'])) { 



     $error = "Phone or Password is invalid"; 
    } 
    else 
    { 
    // Define $username and $password 
    $phone=$_POST['signinphone']; 
    $password=$_POST['signpassword']; 


    // To protect MySQL injection for Security purpose 


     $phone = stripslashes($phone); 
     $password = stripslashes($password); 
     $phone = pg_escape_string($db, $phone); // Set email variable 
     $password = pg_escape_string($db, $password); // Set hash variable 

     $pass_crypted = password_hash($password); 

    // SQL query to fetch information of registerd users and finds user match. 

     $sql="SELECT usr_id, usr_email, usr_first_name, usr_last_name, 
       usr_encrypted_password, 
         usr_salt, usr_stos_id, usr_pers_id, usr_username, usr_updated_at, 
         usr_created_at, usr_enabled, usr_role_id, usr_jbrn_id, 
       usr_mobile_number, 
         stp_acc_id, usr_location, usr_mobile_imei, usr_type 
       FROM js_core.stp_users 

       where usr_mobile_number='$phone' 

       AND usr_encrypted_password='$password'"; 
     $result=pg_query($db, $sql); 

     $rows = pg_num_rows($result); 

     if ($rows == 1) { 
       $_SESSION['phone']=$phone; // Initializing Session 
       $_SESSION['username'] = pg_fetch_object($result)->usr_last_name; 
       header("location: ../index.php"); 

       } else { 
      //echo "0 results"; 

       echo "Try Again the credentials you entered don't much ours"; 

      } 

     ; // Closing Connection 

     } 
     } 


    ?> 

,這裏是我的示例代碼,我想以顯示電話

<li> 
     <?php 

        if(isset($_SESSION['username'])) { 

         echo '<li><a href="#">'. $_SESSION["username"] . '</a></li>'; 
         echo '<li> 
          <a href="config/logout.php" id="logout">Log Out</a></li>'; 
        } else { 
        echo '<a class="signing" href="#login" data-toggle="modal" data-target="#signIn">Login </a>'; 
        } 
     ?> 
</li> 
+0

將會話數組分配給發佈數組。另外,我希望你不會與此相處。 MD5不再安全。 –

+0

@ Fred-ii-我該怎麼做? – john

+0

您正在從查詢中獲取所有用戶信息,因此只需保存會話所需的位。例如,在'if($ rows == 1){'add:'$ _SESSION ['username'] = pg_fetch_object($ result) - > usr_username;' – Steve

回答

0

的用戶名就地沒有一個回答你的問題。

我發佈這個,因爲它包含所有的事情,已經在您的問題的意見中提到的一個例子。

首先,您會注意到有一個新的使用PDO的$db連接。這是處理數據庫連接的普遍接受的方式,並且相對容易安裝(如果您的PHP版本沒有它) - 這裏有很多例子。我假設你想在你的script.php這個,因爲它很常見。

我也換掉了原生BCRYPTpassword_hash()函數的密碼散列函數。當你登錄用戶時,你可以像這樣使用它:

$encryped_password = password_hash($_POST['signpassword'], PASSWORD_BCRYPT); 

這包含一個默認成本的獨特鹽漬密碼。

接下來,您可以像原來一樣抓取用戶,並進行小調整以使其成爲準備好的語句。這提供了SQL注入保護,並且通常使事情更清潔。

然後您會看到,在取出該行後,您可以將密碼與password_verify()函數進行比較。

最後到你原來的問題 - 我已經設置PDO模式爲對象,所以你可以以同樣的方式訪問和分配儘可能多的屬性。該對象中只有SELECT子句中的屬性可用。

// Starting Session 
session_start(); //I'd suggest this should also go in your script.php 

$db = new PDO('pgsql:dbname=mydb;host=localhost;user=myuser;password=mypass'); 

include "../script.php"; 

$error=''; // Variable To Store Error Message 

if (isset($_POST['signin'])) { 
    if (empty($_POST['signinphone']) || empty($_POST['signpassword'])) { 
     $error = "Phone or Password is invalid"; 
    } 
    else 
    { 
     // SQL query to fetch information of registerd users and finds user match. 
     $sql = 'SELECT usr_id, usr_email, usr_first_name, usr_last_name, usr_encrypted_password 
          usr_stos_id, usr_pers_id, usr_username, usr_updated_at, 
          usr_created_at, usr_enabled, usr_role_id, usr_jbrn_id, 
          usr_mobile_number, stp_acc_id, usr_location, usr_mobile_imei, 
          usr_type 
        FROM js_core.stp_users 
        WHERE usr_mobile_number = :phone_number'; 

     $stmt = $db->prepare($sql); 
     $stmt->execute(['phone_number' => $_POST['signinphone']]); 

     if ($row = $stmt->fetch(PDO::FETCH_OBJ)){ 
      if(password_verify($_POST['signinpassword'], $row->usr_encrypted_password)) { 
       $_SESSION['phone'] = $row->usr_mobile_number; // Initializing Session 
       $_SESSION['username'] = $row->usr_username; 

       header("location: ../index.php"); 
      } else { 
       //valid user, invalid password 
      } 
     } else { 
      //Invalid user 
      echo "Try Again the credentials you entered don't much ours"; 
     } 
    } 
} 

我做了你在租約PHP 5.5的password_hash運行的假設,但有一個polyfill如果不是。

相關問題