Change request ip address using curl and php

I am using curl to send request from "https://webdesign4sites.com/ipl/curl.php" to "https://webdesign4sites.com/ipl/ip.php" with Ip Address "223.190.135.225"

$url = "https://webdesign4sites.com/ipl/ip.php";
$headers   = array();
$headers[] = 'X-Forwarded-For: 223.190.135.225';
$headers[] = 'CLIENT_IP: 223.190.135.225';
$ch = curl_init($url);
 $options = array(
        CURLOPT_URL => $url,   
        CURLOPT_HTTPHEADER => $headers, 
    );
curl_setopt_array($ch, $options);
$content  = curl_exec($ch);
curl_close($ch);
echo $content;

but i am receiving Ip address is:- 162.0.229.1351

and header is

Array ( [PATH] => /usr/local/bin:/bin:/usr/bin [HTTP_ACCEPT] => */* [HTTP_HOST] => webdesign4sites.com [HTTP_USER_AGENT] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Firefox/91.0 [HTTP_X_FORWARDED_FOR] => 223.190.135.225 [HTTP_CLIENT_IP] => 223.190.135.225 [HTTP_X_FORWARDED_PROTO] => https [HTTP_X_HTTPS] => on [DOCUMENT_ROOT] => /home/magirtzm/webdesign4sites.com [REMOTE_ADDR] => 162.0.229.135 [REMOTE_PORT] => 35110 [SERVER_ADDR] => 162.0.229.135 [SERVER_NAME] => webdesign4sites.com [SERVER_ADMIN] => webmaster@webdesign4sites.magicnumbertrading.com [SERVER_PORT] => 443 [REQUEST_SCHEME] => https [REQUEST_URI] => /ipl/ip.php [PROXY_REMOTE_ADDR] => 162.0.229.135 [HTTPS] => on [SCRIPT_FILENAME] => /home/magirtzm/webdesign4sites.com/ipl/ip.php [QUERY_STRING] => [SCRIPT_URI] => https://webdesign4sites.com/ipl/ip.php [SCRIPT_URL] => /ipl/ip.php [SCRIPT_NAME] => /ipl/ip.php [SERVER_PROTOCOL] => HTTP/1.1 [SERVER_SOFTWARE] => LiteSpeed [REQUEST_METHOD] => GET [X-LSCACHE] => on [PHP_SELF] => /ipl/ip.php [REQUEST_TIME_FLOAT] => 1632499807.0277 [REQUEST_TIME] => 1632499807 )

but when i am opening this url "https://webdesign4sites.com/ipl/ip.php" directly into browser then this time the output is Ip address is:- 223.228.255.121.

Array ( [PATH] => /usr/local/bin:/bin:/usr/bin [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 [HTTP_ACCEPT_ENCODING] => gzip, deflate, br [HTTP_ACCEPT_LANGUAGE] => en-US,en;q=0.5 [HTTP_HOST] => webdesign4sites.com [HTTP_USER_AGENT] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:92.0) Gecko/20100101 Firefox/92.0 [HTTP_CACHE_CONTROL] => max-age=0 [HTTP_X_FORWARDED_FOR] => 223.228.255.121 [HTTP_DNT] => 1 [HTTP_UPGRADE_INSECURE_REQUESTS] => 1 [HTTP_SEC_FETCH_DEST] => document [HTTP_SEC_FETCH_MODE] => navigate [HTTP_SEC_FETCH_SITE] => none [HTTP_SEC_FETCH_USER] => ?1 [HTTP_TE] => trailers [HTTP_X_FORWARDED_PROTO] => https [HTTP_X_HTTPS] => on [DOCUMENT_ROOT] => /home/magirtzm/webdesign4sites.com [REMOTE_ADDR] => 223.228.255.121 [REMOTE_PORT] => 42954 [SERVER_ADDR] => 162.0.229.135 [SERVER_NAME] => webdesign4sites.com [SERVER_ADMIN] => webmaster@webdesign4sites.magicnumbertrading.com [SERVER_PORT] => 443 [REQUEST_SCHEME] => https [REQUEST_URI] => /ipl/ip.php [PROXY_REMOTE_ADDR] => 162.0.229.135 [HTTPS] => on [SCRIPT_FILENAME] => /home/magirtzm/webdesign4sites.com/ipl/ip.php [QUERY_STRING] => [SCRIPT_URI] => https://webdesign4sites.com/ipl/ip.php [SCRIPT_URL] => /ipl/ip.php [SCRIPT_NAME] => /ipl/ip.php [SERVER_PROTOCOL] => HTTP/1.1 [SERVER_SOFTWARE] => LiteSpeed [REQUEST_METHOD] => GET [X-LSCACHE] => on [PHP_SELF] => /ipl/ip.php [REQUEST_TIME_FLOAT] => 1632499883.4503 [REQUEST_TIME] => 1632499883 )

On "https://webdesign4sites.com/ipl/ip.php" the code is:-

$ip_address = ip_address();

echo "Ip address is:- " . $ip_address;

function ip_address()
{
    $mainIp = '';
    if (getenv('HTTP_CLIENT_IP'))
        $mainIp = getenv('HTTP_CLIENT_IP');
    else  if (getenv('HTTP_X_FORWARDED_FOR'))
        $mainIp = getenv('HTTP_X_FORWARDED_FOR');
    else if (getenv('HTTP_X_FORWARDED'))
        $mainIp = getenv('HTTP_X_FORWARDED');
    else if (getenv('HTTP_FORWARDED_FOR'))
        $mainIp = getenv('HTTP_FORWARDED_FOR');
    else if (getenv('HTTP_FORWARDED'))
        $mainIp = getenv('HTTP_FORWARDED');
    else if (getenv('REMOTE_ADDR'))
        $mainIp = getenv('REMOTE_ADDR');
    else
        $mainIp = 'UNKNOWN';
    return $mainIp;
}

I want that when i send request using curl i receive ip address is:- 223.228.255.12

Answer

Solution:

You are trying to spoof the IP address of the client using curl. This is not possible: https://curl.se/docs/faq.html#Can_I_make_libcurl_fake_or_hide

Your script is not working because it first evaluate HTTP_CLIENT_IP. At this point it get the ip from where curl is being executed (same server you are hosting your ip.php script).

Although not possible, you could test some approaches if you don't care about the reliability of the results: https://stackoverflow.com/a/1301417/1245926

Source