How can i get the client's ip address in php?

Discussion in 'PHP' started by pinaki, Jul 25, 2009.

  1. #1
    How can i get the client's ip address in php by embedding the code in html of a different site.
     
    pinaki, Jul 25, 2009 IP
  2. Smaug

    Smaug Peon

    Messages:
    374
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you can access client ip using this
    
    $client_ip = $_SERVER['REMOTE_ADDR'];
    
    Code (markup):
     
    Smaug, Jul 25, 2009 IP
  3. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Not exactly sure what you're intending. But PHP can retreive the user's IP (course they actually have to visit the php for it to work) by using the $_SERVER['REMOTE_ADDR'] value.

    You'll have to be more specific in regards to 'different site', but if you mean something like tracking you could simply embed an image to do that trick.

    For example say you have a blank.gif (a single transparent image), and a php file.

    
    <?
    $ip = $_SERVER['REMOTE_ADDR'];
    // Do something with the IP here
    
    header('Content-Type: image/gif');
    readfile('blank.gif');
    ?>
    
    PHP:
    Then in the html
    
    <img src="http://www.mysite.com/track.php"/>
    
    Code (markup):
    Course that seems a tad obvious, if we were to add into an .htaccess file:

    
    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^track/image.gif /track.php
    
    Code (markup):
    You can then use
    
    <img src="http://www.mysite.com/track/image.gif"/>
    
    Code (markup):
    and it'll look like a normal image, but still be executing the php code.

    In a Nutshell:
    1) User's browser requests the image.
    2) The PHP retrieves the visitor's information (can be IP, referer, cookies, etc etc etc)
    3) The PHP sends back an HTTP header (in this case an image content-type, but you could set cookies and send back other headers)
    4) The PHP begins reading the data of the file blank.gif and sends that directly back to the browser
    5) The browser notices the header as being an image/gif type, and starts loading the data being sent back as an image.

    You could go a step further in making it load other things, like a banner perhaps, or using GD Extensions have it autodraw the user's info onto the image being sent back (remember all those popular banners that said "Your IP is....", its also the same tactic they use with the banners that show your city/state)

    At one point of time I had a rule in an htaccess like this.
    
    RewriteRule ^-(.*)/(.*)/blank.gif /trackr.php?section=$1&sub=$2 [L]
    
    Code (markup):
    And what I could do is for example in a message board
    
    [img ]http://www.mydomain.com/messageboard-name/thread-title/blank.gif[ /img]
    
    Code (markup):
    Which would show either a banner or blank pixel on the message board, but behind the scene it was creating a folder named 'messageboard-name' and saving a log file 'thread-title.log' into that folder. So that I could record the visitors hitting my thread. Course eventually I ended up moving over to a MySQL method.
     
    Last edited: Jul 25, 2009
    kblessinggr, Jul 25, 2009 IP
  4. pinaki

    pinaki Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    by different site i mean...if we have the php code to get the ip,say, $_SERVER['REMOTE_ADDR'] in my site....now another site is implementing my code.

    so i gave a link which they will put in their site..now the ip address returned for the user is not the client_ip but my server's IP...

    This is the problem i am facing :)
     
    pinaki, Jul 25, 2009 IP
  5. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I'm still a tad confused.

    If another site implemented your code that used $_SERVER['REMOTE_ADDR'] they would be receiving the IP address of the person that visited that page on their server (not your IP).

    The only way your server's IP would end up being recorded, is if your server pulled the php from the other server. In order for $_SERVER["REMOTE_ADDR"] to work as is, the visitor needs to contact that page directly.

    For example
    User -> Your Server -> Another server (via curl or whatever)
    If $_SERVER['REMOTE_ADDR'] is pulled from "another server" then it'll show your server's IP, likewise if its pulled from your server, it'll show the user's IP.

    So I guess I'm not entirely sure how you got it setup currently.
     
    kblessinggr, Jul 25, 2009 IP
  6. pinaki

    pinaki Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Let me give u a example
    i have a code say
    <!-- My site Text Ads Code START -->
    <?php include("http://test.mysite.com/sserve.php?pid=30313"); ?>
    <!-- Mysite Text Ads Code END -->

    now u have placed this code in www.yoursite.com...

    the function of the code is to log ip address visitor...

    here when when get record i get the ip of the mysite.com server whereas i am expecting the ip of user..i have used $_SERVER['REMOTE_ADDR'] to get ip.

    is there any solution to it?
     
    pinaki, Jul 25, 2009 IP
  7. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #7
    See... that's a problem, you can't include a php file from accross the internet. The PHP code itself gets executed just like with a webbrowser requesting it before it gets back to the requester. If you could include it in the manner you were hoping for, that would be a serious security problem since it would mean PHP code would no longer be protected.

    For example:

    User visits your php
    PHP executes code
    Returns response/html/etc back to the USer's browser

    In the scenario above
    Server requests your php
    PHP executes code (hence using the server's IP as the REMOTE_ADDR)
    Returns the response
    Response gets placed into the new response to the user... which was already executed, as such never got the user's IP

    If you still insist on using the include() method. you're going to have to modify your code like so
    <!-- My site Text Ads Code START -->
    <?php include("http://test.mysite.com/sserve.php?pid=30313&ip=".$_SERVER['REMOTE_ADDR']); ?>
    <!-- Mysite Text Ads Code END -->

    That way the IP gets passed along to you as that server see's it. You'll of course need to make an adjustment in your php to expect the incoming ip variable, perhaps something like this at the top of the code:

    
    $ip = isset($_GET['ip'])?$_GET['ip']:$_SERVER['REMOTE_ADDR'];
    
    PHP:
    Basically what that's doing, if $_GET['ip'] is set, meaning a value was sent, it uses that value for the IP, otherwise it uses $_SERVER['REMOTE_ADDR'] as the value (in case a user visited the url directly). But course if someone were to not use the $_SERVER['REMOTE_ADDR'] value, they could potentially just make up their own ips to send to you simply by changing the url to something else. Not something I would worry about if these inclusions are all on your same box, or if you actually trust the person to keep the code intact.

    I would actually recommend using the image method I mentioned earlier, and have your users embed an image instead of a php include. That way their visitors would be requesting your little tracking graphic directly thus giving you much better access to the user's stats.
     
    Last edited: Jul 25, 2009
    kblessinggr, Jul 25, 2009 IP
  8. pinaki

    pinaki Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    If i do it by javascript like

    <!-- My site Text Ads Code START -->
    <script language="javascript" type="text/javascript" src="http://mysite.test.com/serve.php?pid=30313"></script>
    <!-- My site Text Ads Code END -->

    will that help?
     
    pinaki, Jul 25, 2009 IP
  9. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #9
    That would work because the server isn't requesting the php file in that instance, but instead the user's browser is.

    Just make sure the PHP sends something back to the browser, even if its simply a javascript comment line like //hello
     
    kblessinggr, Jul 25, 2009 IP
  10. pinaki

    pinaki Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Thanks a lot. This works.
     
    pinaki, Jul 25, 2009 IP
  11. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #11
    FYI, with the javascript method, you could actually have the php send back javascript code that could collect more data (via ajax and what not), such as the visitor's browser size, etc etc , the same kind of stuff google analytics accomplishes with their javascript code.
     
    kblessinggr, Jul 25, 2009 IP