Welcome to Dream.In.Code
Become a PHP Expert!

Join 137,391 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,138 people online right now. Registration is fast and FREE... Join Now!




Getting the visitor's IP

2 Pages V  1 2 >  
Reply to this topicStart new topic

Getting the visitor's IP

duffsstuff
11 Oct, 2006 - 03:40 PM
Post #1

D.I.C Head
**

Joined: 10 Sep, 2006
Posts: 67


My Contributions
So I am now working on a small script to echo a URL to a Administration page If the Remote Address is 71.134.229.249. This is what I have so far:

CODE

<?php
      if (['REMOTE_ADDR'])==(['71.134.229.249')
          echo "<a href="#">Administration</a>"
  ?>


1) I don't know how to compare the remote Address to the corresponding one
2) I don't think I did the link is right in the "echo" line
3) Do I need an else variable?

- Thanks biggrin.gif

This post has been edited by hotsnoj: 11 Oct, 2006 - 05:40 PM
User is offlineProfile CardPM
+Quote Post

snoj
RE: Getting The Visitor's IP
11 Oct, 2006 - 05:47 PM
Post #2

$Null
Group Icon

Joined: 31 Mar, 2003
Posts: 3,304



Thanked: 7 times
Dream Kudos: 700
My Contributions
Well first off, that syntax will never run.

The $_SERVER['REMOTE_ADDR'] variable is the ip address of the computer making the request (so it could very well be a proxy ip you get). While this "authentication" is easy, it isn't entirely safe either. Because now ANYONE using your computer can access the administration without a username or password. And conversely, if you're on dialup, any other customer could get access. And there is also the point that you can't log in on someone else's computer (like a friends or at the library) and administrate.

Anyway, fixed code.
CODE
<?php
      if ($_SERVER['REMOTE_ADDR']=='71.134.229.249') {
          echo "<a href="#">Administration</a>";
      }
  ?>

User is offlineProfile CardPM
+Quote Post

duffsstuff
RE: Getting The Visitor's IP
12 Oct, 2006 - 03:08 PM
Post #3

D.I.C Head
**

Joined: 10 Sep, 2006
Posts: 67


My Contributions
I don't think the link is working because of the quotes. Is there some other way to put in quotes?

Thanks and I, see my errors, how would I then make it search a txt file with all the admin's IPs? So it's not just one.

P.S.
I plan to set up a second security step where when your Ip is right and you link to the page it asks you to login, it contacts my MySQL server, on a separate table from users and verifies you are an admin. Then it will take you to the administration page.

This post has been edited by duffsstuff: 12 Oct, 2006 - 03:14 PM
User is offlineProfile CardPM
+Quote Post

AYYASH
RE: Getting The Visitor's IP
12 Oct, 2006 - 04:12 PM
Post #4

New D.I.C Head
*

Joined: 27 Mar, 2006
Posts: 30


My Contributions
In order for the script to be working well you should escape the double qouts inside the HTML element by using back slash or use single qoute instead to be like this ...href='#'>...

CODE
<?php
      if ($_SERVER['REMOTE_ADDR']=='71.134.229.249') {
          echo "<a href=\"#\">Administration</a>";
      }
?>


good luck

This post has been edited by AYYASH: 12 Oct, 2006 - 05:24 PM
User is offlineProfile CardPM
+Quote Post

duffsstuff
RE: Getting The Visitor's IP
12 Oct, 2006 - 04:58 PM
Post #5

D.I.C Head
**

Joined: 10 Sep, 2006
Posts: 67


My Contributions
thanks, everything seems to be working now.
User is offlineProfile CardPM
+Quote Post

Wild_Rose
RE: Getting The Visitor's IP
3 Jun, 2007 - 01:04 PM
Post #6

New D.I.C Head
*

Joined: 13 Apr, 2007
Posts: 39


My Contributions
how I put a range for some IPs?
for example if I want all the IPs from 213.5.58.247 to 213.5.58.250?

so far I have this

CODE
$IP = $_SERVER['REMOTE_ADDR'];

    if ($IP=='213.5.58.247')
    { $content.="<br><br>you're at <b>school</b>!";}
    

User is offlineProfile CardPM
+Quote Post

snoj
RE: Getting The Visitor's IP
3 Jun, 2007 - 02:37 PM
Post #7

$Null
Group Icon

Joined: 31 Mar, 2003
Posts: 3,304



Thanked: 7 times
Dream Kudos: 700
My Contributions
Regular expressions...or just match the first X number of characters in the address.
User is offlineProfile CardPM
+Quote Post

Wild_Rose
RE: Getting The Visitor's IP
3 Jun, 2007 - 02:56 PM
Post #8

New D.I.C Head
*

Joined: 13 Apr, 2007
Posts: 39


My Contributions
I tried this
CODE
    if ($IP>='195.130.92.69' AND $IP<='195.130.92.89')
    { $content.="<br><br>you're at home!";}


but it doesn't work sad.gif
and I can't leave the last X numbers because that doesn't bring the right results in this case.
User is offlineProfile CardPM
+Quote Post

snoj
RE: Getting The Visitor's IP
3 Jun, 2007 - 04:04 PM
Post #9

$Null
Group Icon

Joined: 31 Mar, 2003
Posts: 3,304



Thanked: 7 times
Dream Kudos: 700
My Contributions
It doesn't work because those are strings which in this case are likely converted to floats (or at best integers) to be compared against whatever $IP holds.

What I spoke of in comparing the first so many characters is using something like substr() to grab only the part of the IP you wish to check against.

Another thing you could try is putting all IP's in an array and then use something like in_array() to see if the IP of the client are valid or in the range specified.
User is offlineProfile CardPM
+Quote Post

Wild_Rose
RE: Getting The Visitor's IP
4 Jun, 2007 - 07:00 AM
Post #10

New D.I.C Head
*

Joined: 13 Apr, 2007
Posts: 39


My Contributions
I thought so... I just wanted to see if there's an easier solution tongue.gif
thanks!
User is offlineProfile CardPM
+Quote Post

burial
RE: Getting The Visitor's IP
4 Jun, 2007 - 06:31 PM
Post #11

New D.I.C Head
*

Joined: 3 Jun, 2007
Posts: 5


My Contributions
You can use .htaccess authorization, too.
Put this in your httpd.conf to enable .htaccess:

CODE
AccessFileName .htaccess

<FilesMatch "^\.ht">
    Order allow,deny
    Deny from all
</FilesMatch>


And put this in .htaccess file in your root folder:

CODE
AuthName "What?"
AuthType Basic
<Limit GET POST>
order deny,allow
deny from all
allow from 127.0.0.1
</Limit>


This will allow only users with IP 127.0.0.1 to access the page ;]

Good luck!
User is offlineProfile CardPM
+Quote Post

trashofmasters
RE: Getting The Visitor's IP
11 Jun, 2007 - 02:04 AM
Post #12

New D.I.C Head
*

Joined: 10 Jun, 2007
Posts: 1


My Contributions
QUOTE(Wild_Rose @ 3 Jun, 2007 - 03:56 PM) *

I tried this
CODE
    if ($IP>='195.130.92.69' AND $IP<='195.130.92.89')
    { $content.="<br><br>you're at home!";}


but it doesn't work sad.gif
and I can't leave the last X numbers because that doesn't bring the right results in this case.

To compare ips you should convert them into integers :
ip2long() and long2ip() functions.


User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 12/5/08 02:34AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month