Issue
Developing a plugin for WordPress locally I'm wanting to implement Akismet in form validation but I'm required an IP address with the submission and when I run:
function check_ip_address() {
if (isset($_SERVER['REMOTE_ADDR'])) :
$ip_address = $_SERVER['REMOTE_ADDR'];
else :
$ip_address = "undefined";
endif;
return $ip_address;
}
echo check_ip_address();
I get back a ::1
. When I researched to resolve this I didn't find a solid answer from:
When researching how to resolve ::1
I found Should a MAMP return ::1 as IP on localhost? that suggests a sudo
of:
sudo vi /etc/apache2/httpd.conf
So I go to MAMP/conf/apache/httpd.conf
and try to modify line 48 from Listen 8888
to Listen 127.0.0.1
and I get an error and Apache will not restart. How can I modify my MAMP IP so I can get a proper IP from $_SERVER['HTTP_USER_AGENT']
?
Solution
It's ::1
because that's the IPv6 loopback address, equivalent of 127.0.0.1
, and the remote address is yourself as MAMP is running locally, the remote browser is on the same machine.
REMOTE_ADDR
represents the IP the request came from. In most scenarios this is the same as the IP the browsers machine has on the open-internet, but here MAMP is running Apache natively so it's 127.0.0.1
or ::1
. If you were using a docker container or a Virtual machine, it would be a private IP on a range specified when configuring your containers/VMs.
So to retrieve the IP you're expecting, you'll need to use an external service, or, for the sake of debugging, pass Akismet a hardcoded IP, but I suspect it's asking for the IP of whomever is commenting.
Answered By - Tom J Nowell
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.