我有這個問題最近工作的一個項目。這使我心中唯一的事情就是:
"**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
把出口AFTER頭重定向 – Mihai
請使用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)或在哈希之前使用其他任何清理機制。這樣做*更改密碼並導致不必要的附加編碼。 –
僅使用帶有退出的整數值。 試試Mihai的回答 –