2

I have used this code to open whatismyipaddress.com

$fp = fsockopen("whatismyipaddress.com", 80, $errno, $errstr, 5);

if ($fp) {
    $url = "/";

    fputs($fp, "GET $url HTTP/1.1\r\nHost: {whatismyipaddress.com}\r\nConnection: close\r\n\r\n");
    $resp = '';

    while(!feof($fp)) {
        $resp .= fgets($fp, 1024);
    }

    echo "$resp";
}

and i always can see this error

HTTP/1.1 301 Moved Permanently Date: Tue, 29 Nov 2011 20:19:36 GMT Server: Apache/2.2.17 (Unix) DAV/2 Location: http://whatismyipaddress.com/ MS-Author-Via: DAV Content-Length: 0 Connection: close Content-Type: text/html

Also i have used this code to open whatismyipaddress.com/proxy-check

$fp = fsockopen("whatismyipaddress.com", 80, $errno, $errstr, 5);

if ($fp) {
    $url = "/proxy-check";

    fputs($fp, "GET $url HTTP/1.1\r\nHost: {whatismyipaddress.com}\r\nConnection: close\r\n\r\n");
    $resp = '';

    while(!feof($fp)) {
        $resp .= fgets($fp, 1024);
    }

    echo "$resp";
}

and have this error

HTTP/1.1 404 Not Found Date: Tue, 29 Nov 2011 20:32:07 GMT Server: Apache/2.2.17 (Unix) DAV/2 Content-Length: 421 Connection: close Content-Type: text/html; charset=iso-8859-1 Not Found

The requested URL /proxy-check was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request. Apache/2.2.17 (Unix) DAV/2 Server at {whatismyipaddress.com} Port 80

I'm sure, there is no any problem with the codes. i have tested it with many sites and i didn't get any problem

Please can anyone explain this problem ?

Thank you.

Maroman
  • 316
  • 1
  • 5
  • 13

2 Answers2

2

I would try to use another user-agent in headers. I'm sure they are blocking robots using some sort of header based protection.

Using curl:

$ curl -I 'http://whatismyipaddress.com'
HTTP/1.1 403 Forbidden
Date: Tue, 29 Nov 2011 20:48:28 GMT
Server: Apache/2.2.17 (Unix) DAV/2
Vary: Accept-Encoding
Connection: close
Content-Type: text/html; charset=iso-8859-1

However, once trying a forced user agent it works:

$ curl -I -A 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:8.0.1) Gecko/20100101 Firefox/8.0.1' 'http://whatismyipaddress.com'
HTTP/1.1 200 OK
Date: Tue, 29 Nov 2011 20:49:24 GMT
Server: Apache/2.2.17 (Unix) DAV/2
Set-Cookie: pt=f737a9bb1a119dcec75073f11b05d213; expires=Wed, 30-Nov-2011 20:49:24 GMT
MS-Author-Via: DAV
Vary: Accept-Encoding
Content-Type: text/html
u.k
  • 3,091
  • 1
  • 20
  • 23
  • This is correct, they block clients they think are bots such as curl, lynx, or requests with no user agent header. He needs to spoof the request using a valid user agent from Firefox, Chrome, IE etc. – drew010 Nov 29 '11 at 20:51
  • Thank you very very much Uzi & drew010 My Best Regards. – Maroman Nov 29 '11 at 21:26
1

Basically your script works fine. There are some errors. One is specific to HTTP, see this line of code:

fputs($fp, "GET $url HTTP/1.1\r\nHost: {whatismyipaddress.com}\r\nCon ...
                                       ^                     ^

Remove those brackets, the HTTP protocol does not have any of those there, you need to provide a valid hostname. Solution:

fputs($fp, "GET $url HTTP/1.1\r\nHost: whatismyipaddress.com\r\nCon ...

Then the remote site will tell you need a user agent. Add it as an additional header:

fputs($fp, "GET $url HTTP/1.1\r
Host: whatismyipaddress.com\r
Connection: close\r
User-Agent: Florian der Fensterputzer\r\n\r\n");

This will do it. Demo.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • Thank you very very much hakre My Best Regards. – Maroman Nov 29 '11 at 21:27
  • Guys please wait...don't leave me – Maroman Nov 29 '11 at 21:39
  • The problem with "Advanced Proxy Check" whatismyipaddress.com/proxy-check this page will check the hosting DNS IP not REMOTE_ADRR for the visitor.Deos it possible to do that ? – Maroman Nov 29 '11 at 21:48
  • To get the IP address of your visitor (better the computer which accesses your script, e.g. a proxy), you can use `$_SERVER['REMOTE_ADDR']`. Anyway that's only technical detail, compare with [How to show different homepage based on the user's Country?](http://stackoverflow.com/q/2122169/367456). The service you are using (http://whatismyipaddress.com) is IMHO fooling it's visitors, so I won't offer any support for them. – hakre Nov 29 '11 at 22:54
  • I just wish to use there service to detect the visitor who using aproxy..i think this not possible by fsockopen right ? – Maroman Nov 29 '11 at 23:06
  • You can't provide an IP address in form of a parameter as far as I can see, so it's not possible by `whatismyipadress.com`. `fsockopen` is only a function to open a socket and do a HTTP request, same as comparably `file_get_contents` or some curl stuff. So this is more related to the HTTP service you're using than the concrete function you use to use the HTTP service. – hakre Nov 29 '11 at 23:09
  • That is ok..Thank you hakre :) – Maroman Nov 29 '11 at 23:11
  • Just to clarify: `$_SERVER['REMOTE_ADDR']` will contain the address you'd like to check, but the `wimipadrr.com/proxy-check` HTTP Service does not provide an IP parameter to make it particularly check for the IP address you like it to check (at least I guess so, I didn't really elaborated that service, as written I don't like to support them). – hakre Nov 29 '11 at 23:15
  • yes, i understand your point..by the way i have found something here http://php.net/manual/en/function.fsockopen.php maybe i can connect by fsockopen by proxy ip and port :) and i will add the proxy ip $_SERVER['REMOTE_ADDR']..i should try – Maroman Nov 29 '11 at 23:21
  • Sure you can try to trick their script by mimicking "fakre" proxy headers. This probably works ;) – hakre Nov 29 '11 at 23:31