如果白名单IP不存在,PHP检查cookie(获取T_ELSEIF解析错误)

时间:2013-01-30 13:53:22

标签: php wordpress

我根本不是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;
    }

2 个答案:

答案 0 :(得分: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');
}

答案 1 :(得分:0)

elseif之后你不能拥有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;
}