Get IP address in PHP

Discussion in 'Community' started by Computer_Phreak, Apr 17, 2003.

  1. Computer_Phreak macrumors 6502

    Joined:
    Jul 15, 2002
    #1
    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.
     
  2. Rower_CPU Moderator emeritus

    Rower_CPU

    Joined:
    Oct 5, 2001
    Location:
    San Diego, CA
    #2
    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.
    PHP:
    $ip $REMOTE_ADDR;
    Let me know if you want specifics for talking to MySQL.
     
  3. Computer_Phreak thread starter macrumors 6502

    Joined:
    Jul 15, 2002
    #3
    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.
     
  4. Rower_CPU Moderator emeritus

    Rower_CPU

    Joined:
    Oct 5, 2001
    Location:
    San Diego, CA
    #4
    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" ;)).
     
  5. Mr. Anderson Moderator emeritus

    Mr. Anderson

    Joined:
    Nov 1, 2001
    Location:
    VA
    #5
    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
     
  6. eyelikeart Moderator emeritus

    eyelikeart

    Joined:
    Jan 2, 2001
    Location:
    Metairie, LA
    #6
    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
     
  7. Computer_Phreak thread starter macrumors 6502

    Joined:
    Jul 15, 2002
    #7
    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:
    PHP:

    $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.
     
  8. Rower_CPU Moderator emeritus

    Rower_CPU

    Joined:
    Oct 5, 2001
    Location:
    San Diego, CA
    #8
    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.
     
  9. Computer_Phreak thread starter macrumors 6502

    Joined:
    Jul 15, 2002
    #9
    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?
     
  10. Rower_CPU Moderator emeritus

    Rower_CPU

    Joined:
    Oct 5, 2001
    Location:
    San Diego, CA
    #10
    How would the client name get there in the first place? Is there a login of some sort?
     
  11. Computer_Phreak thread starter macrumors 6502

    Joined:
    Jul 15, 2002
    #11
    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
     
  12. Rower_CPU Moderator emeritus

    Rower_CPU

    Joined:
    Oct 5, 2001
    Location:
    San Diego, CA
    #12
    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?
     
  13. Computer_Phreak thread starter macrumors 6502

    Joined:
    Jul 15, 2002
    #13
    ok...

    could i do something like this to assign the incoming name to a variable called myvar?

    www.mysite.com/mypage.php?myvar=%n
     
  14. Rower_CPU Moderator emeritus

    Rower_CPU

    Joined:
    Oct 5, 2001
    Location:
    San Diego, CA
    #14
    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.
     
  15. Computer_Phreak thread starter macrumors 6502

    Joined:
    Jul 15, 2002
    #15
    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?
     
  16. Rower_CPU Moderator emeritus

    Rower_CPU

    Joined:
    Oct 5, 2001
    Location:
    San Diego, CA
    #16
    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.
     

Share This Page