首页 > 编程技术 > php

php HTTP_REFERER函数的使用用法

发布时间:2016-11-25 16:48

利用php教程的http_referer函数来判断用户的来路,这是简单了,
实例

<?php
            if (isset($_SERVER['HTTP_REFERER'])) {
                    print "The page you were on previously was {$_SERVER['HTTP_REFERER']}<br />";
            } else {
                    print "You didn't click any links to get here<br />";
            }
    ?>

    <a href="refer.php">Click me!</a>

下面我们让用户不知道我们的来路处理
实例

]<?php
$host = "www.123cha.com";
$referer = "http://".$host;
$fp = fsockopen ($host, 80, $errno, $errstr, 30);
if (!$fp){
        echo "$errstr ($errno)<br>;n";
}else{
$request = "
GET / HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */"."*
Referer: http://$host
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
Host: $host
Connection: Close"
."rnrn";


fputs ($fp, "$request");
while (!feof($fp))
{
   $res[] = fgets($fp,1024);
}
$html = join("",$res);
fclose ($fp);
$fp = file_put_contents("123cha.html",$html);
echo "done";
}[/code]

这不就行了?

不过很奇怪的是,
www.hao123.com
的页面抓下来是乱码(除了http头),这是为什么?难道是因为用了gzip之类压缩?

[code]<?php
$host = "www.111cn.net";
$html = file_get_contents("http://".$host);
$fp = file_put_contents("hao123.html",$html);
echo "done";
?>;[/code]


但这样抓的就没问题.

再来分析开始抓的http头
[code]HTTP/1.1 200 OK Date: Wed, 31 Aug 2005 00:59:36 GMT Server: Apache/1.3.27 Cache-Control: max-age=1296000 Expires: Thu, 15 Sep 2005 00:59:36 GMT Last-Modified: Mon, 29 Aug 2005 13:56:00 GMT Accept-Ranges: bytes Connection: close Content-Type: text/html Content-Encoding: gzip Content-Length: 14567[/code]

果然有这句,Content-Encoding: gzip
原来压缩了的,长度14567字节了,
用第二种方法抓,原来没压缩的html是71143字节,原来file_get_contents还可以自动解压缩.


实例二

<?php
$host = '127.0.0.1';
$target = '/2.php';
$referer = 'http://www.111cn.net'; //伪造HTTP_REFERER地址
$fp = fsockopen($host, 80, $errno, $errstr, 30);
if (!$fp){
echo "$errstr($errno)<br />n";
}
else{
$out = "
GET $target HTTP/1.1
Host: $host
Referer: $referer
Connection: Closernrn";
fwrite($fp, $out);
while (!feof($fp)){
echo fgets($fp, 1024);
}
fclose($fp);
}
?>

    另一个2.php文件很简单,只是写上一行读取当前的HTTP_REFERER服务器值的代码即可,如下:

<?php
echo "<hr />";
echo $_SERVER["HTTP_REFERER"];
?>

php教程 ereg()把htm转换成xml文件实现方法

语法: int ereg(string pattern, string string, array [regs]);

返回值: 整数/数组

 

<?php

$text = "<p>This is some text here "</p>".</p>";
ereg("<p>(([^<"]|[^<]*<[^/][^<])*("[^"]*"([^<"]|[^<]*<[^/][^<])*)*)?</p>", $text, $matches);
echo "Found text: " . $matches[1] . "n";
?>

 

内容说明


本函数以 pattern 的规则来解析比对字符串 string。比对结果返回的值放在数组参数 regs 之中,regs[0] 内容就是原字符串 string、regs[1] 为第一个合乎规则的字符串、regs[2] 就是第二个合乎规则的字符串,余类推。若省略参数 regs,则只是单纯地比对,找到则返回值为 true。

var_export -- 输出或返回一个变量的字符串表示
描述
mixed var_export ( mixed expression [, bool return])


此函数返回关于传递给该函数的变量的结构信息,它和 var_dump() 类似,不同的是其返回的表示是合法的 PHP 代码。

