du127953 2013-01-30 13:53
浏览 14
已采纳

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

I'm not a PHP developer at all, this is a script provided by a theme to restrict access to whitelisted IP addresses. I'm hoping to amend it so that it if a whitelisted IP exists, it loads WordPress (as it does now), else if the IP does not exist in the array, it checks for a cookie. If the cookie exists (passedSecurityTT) show WordPress and lastly, if both do not exist - redirect to /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');
}

But I'm getting an error on Line 16: Parse error: syntax error, unexpected T_ELSEIF in /home/public_html/index.php on line 16

Can anyone help? Also thinking I should wrap this in a function somehow, as it's repeated...

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条回答 默认 最新

  • dotwc62080 2013-01-30 14:16
    关注
    <?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条)

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示