原貼 百度站長推送php,獲取隨機(jī)100條網(wǎng)址并推送 - 經(jīng)驗(yàn)分享
寫的很不錯,我只是借花獻(xiàn)佛,在原貼的代碼基礎(chǔ)上,增加了網(wǎng)站安全,防止SQL注入,對代碼進(jìn)行優(yōu)化
geturl.php的代碼
<?php
// 添加錯誤報告
error_reporting(E_ALL);
ini_set('display_errors', 1);
$config = include("application/database.php");
// 添加連接錯誤處理
$conn = mysqli_connect($config['hostname'], $config['username'], $config['password'], $config['database']);
if (!$conn) {
die("數(shù)據(jù)庫連接失敗: " . mysqli_connect_error());
}
mysqli_query($conn, "SET NAMES UTF8");
// 使用預(yù)處理語句
$stmt = mysqli_prepare($conn, "SELECT aid, typeid FROM ey_archives WHERE author != '' ORDER BY RAND() LIMIT 100");
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$urls = [];
while ($row = mysqli_fetch_array($result)) {
$typeid = $row['typeid'];
// 使用預(yù)處理語句
$stmt2 = mysqli_prepare($conn, "SELECT dirpath FROM ey_arctype WHERE id = ?");
mysqli_stmt_bind_param($stmt2, "i", $typeid);
mysqli_stmt_execute($stmt2);
$result1 = mysqli_stmt_get_result($stmt2);
if ($row1 = mysqli_fetch_row($result1)) {
$urls[] = $web . $row1[0] . '/' . $row['aid'] . '.html';
}
}
?>
tuisong.php的代碼
<?php
include("geturl.php");
$api = 'http://data.zz.baidu.com/urls?site=你的網(wǎng)址&token=你的token';
$ch = curl_init();
$options = array(
CURLOPT_URL => $api,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => implode("\n", $urls),
CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
CURLOPT_TIMEOUT => 30 // 添加超時設(shè)置
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
if ($result === false) {
// 記錄錯誤日志
file_put_contents('push_error.log', date('Y-m-d H:i:s') . ' - ' . curl_error($ch) . "\n", FILE_APPEND);
} else {
// 記錄成功日志
file_put_contents('push_success.log', date('Y-m-d H:i:s') . ' - ' . $result . "\n", FILE_APPEND);
}
curl_close($ch);
echo $result;
?>
上面這兩個PHP代碼,作了一下SQL的防注入,如果放在易優(yōu)網(wǎng)站的目錄,防SQL注入就更為重要了。感謝原貼作者。