Example of HTTP_REFERER function usage in php

Source: Internet
Author: User
This article mainly introduces the usage of the HTTP_REFERER function in php, and describes applications such as user identification and forgery in the form of instances, for more information about HTTP_REFERER function usage in php, see the following example. Share it with you for your reference. The specific analysis is as follows:

Using php's http_referer function to determine the user's path is relatively simple. the instance code is as follows:

The code is as follows:

<? Php
If (isset ($ _ SERVER ['http _ referer']) {
Print "The page you were on previusly was {$ _ SERVER ['http _ referer']}
";
} Else {
Print "You didn't click any links to get here
";
}
?>
Click me!


The following is an example of how to handle this problem. The instance code is as follows:

The code is as follows:

<? Php
$ Host = "www.jb51.net ";
$ Referer = "http: //". $ host;
$ Fp = fsockopen ($ host, 80, $ errno, $ errstr, 30 );
If (! $ Fp ){
Echo "$ errstr ($ errno)
; 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 ";
}


That's not enough?

However, it is strange that the webpage www.jb51.net is garbled (except for the http header). why? Is it because gzip is used for compression?

The code is as follows:

<? Php
$ Host = "www.jb51.net ";
$ Html = file_get_contents ("http: //". $ host );
$ Fp = file_put_contents ("hao123.html", $ html );
Echo "done ";
?>;


But there is no problem with the http header:

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

Sure enough, the Content-Encoding: gzip is compressed, with a length of 14567 bytes. The second method is used to capture that the html not compressed is 71143 bytes, the original file_get_contents can also be automatically decompressed.

Php example 2: The code is as follows:

The code is as follows:

<? Php
$ Host = '1970. 0.0.1 ';
$ Target = '/2. php ';
$ Referer = 'http: // www.jb51.net'; // forge the HTTP_REFERER address
$ Fp = fsockopen ($ host, 80, $ errno, $ errstr, 30 );
If (! $ Fp ){
Echo "$ errstr ($ errno)
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 );
}
?>


Another 2. php file is very simple. just write a line of code to read the current HTTP_REFERER server value, as shown below:

The code is as follows:

<? Php
Echo"

";
Echo $ _ SERVER ["HTTP_REFERER"];
?>

I hope this article will help you with php programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.