我用php编写了一个小脚本,并将其包含在wordpress的page.php中,以便它可以在每个页面上运行。
当前,脚本只有在取消
注释时才运行。echo "连接成功"
我把它注释掉的那一刻,脚本停止工作…
请参见下面的脚本。
<?php
ob_start();
//SQL DATABASE INIT
$servername = "localhost";
$username = "user";
$password = "password";
$dbname = "databasename";
// Create connection
$mysqli = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
//echo "Connected successfully";
//URL RETREIVAL
if (isset($_GET['uid']) && !isset($_GET['valid'])) {
$urlId = $mysqli->mysqli_real_escape_string($_GET['uid']);
$result = $mysqli->query("SELECT * FROM tablename WHERE id= '". $urlId ."'");
if (mysqli_num_rows($result) > 0)
{
$url = "https://thisismywebsite.com/?valid=1&uid=". $_GET['uid'];
header('Location:'. $url);
ob_end_flush();
}
else
{
$url = "https://thisismywebsite.com/?valid=0&uid=". $_GET['uid'];
header('Location:'. $url);
ob_end_flush();
}
} else {
// Fallback behaviour goes here
}
?>
在wordpress中你不需要连接数据库。只需声明全局$wpdb
ob_start();
global $wpdb;
//URL RETREIVAL
if (isset($_GET['uid']) && !isset($_GET['valid'])) {
$urlId =$_GET['uid'];
$result = $wpdb->$wpdb->get_results("SELECT * FROM tablename WHERE
id= '". $urlId ."'");
if (!empty($result))
{
$url = "https://thisismywebsite.com/?valid=1&uid=". $_GET['uid'];
header('Location:'. $url);
ob_end_flush();
}
else
{
$url = "https://thisismywebsite.com/?valid=0&uid=". $_GET['uid'];
header('Location:'. $url);
ob_end_flush();
}
} else {
// Fallback behaviour goes here
}