SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: Logging ip's

Hybrid View

  1. #1
    SitePoint Zealot Beckwith's Avatar
    Join Date
    Mar 2004
    Location
    California
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Logging ip's

    I have a code that will log the ip of the user and display it, however it's not logging it. It doesn't say there's anything wrong with the code, and doesn't give me any errors, just when I type select * from ips; it says empty set. Here's the code:
    PHP Code:
    <?php
    $ip 
    $_SERVER['REMOTE_ADDR'];
    $sql "INSERT INTO ips SET
           ip='
    $ip',
           view_date='CURDATE()',
           view_time='CURTIME()'"
    ;
    if (
    $sql) {
      echo(
    'IP Logged: ' $ip '');
    } else {
      die(
    'Error logging your ip:' $ip '');
    }
    ?>
    and here's my sql code (MySQL):
    Code:
    CREATE TABLE ips (
    ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    ip VARCHAR(15) NOT NULL,
    view_date DATE NOT NULL,
    view_time TIME NOT NULL
    );
    Query OK, 0 rows affected (0.01 sec)
    I don't see anything wrong with my syntax or sql, any of you see anything wrong?

  2. #2
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to use mysql_query(). Also, are you connecting to the db?

    PHP Code:
    <?php
    $ip 
    $_SERVER['REMOTE_ADDR'];
    $sql mysql_query("INSERT INTO ips SET
           ip='
    $ip',
           view_date='CURDATE()',
           view_time='CURTIME()'"
    );
    if (
    $sql) {
      echo(
    'IP Logged: ' $ip '');
    } else {
      die(
    'Error logging your ip:' $ip '');
    }
    ?>

  3. #3
    SitePoint Zealot Beckwith's Avatar
    Join Date
    Mar 2004
    Location
    California
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, worked! Oh and one other thing though for other people coming with the same problem,
    PHP Code:
    <?php 
    $ip 
    $_SERVER['REMOTE_ADDR']; 
    $sql mysql_query("INSERT INTO ips SET 
           ip='
    $ip', 
           view_date='CURDATE()', 
           view_time='CURTIME()'"
    ); 
    if (
    $sql) { 
      echo(
    'IP Logged: ' $ip ''); 
    } else { 
      die(
    'Error logging your ip:' $ip ''); 

    ?>
    view_date='CURDATE()', needs to be view_date=CURDATE(), without the single quotes, and same for CURTIME().

    Thanks a lot!

  4. #4
    Apache Expert i_like_php's Avatar
    Join Date
    Nov 2001
    Location
    Dallas, Texas
    Posts
    1,342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok so could you show the updated sql and code so i can use this for testing as well .
    i love php

  5. #5
    SitePoint Member newfiewebguru's Avatar
    Join Date
    Apr 2004
    Location
    Mount Pearl, Newfoundland
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $ip 
    $_SERVER['REMOTE_ADDR'];
    $sql mysql_query("INSERT INTO ips SET
           ip='
    $ip',
           view_date='CURDATE()',
           view_time='CURTIME()'"
    );
    if (
    $sql) {
      echo(
    'IP Logged: ' $ip '');
    } else {
      die(
    'Error logging your ip:' $ip '');
    }
    ?>
    Should actually be the following, if you want to input curtime() and curdate() dirrectly into the database:
    PHP Code:
    <?php
    $ip 
    $_SERVER['REMOTE_ADDR'];
    $sql mysql_query("INSERT INTO ips SET
           ip='
    $ip',
           view_date='"
    .CURDATE()"',
           view_time='"
    .CURTIME()."'");
    if (
    $sql) {
      echo(
    'IP Logged: ' $ip '');
    } else {
      die(
    'Error logging your ip:' $ip '');
    }
    ?>
    It may work the way you posted, but this way is safer.

  6. #6
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by newfiewebguru
    PHP Code:
    <?php
    $ip 
    $_SERVER['REMOTE_ADDR'];
    $sql mysql_query("INSERT INTO ips SET
           ip='
    $ip',
           view_date='"
    .CURDATE()"',
           view_time='"
    .CURTIME()."'");
    if (
    $sql) {
      echo(
    'IP Logged: ' $ip '');
    } else {
      die(
    'Error logging your ip:' $ip '');
    }
    ?>
    It may work the way you posted, but this way is safer.
    Should actually be: (Missed a period)
    PHP Code:
    <?php
    $ip 
    $_SERVER['REMOTE_ADDR'];
    $sql mysql_query("INSERT INTO ips SET
           ip='
    $ip',
           view_date='"
    .CURDATE()."',
           view_time='"
    .CURTIME()."'");
    if (
    $sql) {
      echo(
    'IP Logged: ' $ip '');
    } else {
      die(
    'Error logging your ip:' $ip '');
    }
    ?>
    How is this method more safe?

  7. #7
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    UK
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Incidentally, you may find this IP gathering function a little more reliable.

    PHP Code:
    function realIp() {
        
    $ip=FALSE;
        if(!empty(
    $_SERVER["HTTP_CLIENT_IP"])) $ip=$_SERVER["HTTP_CLIENT_IP"];

        if(!empty(
    $_SERVER['HTTP_X_FORWARDED_FOR'])) {
            
    $ips=explode(", "$_SERVER['HTTP_X_FORWARDED_FOR']);
            if (
    $ip) { array_unshift($ips$ip); $ip FALSE; }
            for (
    $i 0$i count($ips); $i++) {
                if (!
    eregi ("^(10|172\.16|192\.168)\."$ips[$i])) {
                    
    $ip $ips[$i];
                    break;
                }
            }
        }
        return (
    $ip $ip $_SERVER['REMOTE_ADDR']);

    This function will get the real IP Address, as opposed to the IP address of the transparent proxy that many cable companies force their customers to route through.

    How we currently use it:

    PHP Code:
    <div class="box_general">
        <p>To help us resolve the issue that you report, the following information will be sent to the Studio-X Admin team:</p>
    <!-- Advise User of Information Being Sent -->
    <div class="callout_box">
        <?
            $ip 
    realip();
            
    $browser $_SERVER['HTTP_USER_AGENT'];
            if (!
    $cmd) { $callout_text "<p>IP Address: $ip</p><p>Browser: $browser</p><p>Member Name: $member_name</p>"; }
            else { 
    $callout_text $redirect_message; }
            echo 
    $callout_text;
        
    ?>
    </div>.....
    I can't take credit for the function, it's just something that I picked up from somewhere a while back and now can't live without.

  8. #8
    SitePoint Addict Mower's Avatar
    Join Date
    Feb 2004
    Location
    Aussie Aussie Aussie
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks that ip function rocks


    "Will I ever find my way to GURU?"


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
  •