0

我根本不是 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 行遇到错误:Parse error: syntax error, unexpected T_ELSEIF in /home/public_html/index.php on line 16

任何人都可以帮忙吗?还想我应该以某种方式将它包装在一个函数中,因为它重复了......

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;
    }
4

2 回答 2

2
<?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');
}
于 2013-01-30T14:16:11.953 回答
0

你不能有一个elseifafter else

<?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;
}
于 2013-01-30T13:55:06.387 回答