2015-04-03 144 views
0

因此,基本上,我試圖匹配已經在數據庫中的用戶名和密碼,但我不明白爲什麼當密碼沒有重定向到另一頁時和用戶名是正確的如何將密碼用戶名與登錄表單中的php mysql匹配

這裏是我的HTML表單

<form class="form-inline" method="post" action= "login.php" > 

<input type="email" placeholder="Enter email" class="form-control" name="logemail"> 
<input type="password" placeholder="password" class="form-control" name="logpass"> 

<button type="submit" class="btn btn-default" id="login" name ="submit1">Log in</button> 
</form> 

這是我的login.php文件

<?php 



$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "registration_form"; 

if(isset($_POST['submit'])) 
{ 
$username = $_POST['logemail']; 
$password = $_POST['logpass']; 
$con=mysqli_connect("localhost","root","","registration_form"); 
// Check connection 
if (mysqli_connect_errno()) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 
$qz = "SELECT * FROM regis where email1='".$username."' and password3='".$password."'" ; 
$qz = str_replace("\'","",$qz); 
$result = mysqli_query($con,$qz); 
$row = mysqli_num_rows($result); 
if($row == 1) 
    { 
    echo "successfully logged in"; 
    } 
mysqli_close($con); 
} 
?> 
+0

不要使用引號替換。我會說這是你的問題。 http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1 – chris85 2015-04-03 02:19:04

+0

你沒有任何代碼重定向,所以它不會重定向。您可能正在尋找標頭,http://php.net/manual/en/function.header.php,但回聲後無法使用。 – Jonathan 2015-04-03 02:19:24

+0

停止並重組,如果可用,'password_hash'用於保存密碼和'password_verify'用於檢查,並且最後使用準備好的語句 – Ghost 2015-04-03 02:23:16

回答

0

編輯

當你應該檢查submit1時,你重新檢查isset中的提交,這就是爲什麼你的代碼沒有在if塊中執行,但是我更新的答案消除了這一點。

更新答::

dologin.php

<?php 

// here u would move these vars onto a seperate file outside 
// the webroot that is readable by the server and "require_once" 
// that file. 
$dbhost = "localhost"; 
$dbuser = "mysql_user_name"; 
// using root is bad only use it for local development but better yet, 
//// make a user for the database in question 
$password = "password"; 
$dbname = "registration_form"; 

// Setup a filter array to sanitize/validate user input. 
// look on php.net for more information on filters available 
$filters = [ 
       'logemail' => ["ARRAY, OF, FILTERS"], 
       'logpass' => ["ARRAY, OF, FILTERS"] 
      ]; 

// NEVER NEVER NEVER access $_POST without filtering it. 
$posted = filter_var_array(INPUT_POST, $filters, true); 

// check our posted array is not empty, even if its submitted it 
// could be empty values. 
if (!empty($posted)) { 

    // I have omitted the $dbname in this procedural example 
    // and move it after the error No, this is just to avoid 
    // confusion and limit the number of possible things to go wrong. 
    $con = mysqli_connect($dbhost, $dbuser, $dbpass); 

    $username = $posted['logemail']; 
    $password = $posted['logpass']; 

    // Check no connection error 
    if (!mysqli_connect_errno()) { 
     mysqli_select_db($con, $dbname) or die("Database select error" . mysqli_error()); 
    } else { 
     die("Failed to connect to MySQL: " . mysqli_connect_error()); 
    } 

    $qz = "SELECT * FROM regis WHERE email1 = $username AND password3 = $password"; 


    $result = mysqli_query($con, $qz); 
    if (mysqli_num_rows($result) == 1) { 

     // initialise the session. 
     session_start(); 

     // add an entry "loggedin" and set it to true. 
     $_SESSION['loggedin'] = true; 
     header('location :index.php');   
    } 
    mysqli_close($con); 
} 

現在,你需要做你的索引頁。

的index.php

<?php 

// Start up the session its needed to maintain logins 
session_start(); 

// We don't know that the raw $_SESSION is safe 
$session_unsafe = $_SESSION; 

// Lets play with some more filtering (php.net) 
$session = filter_var($session_unsafe, FILTER_VALIDATE_BOOLEAN); 

// add more filtering as required. 

// remember the boolean in dologin.php here === makes sure it 
// matches the "type" of the variable too because: 
// 1 == yes == true 
// 0 == no == false 
// but === means an exact match of type (integer/string/boolean) as 
// well as its value. True === 1 would return false. 
if ($session === true) { 

    // show logged in stuff 


} else { 

    // do your none logged in actions 
} 

結論:這不是解決這個最好的方法,你可以使用使用庫MySQLi或PDO的OOP方式,實現更清潔更看代碼可管理相同的結果。由於您似乎剛剛從樹上移開,您可能希望查看這兩種方法之一併在繼續之前瞭解這些方法。同時注意保護會話。我給你的例子會讓你的登錄工作,但它不會是安全的,你也不會做很多事情。

所以你的檢查要點:

PHP:面向對象,會話,過濾器這些關鍵點我想看看作爲一個優先事項,然後再繼續。

現在,你會選擇sql查詢,但它不會同時學習它們。所以找到一些用於學習SQL的好資源。

+0

問題是,它甚至不顯示回聲線,它只是一個任務,我只是想了解我的代碼的問題 – lelouche 2015-04-03 22:59:59

+0

**代碼中缺少mysqli_select_db($ dbname)**,您正在連接成功地到服務器(如果您提供的是正確的憑證),然後用命令查詢服務器,因爲您沒有選擇查詢所有數據庫。這將失敗的原因應該是讀取權限(不授予任何其只是原因)。 **在連接和查詢**之間添加數據庫選擇,並且「應該」起作用。如果不能深入研究,請逐一打印出錯。像錯誤的列名稱中斷查詢 – Chris 2015-04-04 14:31:43

+0

更新我的答案一樣簡單。 – Chris 2015-04-04 15:33:16

0

既然你已經使用isset($ _ POST [ '提交'])修改您的按鈕的HTML作爲

<button type="submit" class="btn btn-default" id="login" name ="submit">Log in</button> 

,另外檢查,你沒有寫任何代碼重定向,而不是

echo "successfully logged in"; 

header("location:new_page.php"); 
exit(); 
+0

謝謝你幫助我! – lelouche 2015-04-04 19:14:53

+0

你應該提高答案,以便它也可以幫助其他人。 – 2015-04-04 19:16:28