0
我有一個php api終端如下。如何在php中保護數據庫配置
我需要改變這樣的:
需要包括所有在單獨的文件中使用服務器令牌
驗證API請求,以確保只接受真正的請求的配置
捕獲單獨文件中的所有錯誤日誌,而不是在瀏覽器中顯示
這是我的代碼:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$data = json_decode(file_get_contents('php://input'), true);
if(!empty($data)):
header('Content-Type:text/plain');
/*MYSQL CREDENTIALS*/
$hostname = 'localhost';
$username = 'root';
$password = '';
$dbname = 'mydb';
$dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
$arraykey=array_keys($data);
$array=$data[$arraykey[0]];
try
{
foreach($data as $array)
{
//MYSQL execute
$count = $dbh->exec("INSERT INTO gmr(version,visitorId,dwellTime,poiId,srId,zoneId,poiProximityConfidence,zoneProximityConfidence,poiPresenceConfidence,zonePresenceConfidence,normalizedTime) VALUES ('" . implode("', '", $array) . "')") or die(print_r($dbh->errorInfo(), true));
echo count($data);
echo 'Data Successfully inserted!!<br />';
}
//echo $data;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
endif;
?>
對於點1)我已經把所有的configs在一個單獨的文件,如:
<?php
define (DB_USER, "root");
define (DB_PASSWORD, "");
define (DB_DATABASE, "mydb");
define (DB_HOST, "localhost");
?>
需要的更好的方式清晰,以便在此配置文件我的主文件。
由於im將API端點共享給客戶端,主文件應該能夠讀取我的db配置文件。
因此,這是建議使用:對於其他2分
3)您可以使用PHP [set_error_handler](http://php.net/manual/en/function.set-error-handler.php)。 –