php如何模拟不同ip访问,下面web建站小编给大家详细介绍一下具体代码!
1、新建server.php文件,新增以下代码:
复制代码<?php
$client_ip = getip();
$referer = getreferer();
$allow_ip = '192.168.1.120';
$allow_referer = 'https://ipkd.cn/';
if($client_ip==$allow_ip && strpos($referer, $allow_referer)===0){
echo 'allow access';
}else{
echo 'deny access';
}
// 获取访问者ip
function getip(){
if(!empty($_SERVER['HTTP_CLIENT_IP'])){
$cip = $_SERVER['HTTP_CLIENT_IP'];
}elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
$cip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}elseif(!empty($_SERVER['REMOTE_ADDR'])){
$cip = $_SERVER['REMOTE_ADDR'];
}else{
$cip = '';
}
return $cip;
}
// 获取访问者来源
function getreferer(){
if(isset($_SERVER['HTTP_REFERER'])){
return $_SERVER['HTTP_REFERER'];
}
return '';
}
?>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
2、curl模拟方法:
复制代码<?php
function doCurl($url, $data=array(), $header=array(), $referer='', $timeout=30){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
// 模拟来源
curl_setopt($ch, CURLOPT_REFERER, $referer);
$response = curl_exec($ch);
if($error=curl_error($ch)){
die($error);
}
curl_close($ch);
return $response;
}
// 调用
$url = 'https://www.example.com/server.php';
$data = array();
// 设置IP
$header = array(
'CLIENT-IP: 192.168.1.120',
'X-FORWARDED-FOR: 192.168.1.120'
);
// 设置来源
$referer = 'https://ipkd.cn/';
$response = doCurl($url, $data, $header, $referer, 5);
echo $response;
?>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
php语法对index.php首页进行判断(根据需要显示不同页面)
上面是“如何利用php语法模拟不同ip访问”的全面内容,想了解更多关于 php入门 内容,请继续关注web建站教程。
当前网址:https://m.ipkd.cn/webs_3372.html
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!