tags:

views:

40

answers:

2

I want to change the IP of every request in a socket connection in php.(to prevent the host from blocking my ip if I sent large number of requests) Iam attaching my code below

function httpGet( $url, $followRedirects=true ) {
global $final_url;
$url_parsed = parse_url($url);
if ( empty($url_parsed['scheme']) ) {
    $url_parsed = parse_url('http://'.$url);
}
$final_url = $url_parsed;

$port = $url_parsed["port"];
if ( !$port ) {
    $port = 80;
}
$rtn['url']['port'] = $port;

$path = $url_parsed["path"];
if ( empty($path) ) {
    $path="/";
}
if ( !empty($url_parsed["query"]) ) {
    $path .= "?".$url_parsed["query"];
}
$rtn['url']['path'] = $path;

$host = $url_parsed["host"];
$foundBody = false;

$out = "GET $path HTTP/1.0\r\n";
$out .= "Host: $host\r\n";
$out .= "User-Agent:      Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0\r\n";
$out .= "Connection: Close\r\n\r\n";

if ( !$fp = @fsockopen($host, $port, $errno, $errstr, 30) ) {
    $rtn['errornumber'] = $errno;
    $rtn['errorstring'] = $errstr;

}
fwrite($fp, $out);
while (!@feof($fp)) {

    $s = @fgets($fp, 128);
    if ( $s == "\r\n" ) {
        $foundBody = true;
        continue;
    }
    if ( $foundBody ) {
        $body .= $s;
    } else {
        if ( ($followRedirects) && (stristr($s, "location:") != false) ) {
            $redirect = preg_replace("/location:/i", "", $s);
            return httpGet( trim($redirect) );
        }
        $header .= $s;
    }
}

fclose($fp);

return(trim($body));

}

A: 

When you're using TCP, you can't change the sender IP: If you specify an address that doesn't belong to you, any answers will be routed to that IP and never reach your computer.

Therefore, you can only change the sender IP if you don't expect any answer. With TCP, you have to receive an answer (the SYN-ACK) before the connection is initiated. In any case, packets from an IP that doesn't belong to you will be blocked by most, if not any ISPs.

phihag
+2  A: 

You can't change (spoof) the IP address that you send it with. However, you can use a proxy like SOCKS4/5 or a HTTP proxy with the PHP extension cURL. That would result in the desired effect.

However, note that you should first gather more knowledge about how a proxy works before trying random stuff with it (especially with code like what you pasted, which isn't anything you want to do with PHP5 anymore).

Patrick Daryll Glandien
so what is a good replacement for this function?file_get_contents??does it support redirection?
Ashwin
As said if you wanna use a proxy use cURL or PECLs HttpRequest
Patrick Daryll Glandien