SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict
    Join Date
    Jul 2007
    Posts
    233
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Curl and PHP: Client IP address gets changed

    Greetings,

    I am building a counter/security application of sorts that uses Curl and PHP. When a visitor comes to my site, the script is supposed to pass their IP address, browser headers, referring URLs and user agent through the Curl script, record data and perform security analysis.

    Everything works fine except in the output of the script, it records my server's IP address instead. This is a problem because I lose all the IP information from my clients

    My question is how do I pass the client's IP address through Curl without it being altered?

    Thanks

  2. #2
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Its difficult to tell where your problem lies withou some code
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  3. #3
    SitePoint Addict
    Join Date
    Jul 2007
    Posts
    233
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is my basic curl code:
    PHP Code:
    session_start();
    $url "http://www.mysite.com/target-page.php";
    $header "Location: ".$url;
    $ch curl_init($url);
    curl_setopt($chCURLOPT_HEADER$header);
    curl_setopt($chCURLOPT_FOLLOWLOCATION1);
    curl_exec($ch);
    curl_close($ch); 
    After the code runs, target-page.php is supposed to record the IP address of the visitor. Instead it records the IP address of my server running curl.

    I'm not sure how "sockets" work but is there a way to create a connection between target-page.php and the client (for example, the target-page.php collects client IP information and sends information back to the client?)

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    There's no need for sockets.

    You already have access to the users information, you just need to capture/send in a manageable way.

    PHP Code:
    <?php
    function get_server_var($key){
      return !empty(
    $_SERVER[$key]) ? $_SERVER[$key] ? null ;
    }

    $params = array(
      
    'remote-address'  => get_server_var('REMOTE_ADDR'),
      
    'user-agent'      => get_server_var('HTTP_USER_AGENT'),
      
    'referrer'        => get_server_var('HTTP_REFERER')
    );

    $handle curl_init(sprintf(
      
    'http://www.example.org/tracker.php?%s',
      
    http_build_query($params)
    ));

    curl_setopt($handleCURLOPT_RETURNTRANSFERtrue);

    $response curl_exec($handle);
    In 'tracker.php' you can access the $_GET superglobal to obtain the remote users information. Although, I'm guessing you're wanting some additional behaviour here...
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Addict
    Join Date
    Jul 2007
    Posts
    233
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help Anthony.

    Unfortunately, I am getting this error right after the "null" part of the script:
    Parse error: syntax error, unexpected ';' on line 3"

  6. #6
    SitePoint Guru
    Join Date
    Aug 2009
    Posts
    669
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's a tenary thing. One of those ? marks should be a colon :.
    I'll do anything to avoid working on my own code

    Are you using: if (isset($_POST['submit'])) ?
    IE has a bug and does not always send the value.

  7. #7
    SitePoint Addict
    Join Date
    Jul 2007
    Posts
    233
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, I got the script working now and I see what it is doing. I was actually able to do the equivalent using session variables in some of my earlier attempts

    What if we take this a step further though and tracker.php is on an external third party site out of my control (such as a counter and API)? If we use "REMOTE_ADDR" on tracker.php, the IP still shows up as my server.

  8. #8
    SitePoint Guru
    Join Date
    Aug 2009
    Posts
    669
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Correct because your server has become the client to that site and its script. I think you might be able to convince it your a proxy or mirror using the HTTP_X_FORWARDED_FOR header but you'll need to look into that.
    I'll do anything to avoid working on my own code

    Are you using: if (isset($_POST['submit'])) ?
    IE has a bug and does not always send the value.

  9. #9
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by peppy View Post
    Ah, I got the script working now and I see what it is doing. I was actually able to do the equivalent using session variables in some of my earlier attempts
    Yes, sorry about that. As pointed out by tangoforce, the second question mark should have been a colon.

    Quote Originally Posted by peppy View Post
    What if we take this a step further though and tracker.php is on an external third party site out of my control (such as a counter and API)? If we use "REMOTE_ADDR" on tracker.php, the IP still shows up as my server.
    There's nothing you can do about that I'm afraid, unless of course the remote server permits (and supplies for) this functionality.

    What exactly are you trying to do here, what underlying problem are you trying to solve?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •