<?php
$allow = array("xxx.xx.xxx.xxx","xxx.xx.xxx.xxx"); //allowed IPs
if(!in_array($_SERVER[\'REMOTE_ADDR\'], $allow) && !in_array($_SERVER["HTTP_X_FORWARDED_FOR"], $allow)) { ?>
<div class="enclose comn-pg">
<h1 align="center" style="padding:10% 0;">Access denied. This page is accessible only to customers.</h1>
</div>
<?php } else { ?>
<div class="enclose comn-pg">
<div id="primary">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( \'content\', \'page\' ); ?>
<?php comments_template( \'\', true ); ?>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
</div>
<?php
}
?>
我尝试使用上述代码将内容仅显示到允许的IP地址,但它不起作用。有人能建议我如何限制内容吗?
最合适的回答,由SO网友:lalitpendhare 整理而成
我已经尝试了你的代码,它对我来说工作得很好。
<?php
$allow = array("xxx.xx.xxx.xxx","xxx.xx.xxx.xxx"); //allowed IPs
if(!in_array($_SERVER[\'REMOTE_ADDR\'], $allow) && !in_array($_SERVER["HTTP_X_FORWARDED_FOR"], $allow) && !in_array($_SERVER["HTTP_CLIENT_IP"], $allow)) { ?>
<div class="enclose comn-pg">
<h1 align="center" style="padding:10% 0;">Access denied. This page is accessible only to customers.</h1>
</div>
<?php } else { ?>
<div class="enclose comn-pg">
<div id="primary">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( \'content\', \'page\' ); ?>
<?php comments_template( \'\', true ); ?>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
</div>
<?php
}
?>