Professional Web Applications Themes

Useronline code does not work ?? any ideas - PHP Development

I just cannot get the following code to work ?? $insert = mysql_db_query($database, "INSERT INTO useronline VALUES ('$timestamp','$REMOTE_ADDR','$PHP_SELF')"); I've changed it to $ip = getHostbyAddr($REMOTE_ADDR); $insert = mysql_db_query($database, "INSERT INTO useronline VALUES ('$timestamp','$ip','$PHP_SELF')"); The 1st bit of code I get the following in the Mysql database timestamp - ip - file 1124650534 - 'empty' - 'empty' The 2nd bit of code I get the following in the Mysql database timestamp - ip - file 1124650534 - server - 'empty' I running behind an apache server Any ideas how I can fill in the user ip address etc ???...

  1. #1

    Default Useronline code does not work ?? any ideas

    I just cannot get the following code to work ??

    $insert = mysql_db_query($database, "INSERT INTO useronline VALUES
    ('$timestamp','$REMOTE_ADDR','$PHP_SELF')");

    I've changed it to
    $ip = getHostbyAddr($REMOTE_ADDR);
    $insert = mysql_db_query($database, "INSERT INTO useronline VALUES
    ('$timestamp','$ip','$PHP_SELF')");


    The 1st bit of code I get the following in the Mysql database

    timestamp - ip - file
    1124650534 - 'empty' - 'empty'

    The 2nd bit of code I get the following in the Mysql database

    timestamp - ip - file
    1124650534 - server - 'empty'

    I running behind an apache server

    Any ideas how I can fill in the user ip address etc ???
    shorty Guest

  2. #2

    Default Re: Useronline code does not work ?? any ideas

    "shorty" wrote:
     

    Try looking here:
    <http://www.googleityoumoron.com/?go=php+REMOTE_ADDR+PHP_SELF>

    --
    phil [dot] ronan virgin [dot] net
    http://vzone.virgin.net/phil.ronan/


    Philip Guest

  3. #3

    Default Re: Useronline code does not work ?? any ideas

    > ('$timestamp','$REMOTE_ADDR','$PHP_SELF')");

    $_SERVER['REMOTE_ADDR'] and $_SERVER['PHP_SELF'].

    Regards,

    Anze
    Anze Guest

  4. #4

    Default Re: Useronline code does not work ?? any ideas

    No - sorry that dosn't work.

    Any other ideas, from behind an Apache server.

     
    >
    >$_SERVER['REMOTE_ADDR'] and $_SERVER['PHP_SELF'].
    >[/ref]


    shorty Guest

  5. #5

    Default Re: Useronline code does not work ?? any ideas

    > > > ('$timestamp','$REMOTE_ADDR','$PHP_SELF')");
     [/ref]
     

    It will work if used properly:

    $query = 'INSERT INTO useronline ( timestamp, ip, file ) '
    . "VALUES ('" . mysql_escape_string( $timestamp ) . "', "
    . "'" . mysql_escape_string( $_SERVER['REMOTE_ADDR'] ) . "', "
    . "'" . mysql_escape_string( $_SERVER['PHP_SELF'] ) . "' )";
    echo $query . "<br />\n";
    $insert = mysql_db_query( $database, $query );
    if ($insert===FALSE)
    echo mysql_error() . "<br />\n";


    Hilarion
    Hilarion Guest

  6. #6

    Default Re: Useronline code does not work ?? any ideas

    No sorry that does not work, all I get displayed is

    INSERT INTO useronline ( timestamp, ip, file ) VALUES ('1124698488',
    '82.15.78.135', '/family_search4/familysearch.php' )
    insert command denied to user: 'userlocalhost' for table 'useronline'

    But its giving me the ip address etc and thats more information than I
    managed to get.

    I've checked on the priviliges for that database and table and
    evertthing seems correct

     [/ref]
    > [/ref]

    >
    >It will work if used properly:
    >
    >$query = 'INSERT INTO useronline ( timestamp, ip, file ) '
    > . "VALUES ('" . mysql_escape_string( $timestamp ) . "', "
    > . "'" . mysql_escape_string( $_SERVER['REMOTE_ADDR'] ) . "', "
    > . "'" . mysql_escape_string( $_SERVER['PHP_SELF'] ) . "' )";
    >echo $query . "<br />\n";
    >$insert = mysql_db_query( $database, $query );
    >if ($insert===FALSE)
    > echo mysql_error() . "<br />\n";
    >
    >
    >Hilarion[/ref]

    shorty Guest

  7. #7

    Default Re: Useronline code does not work ?? any ideas

    > > > > > ('$timestamp','$REMOTE_ADDR','$PHP_SELF')"); 
    > >
    > > It will work if used properly:
    > >
    > > $query = 'INSERT INTO useronline ( timestamp, ip, file ) '
    > > . "VALUES ('" . mysql_escape_string( $timestamp ) . "', "
    > > . "'" . mysql_escape_string( $_SERVER['REMOTE_ADDR'] ) . "', "
    > > . "'" . mysql_escape_string( $_SERVER['PHP_SELF'] ) . "' )";
    > > echo $query . "<br />\n";
    > > $insert = mysql_db_query( $database, $query );
    > > if ($insert===FALSE)
    > > echo mysql_error() . "<br />\n";[/ref][/ref]
     

    This means that the user called "user" does not have correct privileges
    to perform INSERT.

     

    As you see the solution Anze gave you ($_SERVER['REMOTE_ADDR']
    and $_SERVER['PHP_SELF']) was correct one. Next time when some solution
    you get here does not work, then doble check your code, check what
    results you get (by using echo on your query statement before execution),
    and what error messages you get (like mysql_error). If you still do
    not get the code to work, then write how did you try to use given
    solution and what results you got.

     

    I do not know MySQL good enough to give you any hints on that, but it
    really looks like you did not give proper rights to the user called "user".


    Hilarion
    Hilarion Guest

  8. #8

    Default Re: Useronline code does not work ?? any ideas

    Why would this error come up
     

    I am trying to access my server from a remote computer, so why would I
    get userlocalhost localhost ??? surely I would have got usermydomain
    or whatever mydomain is ??
    shorty Guest

  9. #9

    Default Re: Useronline code does not work ?? any ideas

    Forget to mention if the priviliges are not set correctly then why am
    I able insert the 'timestamp' into its field ?? but not the others ??

    The ip and file fields are set to 'varchar', so should accept the data
    ??
     

    shorty Guest

  10. #10

    Default Re: Useronline code does not work ?? any ideas

    > Why would this error come up 
    >
    > I am trying to access my server from a remote computer, so why would I
    > get userlocalhost localhost ??? surely I would have got usermydomain
    > or whatever mydomain is ??[/ref]


    I do not know (I do not use MySQL).
    Maybe it's all about user on the server system (which is localhost
    for the MySQL server) which is used to access the MySQL (not the
    MySQL login). Or maybe the MySQL login you are using is

    You may want to ask this question on "alt.php.sql".


    Hilarion
    Hilarion Guest

  11. #11

    Default Re: Useronline code does not work ?? any ideas

    > Forget to mention if the priviliges are not set correctly then why am 


    Are the fields really called "timestamp", "ip" and "file"?
    If not, then change the names in the query:
    INSERT INTO useronline ( timestamp, ip, file )...


    Hilarion

    PS.: Maybe you have rights to insert into "timestamp" column/field,
    but not other column/fields.
    Hilarion Guest

  12. #12

    Default Re: Useronline code does not work ?? any ideas


    Hilarion Wrote: [/ref]
    > 'useronline' 
    > I 
    > usermydomain 
    >
    >
    > I do not know (I do not use MySQL).
    > Maybe it's all about user on the server system (which is localhost
    > for the MySQL server) which is used to access the MySQL (not the
    > MySQL login). Or maybe the MySQL login you are using i
    >
    >
    > You may want to ask this question on "alt.php.sql".
    >
    >
    > Hilarion[/ref]

    Hilarion is correct. The Apache server running PHP is what's accessin
    (or trying to at any rate) the MySQL server on the same system. I
    other words, your remote computer connects to the web server which i
    turn connects to the database. Hence the "userlocalhost"...

    --
    macbr
    -----------------------------------------------------------------------
    macbri's Profile: http://www.macosx.com/forums/member.php?userid=3441
    View this thread: http://www.macosx.com/forums/showthread.php?t=24029
    macosx.com - The Answer to Mac Support - http://www.macosx.co

    macbri Guest

  13. #13

    Default Re: Useronline code does not work ?? any ideas

    Got it working and thanks to all.

    Nothing to do with priviliges or any Mysql problems.

    The code now reads

    $ip = $_SERVER['REMOTE_ADDR'];
    $file = $_SERVER['PHP_SELF'];
    $query = mysql_db_query($dbname, "INSERT INTO useronline
    VALUES('$timestamp','$ip','$file')") or die("<b>MySQL Error:</b>
    ".mysql_error());


    For some reason, be it the way i write it, the code will not run if
    $_SERVER['REMOTE_ADDR'] or $_SERVER['PHP_SELF'] is entered directly
    into the insert command.

    Once again thanks.



    shorty Guest

  14. #14

    Default Re: Useronline code does not work ?? any ideas

    > Nothing to do with priviliges or any Mysql problems. 


    You got it working, but the cause of problems was not using
    (or not using) $_SERVER array directly, but using column names
    in the INSERT query.

    When I wrote:

    $query = 'INSERT INTO useronline ( timestamp, ip, file ) '
    . "VALUES ('" . mysql_escape_string( $timestamp ) . "', "
    . "'" . mysql_escape_string( $_SERVER['REMOTE_ADDR'] ) . "', "
    . "'" . mysql_escape_string( $_SERVER['PHP_SELF'] ) . "' )";
    I assumed that the columns you are inserting into are called
    "timestamp", "ip" and "file". I was probably right, but I forgot
    that TIMESTAMP is reserved keword of SQL (at least in MySQL).
    If you change the query above to:
    $query = 'INSERT INTO useronline ( `timestamp`, `ip`, `file` ) '
    . "VALUES ('" . mysql_escape_string( $timestamp ) . "', "
    . "'" . mysql_escape_string( $_SERVER['REMOTE_ADDR'] ) . "', "
    . "'" . mysql_escape_string( $_SERVER['PHP_SELF'] ) . "' )";
    then it should work.

    Why do I insist on using column names in INSERT query? Because
    it makes the INSERT more readable and also if you rearrange the
    columns ordering, then the oryginal INSERT (without column names)
    will stop working (or will work not as intended - insert values
    into wrong fields), while the one with the column names will still
    work.


    Hilarion
    Hilarion Guest

Similar Threads

  1. Ideas for sharing same code with 2 web sites
    By sugarstudio in forum Coldfusion - Advanced Techniques
    Replies: 7
    Last Post: September 17th, 07:20 PM
  2. Replies: 5
    Last Post: September 3rd, 05:43 PM
  3. Small code problem .. any ideas?
    By nevermindthat in forum Macromedia ColdFusion
    Replies: 4
    Last Post: August 4th, 07:22 PM
  4. code not working..ideas please?
    By Geoff in forum PERL Miscellaneous
    Replies: 17
    Last Post: October 2nd, 09:21 AM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139