Issue
I have a PHP script that runs from our server's non-publicly-accessible directory as a cron task. The script sends cURL POSTs to a publicly-accessible URL.
I set up the receiving URL with a .htaccess file that denies access from any IP address other than the ones I allowed. I ran a PHP script that emailed me the output of a file_get_contents("http://www.whatismyipaddress.com");
to get my server's origin IP address (it's a dedicated server so I don't expect it to change). That's the IP I allowed in the .htaccess file.
But when I ran the script through an SSH prompt, all of the POSTs returned 403 Forbidden errors. When I commented out the .htaccess protections, the POSTs succeeded.
Then I looked at my log files and it turned out the POSTs were coming from a different IP than what was reported by my file_get_contents()
. It was easy enough to add that IP to the .htaccess file, which fixed the problem.
But I'm confused as to why there are two different origin IP addresses. Can anyone shed some light on the subject?
Solution
First of all, that's a weird way to find out your server's IP address. Tried these?
PHP how to get local IP of system
Second: Do you know if the server is behind a firewall/NAT or something else that redirects traffic? It could be that outgoing traffic from your server via the webserver is allowed, but traffic when called from the command line (different PHP process) goes another route through a proxy so that you end up with 2 different IP addresses in your destination server.
Edit: Clarified something
Edit2: Try this:
On your publicly accessible server, set up a script i.php
with the following content:
<?php die($_SERVER['REMOTE_ADDR']); ?>
Then, on your private server that runs cron, make a script like that:
<?php echo file_get_contents("http://publicserver.com/i.php"); ?>
Run it from your browser. Then run it from the shell via SSH. Then, via SSH, execute the following command:
curl http://publicserver.com/i.php
Compare output. Is it all the same?
Answered By - ntaso Answer Checked By - Marilyn (WPSolving Volunteer)