PHP Script to detect the Ip of the request

Started on the old forum in June 2009 · view the original in the archive · old links redirect here

4 replies · 1,094 views · started 17 years ago · 1 min read

3 participants
taco.de.tripa Newbie · 3 posts #1

Hi guys, I'm developing a wap site and I'd like to detect the IP of the phone to display the content according to the IP so, do you know how I will be able to detect this value using PHP?

Thanks in advance,

Anshul Senior · 296 posts #2
if (isSet($_SERVER['HTTP_X_FORWARDED_FOR'])) $_user_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if(isSet($_SERVER['HTTP_CLIENT_IP'])) $_user_ip = $_SERVER['HTTP_CLIENT_IP'];
else if($_SERVER['REMOTE_ADDR']) $_user_ip = $_SERVER['REMOTE_ADDR'];
else {$_user_ip = 'not detected';}
$_user_ip = preg_replace("/[^0-9.]/", "", $_user_ip);
taco.de.tripa Newbie · 3 posts #3

Anshul wrote:

if (isSet($_SERVER['HTTP_X_FORWARDED_FOR'])) $_user_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if(isSet($_SERVER['HTTP_CLIENT_IP'])) $_user_ip = $_SERVER['HTTP_CLIENT_IP'];
else if($_SERVER['REMOTE_ADDR']) $_user_ip = $_SERVER['REMOTE_ADDR'];
else {$_user_ip = 'not detected';}
$_user_ip = preg_replace("/[^0-9.]/", "", $_user_ip);

Thanks, it did the trick.

something else Fellow · 2,296 posts #4

you may wish to add*

 else if($_user_ip=="unknown")$_user_ip = $_SERVER['REMOTE_ADDR'];
 

for blackberrys*


mmmmmm tasty!

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close