您可以通过将函数的第二个参数设置为 TRUE,从而返回变量的表示。

比较 var_export() 和 var_dump().

 

<?php教程
  $languages = array( 'French', 'German', 'Russian', 'Chinese',
                      'Hindi', 'Quechua', 'Spanish', 'Hausa');
  printf("<pre>Original array:n%s</pre>n", var_export($languages, TRUE));

  $slice1 = array_slice($languages, 2, 4);
  printf("<pre>Slice 1:n%s</pre>n", var_export($slice1, TRUE));

  $slice2 = array_slice($languages, 2, 4, TRUE);
  printf("<pre>Slice 2:n%s</pre>n", var_export($slice2, TRUE));

  $slice3 = array_slice($languages, -6, -2, TRUE);
  printf("<pre>Slice 3:n%s</pre>n", var_export($slice3, TRUE));

  $last3 = array_slice($languages, -3);
  printf("<p>Last 3: %s</p>n", var_export($last3, TRUE));

  $last3 = array_slice($languages, -3, 3, TRUE);
  printf("<p>Last 3: %s</p>n", var_export($last3, TRUE));
?>

<?php教程 echo $_SERVER["HTTP_USER_AGENT"]; ?>
该脚本的输出可能是:
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
PHP 有很多种不同类型的变量。在以上例子中我们打印了一个数组的元素。数组是一类非常有用的变量。
$_SERVER 只是 PHP 自动全局化的变量之一。您可以查阅“保留变量”一节来查看这些变量的列表,或者也可以建立如下的文件来得到一个完整的列表:

php 根据HTTP_USER_AGENT 判断用户浏览器类型

function browsers(){
   global $HTTP_USER_AGENT ;
   if (isset($HTTP_USER_AGENT)){
    $sAgent = $HTTP_USER_AGENT;
   }else{
    $sAgent = $_SERVER['HTTP_USER_AGENT'];
   }
   if (strpos($sAgent,'MSIE') !== false && strpos($sAgent,'mac') === false && strpos($sAgent,'Opera') === false){
    $iVersion = (float)substr($sAgent,strpos($sAgent,'MSIE') + 5,3);
    return ($iVersion >= 5.5) ;
   }else if (strpos($sAgent,'Gecko/') !== false){
    $iVersion = (int)substr($sAgent,strpos($sAgent,'Gecko/') + 6,8);
    return ($iVersion >= 20030210) ;
   }else{
    return false;
   }
  }


可以得到链接/提交当前页的父页面URL

<?php
            if (isset($_SERVER['HTTP_REFERER'])) {
                    print "The page you were on previously was {$_SERVER['HTTP_REFERER']}<br />";
            } else {
                    print "You didn't click any links to get here<br />";
            }
    ?>

    <a href="refer.php">Click me!</a>

php教程 自定字符串中的部分字符进行替换方法

substr_replace()函数对自定字符串中的部分字符进行替换
   语法:substr_replace(str,repl,start,[int length])
   语法说明:
   str——必选参数,指定要操作的原始字符串
   repl——指定替换后的新字符串
   start——指定替换字符串的开始位置
   length——可选参数,指定返回字符串的长度。
   使用substr_replace()函数替换字符串的应用实例:

实例代码:
<?php
$b=”手册”;
$c=”zero的php博客”;
echo substr_replace($c,$b,9,4);
?>
输出结果:
zero的php手册

参数 描述
string 必需。规定要检查的字符串。
replacement 必需。规定要插入的字符串。
start

必需。规定在字符串的何处开始替换。

  • 正数 - 在第 start 个偏移量开始替换
  • 负数 - 在从字符串结尾的第 start 个偏移量开始替换
  • 0 - 在字符串中的第一个字符处开始替换
charlist

可选。规定要替换多少个字符。

  • 正数 - 被替换的字符串长度
  • 负数 - 从字符串末端开始的被替换字符数
  • 0 - 插入而非替换

标签:[!--infotagslink--]

您可能感兴趣的文章: