我想將我的基本授權與我的MySQL數據庫連接起來。實際上,我正在PHP中開發一個登錄REST API。目前,我在PHP授權腳本中硬編碼了我的用戶名和密碼,但我想連接到MySQL並使用基本授權的數據庫驗證用戶名和密碼。如何使用PHP中的基本認證連接SQL查詢
這裏是我的代碼:
<?php
// User name and password for authentication
$username = 'admin';
$password = 'password';
if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) ||
($_SERVER['PHP_AUTH_USER'] != $username) || ($_SERVER['PHP_AUTH_PW'] != $password)) {
// The user name/password are incorrect so send the authentication headers
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="Guitar Wars"');
exit('<h2>Guitar Wars</h2>Sorry, you must enter a valid user name and
password to access this page.');
}
?>
而且我想嵌入這樣的查詢:
$query = mysql_query("SELECT username FROM login WHERE username='$username' and password='$password'");
mysql_ *已被棄用嘗試使用mysqli_ *準備語句或PDO爲您迴應 – JYoThI