2016-04-19 53 views
1

我有用PDO編寫的登錄腳本。當用戶登錄時,它們應該被重定向到index.php。相反,他們保持在同一頁面上。用戶已登錄,但未被重新定向

<?php 

$db_username = "user"; 
$db_password = "pass"; 
$con = new PDO("mysql:host=localhost;dbname=db", $db_username, $db_password); 

if(isset($_POST['submit'])) { 

$username = $_POST['username']; 
$password = md5($_POST['password']); 

$logincheck = $stmt = $con->prepare("SELECT * FROM users WHERE username=:username and password=:password"); 
$stmt->bindParam(':username', $username); 
$stmt->bindParam(':password', $password); 
$stmt->execute(); 
$rows = $stmt->fetch(PDO::FETCH_NUM); 
if($rows > 0) { 
header('Location: index.php'); 
} 
} 
?> 

<form method="post"> 
<input type="text" name="username"> 
<input type="password" name="password"> 
<input type="submit" name="submit"> 
</form> 
+2

把出口AFTER頭重定向 – Mihai

+0

請使用PHP的[內置函數](http://jayblanchard.net/proper_password_hashing_with_PHP.html)來處理密碼安全性。如果您使用的PHP版本低於5.5,則可以使用'password_hash()'[兼容包](https://github.com/ircmaxell/password_compat)。確保你[不要逃避密碼](http://stackoverflow.com/q/36628418/1011527)或在哈希之前使用其他任何清理機制。這樣做*更改密碼並導致不必要的附加編碼。 –

+0

僅使用帶有退出的整數值。 試試Mihai的回答 –

回答

0
  1. 變化if(isset($_POST['submit']))if(isset($_POST['username']) && isset($_POST['password']))

    因爲提交類型無法通過形式

  2. 被髮送務必有關散列算法

  3. 變化exit(header('Location: index.php'));header('Location: http:// www.example.com/index.php');

    因爲exit();不適合,所以這裏是語法錯誤。

頭必須單獨發送該網頁的執行後已退出。

我正在考慮$con變量是預先設定並沒有對SQL的錯誤讀取。代碼中缺少PDO數據庫日誌記錄。這麼多的錯誤可能是可能的。

使用錯誤的php.ini文件報告以跟蹤到底發生了什麼

+0

試過了你的建議,但沒有運氣。我將使用'$ con'變量和數據庫連接更新我的文章。 –

+0

在php中報告什麼錯誤? –

+0

沒有錯誤報告 –

0

我有這個問題最近工作的一個項目。這使我心中唯一的事情就是:

"**Warning:** Cannot modify header information - headers already sent (output started at /htdocs/your_project_name/your_file.php:X, where X is the line number)." 

仔細檢查你的錯誤;插入您使用的是處理表單提交腳本在上面這個地方:

ini_set('display_errors', 1); 
ini_set('display_startup_errors', 1); 
error_reporting(E_ALL); 

我能得到它做這個工作:

ini_set('display_errors', 1); 
ini_set('display_startup_errors', 1); 
error_reporting(E_ALL); 

if ((isset($_POST['username']) && !empty($_POST['username'])) && 
    (isset($_POST['password']) && !empty($_POST['password']))) { 
    //echo $_POST['username']; 
    //echo $_POST['password']; 
    // these statements are commented out because they were 
    // displaying information before the header('Location: index.php') 
    // was called, uncomment this and try to see what I am talking about 

    $username = $_POST['username']; 
    $password = $_POST['password']; 

    $db_username = 'root'; 
    $db_password = 'root'; 
    // always use try-catch when working with databases, api's, etc. 
    try 
    { 
    $dbconn = new PDO('mysql:host=localhost;dbname=db', $db_username, $db_password); 

    $stmt = $dbconn->prepare('SELECT * FROM users WHERE username=:username AND 
    password=:password LIMIT 1'); 
    $stmt->bindParam(':username', $username); 
    $stmt->bindParam(':password', $password); 

    $stmt->execute(); 

    if ($stmt->rowCount() > 0) 
    { 
     header('Location: index.php'); 
    } 
    } 
    catch (PDOException $e) 
    { 
    echo 'Database error: ' . $e->getMessage(); 
    } 
} 

顯然,形式不變化,但我添加了行動屬性:

<form method="post" action="submit.php"> 
<!--form fields--> 
</form> 

顯然,確保您存儲用戶的密碼安全。在檢查數據庫之前,我沒有對密碼進行哈希處理,但應始終對從表單提交的任何輸入進行清理。就拿這裏看看

  • phpsec.org/projects/guide/1.html#1.4

這裏

  • php.net/manual/en/function.password-hash 。PHP

這尤其因爲你會使用這個功能來檢查密碼是否匹配:問題

http://php.net/manual/en/function.password-verify.php

如果您仍然有問題,並且頭已經發出已是問題,看看這個帖子:

How to fix "Headers already sent" error in PHP

相關問題