Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP禁止列表制作_Php - Fatal编程技术网

PHP禁止列表制作

PHP禁止列表制作,php,Php,我有两个文件:index.php和ban.php 我想IP禁止任何访问ban.php的人使用index.php 我不能使用mysql(加载问题)或让php写入.httaccess(安全问题) 您将如何解决此问题?创建banlist.txt <? // In ban.php $fp = fopen('banlist.txt','a+'); fwrite($fp,$USER_IP); ?> <? // In index.php $list = file(banlist.txt);

我有两个文件:index.php和ban.php

我想IP禁止任何访问ban.php的人使用index.php

我不能使用mysql(加载问题)或让php写入
.httaccess
(安全问题)

您将如何解决此问题?

创建banlist.txt

<?
// In ban.php
$fp = fopen('banlist.txt','a+');
fwrite($fp,$USER_IP);
?>

<?
// In index.php
$list = file(banlist.txt);
if(in_array($USER_IP,$list)){
    header('Location:ban.php');
    die();
}
?>

首先,您需要将IP地址添加到可以从两个文件访问的文件中,例如:

ban.php

<?php
$ipAddress = $_SERVER['REMOTE_ADDR'];   // get the ip
$list  = file_get_contents('ipbans.txt');
$list .= $ipAddress . '\n';  
file_put_contents('ipbans.txt', $list);
<?php
$file = file('ipbans.txt');
foreach ($file as $line)
{
    if ($_SERVER['REMOTE_ADDR'] == $line) {
        die; // or header('Location:') or something...
    }
}

您可以写入任何文件吗?使用会话如何?快速而肮脏:在ban.php中写入cookie,在index.php中读取cookie,如果cookie为setSession,则重定向。您不能使用数据库,不能使用
。htaccess
,您能编写php代码吗?这可以接受吗?写入文件是允许的,cookie是不好的-因为我们也将禁止机器人,会话时间太短,编写PHP代码是允许的。虽然这会起作用(与vanneto的解决方案相同),但一旦列表变长到几千条记录,它的伸缩性就不会很好(每个请求都有数千个字符串比较,不管是否被禁止)。相反,如果不使用file(),您可以执行简单的字符串搜索:$is_banked=(strpos(file_get_contents(“banlist.txt”),$USER_IP)!==false);…干杯