我根本不是PHP开发人员,这是一个主题提供的脚本,用于限制对白名单IP地址的访问。我希望修改它,以便如果存在白名单 IP,它会加载 WordPress(就像现在一样),否则如果数组中不存在该 IP,它会检查 cookie。如果 cookie 存在(passedSecurityTT),则显示 WordPress,最后,如果两者都不存在 - 重定向到/login.php。
<?php
$whitelist = array('111.222.333.444', '111.222.333.445');
if (in_array($_SERVER['REMOTE_ADDR'], $whitelist)) {
define('WP_USE_THEMES', true);
/** Loads the WordPress Environment and Template */
require('./wp-blog-header.php');
} else {
//Action for all other IP Addresses
echo 'You are not authorized here.';
echo "<br />IP Address: ".$_SERVER['REMOTE_ADDR'];
exit;
}
elseif (!isset($_COOKIE['passedSecurityTT'])) {
define('WP_USE_THEMES', true);
/** Loads the WordPress Environment and Template */
require('./wp-blog-header.php');
} else {
//Action for all other IP Addresses
echo 'You are not authorized here.';
echo "<br />IP Address: ".$_SERVER['REMOTE_ADDR'];
exit;
}
else {
header('Location: /login.php');
}
但是我在第 16 行收到错误:解析错误:语法错误,第 16 行/home/public_html/index.php 中的意外T_ELSEIF
谁能帮忙?还认为我应该以某种方式将其包装在一个函数中,因为它是重复的......
define('WP_USE_THEMES', true);
/** Loads the WordPress Environment and Template */
require('./wp-blog-header.php');
} else {
//Action for all other IP Addresses
echo 'You are not authorized here.';
echo "<br />IP Address: ".$_SERVER['REMOTE_ADDR'];
exit;
}
<?php
$whitelist = array('111.222.333.444', '111.222.333.445');
// check if remote address is in whitelist OR if cookie exists(this is not the best security practice! @FIXME
if (in_array($_SERVER['REMOTE_ADDR'], $whitelist) || isset($_COOKIE['passedSecurityTT'])) ) {
define('WP_USE_THEMES', true);
/** Loads the WordPress Environment and Template */
require('./wp-blog-header.php');
} else {
header('Location: /login.php');
}
else
后不能有elseif
:
<?php
$whitelist = array('111.222.333.444', '111.222.333.445');
if (in_array($_SERVER['REMOTE_ADDR'], $whitelist)) {
define('WP_USE_THEMES', true);
/** Loads the WordPress Environment and Template */
require('./wp-blog-header.php');
} else {
//Action for all other IP Addresses
echo 'You are not authorized here.';
echo "<br />IP Address: ".$_SERVER['REMOTE_ADDR'];
header('Location: /login.php');
exit;
}