View Full Version : Get IP address in PHP
Computer_Phreak
Apr 17, 2003, 06:42 PM
Ok..
I was wondering if someone could help me make a php file that would:
1.get the viewer's ip address
2.send this data to a MySQL database
and...
3.to retrive this infomation and display it as a table.
Rower_CPU
Apr 17, 2003, 09:56 PM
Use the HTTP POST vars...
REMOTE_ADDR will get you the client's IP address, so something like this will dump the IP into a variable that you can send to the DB.
$ip = $REMOTE_ADDR;
Let me know if you want specifics for talking to MySQL.
Computer_Phreak
Apr 17, 2003, 10:17 PM
Originally posted by Rower_CPU
Use the HTTP POST vars...
REMOTE_ADDR will get you the client's IP address, so something like this will dump the IP into a variable that you can send to the DB.
$ip = $REMOTE_ADDR;
Let me know if you want specifics for talking to MySQL.
Well, I'm a complete n00b to php - I only sound like i know what im talking about (or not) :D.
So your script would put the client's ip into a variable called ip?
Could i use, say, a text file on the server instead of MySQL to store the ip in? I don't need any advanced features or speed, just the simplest thing to do.
Rower_CPU
Apr 18, 2003, 10:17 AM
Let's take a giant step back here and let me ask what you're going to be doing with IPs and how they will need to be accessed.
And yes, the code above dumps the client IP address into a variable called "ip" (could be "sausages", but I used "ip" ;)).
Mr. Anderson
Apr 18, 2003, 10:25 AM
Originally posted by Rower_CPU
Let's take a giant step back here and let me ask what you're going to be doing with IPs and how they will need to be accessed.
And yes, the code above dumps the client IP address into a variable called "ip" (could be "sausages", but I used "ip" ;)).
mmmmmmm, snausages.......
Yeah, I'm curious as well as to why you're interested in getting IPs. What do you plan on doing with them after you've plopped them into your database?
D
eyelikeart
Apr 18, 2003, 10:46 AM
while this may be something useful for future reference in web design...I have no clue how to do this stuff...that's why I have Rower around... ;)
but snausages...now there's something I can elaborate upon...we should start a Willie Wonka thread! :p
Computer_Phreak
Apr 18, 2003, 01:55 PM
update-
I did a little research on my own and decided since i only needed the ip's to look at manually by myself (to see who visited a very low traffic site of mine), that it would be simpler to use a text file, no?
I looked at a little php primer thing, and i came up with:
$txtfl = $fopen("test.txt", "A+")
would that open test.txt for appending? I tried that on my mac os x machine, but got a php parse error for that line.
Rower_CPU
Apr 18, 2003, 02:00 PM
You forgot the trailing semi-colon. I hate when that happens. ;)
That should work just fine for appending the IPs to the file. You'll want to close the file after writing so that the next visitor can write to it.
You might also want to look into other vars you can get from the client, since I don't know how helpful the IP will be, unless you're just trying to track individual users.
Computer_Phreak
Apr 18, 2003, 02:07 PM
ok.. is there a way to record info by a unique link? for example:
www.mysite.com/mypage.php?NameOfClient
edit: lol, thats a real site!
and then record that name of client?
Rower_CPU
Apr 18, 2003, 02:11 PM
How would the client name get there in the first place? Is there a login of some sort?
Computer_Phreak
Apr 18, 2003, 02:29 PM
Originally posted by Rower_CPU
How would the client name get there in the first place? Is there a login of some sort?
they would get there from AIM, which has the %n operator for determining someone's screen name.
It would it be a link like:
www.mysite.com/mypage.php?%n
Rower_CPU
Apr 18, 2003, 02:43 PM
So the variable would always be passed to the page from AIM. There's no need to specify it in the link, unless that's how you'll send it to other pages.
Have you checked to make sure PHP can understand the info coming from AIM?
Computer_Phreak
Apr 18, 2003, 03:02 PM
ok...
could i do something like this to assign the incoming name to a variable called myvar?
www.mysite.com/mypage.php?myvar=%n
Rower_CPU
Apr 18, 2003, 07:42 PM
Problem is that PHP won't read %n as a variable, but a string. So myvar would be "%n" and not the name value.
Give it a shot and see.
Computer_Phreak
Apr 18, 2003, 08:05 PM
Originally posted by Rower_CPU
Problem is that PHP won't read %n as a variable, but a string. So myvar would be "%n" and not the name value.
Give it a shot and see.
actually i dont think that would be a problem since AIM would make the %n the screen name before it followed the link.
the problem i have is getting the myvar to be of use... do i have to "dim" it or whatever in the php code, or is the variable created when the link is followed?
Rower_CPU
Apr 18, 2003, 10:01 PM
Nah, with PHP you don't need to explicitly state variables.
If you pass it to the page with POST or GET, it's there for you to use.
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.