1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to fix this Warning being shown on my website

Discussion in 'PHP' started by mistermix, Nov 21, 2013.

  1. #1
    hi folks

    A couple of days ago one of my websites started displaying the following error message at the top of every page:

    The code on line 355 of functions.php is (line 3):

    PHP:
    1. $delete_query2 = "SELECT ipaddress,usertime FROM userlog";
    2.     $delete_exec2 = mysql_query ($delete_query2);
    3.     while ($delete_result2 = mysql_fetch_array ($delete_exec2))
    4.     {
    5.         $delete_time2 = $delete_result2["usertime"];
    6.         if ($delete_time2 < ($time - 86400))
    The code on line 388 of functions.php is (line 3):

    PHP:
    1. $check_query2 = "SELECT ipaddress FROM userlog WHERE ipaddress='$myip'";
    2.     $check_exec2 = mysql_query ($check_query2);
    3.     $check2 = mysql_num_rows ($check_exec2);
    4.     if ($check2 == 0)
    I basically have no idea how to fix this, any help is appreciated. Initially i'd like to know how to atleast prevent visitors from seeing the error message. The site is actually functioning as normal, apart from error shown at the top of every page.

    If you need more info, please do ask.

    Thanks
    mistermix, Nov 21, 2013 IP
  2. CoreyPeerFly

    CoreyPeerFly Well-Known Member Affiliate Manager

    Messages:
    386
    Likes Received:
    21
    Best Answers:
    5
    Trophy Points:
    190
    #2
    CoreyPeerFly, Nov 21, 2013 IP
  3. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,650
    Likes Received:
    22
    Best Answers:
    12
    Trophy Points:
    115
    #3
    Add after $delete_exec2=mysql_query($delete_query2); another IF statement

    if (mysql_num_rows($delete_query2)) { ... then the while { } and then }
    EricBruggema, Nov 21, 2013 IP
  4. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,650
    Likes Received:
    22
    Best Answers:
    12
    Trophy Points:
    115
    #4
    OMG!~don't do that, never use DIE! its so wrong and the whole damn script stops when you DIE! lol

    Use my example, much better... oh and avoid using die after queries... if you need DIE? you need to start learning more PHP for real!
    EricBruggema, Nov 21, 2013 IP
  5. CoreyPeerFly

    CoreyPeerFly Well-Known Member Affiliate Manager

    Messages:
    386
    Likes Received:
    21
    Best Answers:
    5
    Trophy Points:
    190
    #5
    It's for debugging. You can change it afterwards. Life will go on. :)
    CoreyPeerFly, Nov 21, 2013 IP
  6. PoPSiCLe

    PoPSiCLe Well-Known Member

    Messages:
    1,226
    Likes Received:
    110
    Best Answers:
    51
    Trophy Points:
    160
    #6
    And for the love of god, proper naming of the queries, please. $delete_query2? It doesn't delete anything, it selects. Just... no. Just no.
    PoPSiCLe, Nov 21, 2013 IP
  7. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,650
    Likes Received:
    22
    Best Answers:
    12
    Trophy Points:
    115
    #7
    Not even for debugging, there are better methods to do debugging.
    EricBruggema, Nov 21, 2013 IP
  8. mistermix

    mistermix Active Member

    Messages:
    2,326
    Likes Received:
    85
    Best Answers:
    0
    Trophy Points:
    90
    #8
    Thanks, I will try and implement your fix tonight. Would be good if you pasted the actual code for me...
    mistermix, Nov 22, 2013 IP
  9. mistermix

    mistermix Active Member

    Messages:
    2,326
    Likes Received:
    85
    Best Answers:
    0
    Trophy Points:
    90
    #9
    Ok tried to implement this but it either gave more errors or totally took the site down. its probably because the site is written with bad/old code.

    Here is a wider snippet of the code giving the error (lines 29 and 62):

    PHP:
    1. function log_user()
    2. {
    3.     $myid = $_SESSION['loggedinuserid'];
    4.     if (!$myid)
    5.     $myid = "";
    6.     if ($_SERVER['HTTP_X_FORWARD_FOR'])
    7.     {
    8.         $myip = $_SERVER['HTTP_X_FORWARD_FOR'];
    9.     }
    10.     else
    11.     {
    12.         $myip = $_SERVER['REMOTE_ADDR'];
    13.     }
    14.     $time = time();
    15.     $delete_query = "SELECT userip,usertime FROM online_users";
    16.     $delete_exec = mysql_query ($delete_query);
    17.     while ($delete_result = mysql_fetch_array ($delete_exec))
    18.     {
    19.         $delete_time = $delete_result["usertime"];
    20.         if ($delete_time < ($time - 300))
    21.         {
    22.             $delete_ip = $delete_result["userip"];
    23.             $query = "DELETE FROM online_users WHERE userip='$delete_ip'";
    24.             $exec = mysql_query ($query);
    25.         }
    26.     }
    27.     $delete_query2 = "SELECT ipaddress,usertime FROM userlog";
    28.     $delete_exec2 = mysql_query ($delete_query2);  
    29.     while ($delete_result2 = mysql_fetch_array ($delete_exec2))
    30.     {
    31.         $delete_time2 = $delete_result2["usertime"];
    32.         if ($delete_time2 < ($time - 86400))
    33.         {
    34.             $delete_ip = $delete_result2["ipaddress"];
    35.             $query2 = "DELETE FROM userlog WHERE ipaddress='$delete_ip'";
    36.             $exec2 = mysql_query ($query2);
    37.         }
    38.     }
    39.     $check_query = "SELECT userip FROM online_users WHERE userip='$myip'";
    40.     $check_exec = mysql_query ($check_query);
    41.     $check = mysql_num_rows ($check_exec);
    42.     if ($check == 0)
    43.     {
    44.         $insert_query = "INSERT INTO online_users (userid,userip,usertime) VALUES ('$myid','$myip','$time')";
    45.         $insert_exec = mysql_query ($insert_query);
    46.     }
    47.     else
    48.     {
    49.         if ($myid != 0)
    50.         {
    51.             $update_query = "UPDATE online_users SET usertime=$time, userid='$myid' WHERE userip='$myip'";
    52.             $update_exec = mysql_query ($update_query);
    53.         }
    54.         else
    55.         {
    56.             $update_query = "UPDATE online_users SET usertime=$time, userid='0' WHERE userip='$myip'";
    57.             $update_exec = mysql_query ($update_query);
    58.         }
    59.     }
    60.     $check_query2 = "SELECT ipaddress FROM userlog WHERE ipaddress='$myip'";
    61.     $check_exec2 = mysql_query ($check_query2);
    62.     $check2 = mysql_num_rows ($check_exec2);
    63.     if ($check2 == 0)
    64.     {
    65.         $insert_query2 = "INSERT INTO userlog (userid,ipaddress,usertime) VALUES ('$myid','$myip','$time')";
    66.         $insert_exec2 = mysql_query ($insert_query2);
    67.     }
    68.     else
    69.     {
    70.         if ($myid != 0)
    71.         {
    72.             $update_query2 = "UPDATE userlog SET usertime=$time, userid='$myid' WHERE ipaddress='$ipaddress'";
    73.             $update_exec2 = mysql_query ($update_query2);
    74.         }
    75.         else
    76.         {
    77.             $update_query2 = "UPDATE userlog SET usertime=$time, userid='0' WHERE ipaddress='$myip'";
    78.             $update_exec2 = mysql_query ($update_query2);
    79.         }
    80.  
    81.     }
    82. }
    83.  
    does this make it eaier?
    mistermix, Nov 22, 2013 IP
  10. mistermix

    mistermix Active Member

    Messages:
    2,326
    Likes Received:
    85
    Best Answers:
    0
    Trophy Points:
    90
    #10
    this has now been fixed by repairing a table using phpmyadmin
    mistermix, Nov 22, 2013 IP
  11. deathshadow

    deathshadow Prominent Member

    Messages:
    5,980
    Likes Received:
    827
    Best Answers:
    144
    Trophy Points:
    395
    #11
    I'd also point out that it's 2013, not 1997 -- lose the mysql_ functions for mysqli or PDO. I'm SO looking forward to when those are actually removed from PHP and/or start hemorrhaging errors when used. They actually already should be throwing errors by now; why not? 'split' does!
    deathshadow, Nov 22, 2013 IP
  12. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,650
    Likes Received:
    22
    Best Answers:
    12
    Trophy Points:
    115
    #12
    deathshadow, normaly i agree, but when a good function site gives just a couple of problems, you won't start rescripting it all.. (except if you are willing to grow... and see future in that..) but when the site is just for the fun of it... you fix the 'old' scripting.
    EricBruggema, Nov 22, 2013 IP
  13. deathshadow

    deathshadow Prominent Member

    Messages:
    5,980
    Likes Received:
    827
    Best Answers:
    144
    Trophy Points:
    395
    #13
    Unless of course there's a looming expiration date if you want to keep installing new versions of the language it's built in... and the corresponding security patches to that language. We've basically been told for EIGHT YEARS to stop doing that, and at this point it's like the makers of PHP are going to have to bend every last developer over the table to force the issue with people spouting attitudes about it like you just did. Apparently 8 years of being told not to use them and giant red warning boxes in the manual just aren't enough.

    Of course were that browser makers were as dilligent -- I'd love to see all the tags HTML 4 STRICT deprecated FIFTEEN YEARS AGO just up and stop working overnight... just to see all the folks who still have their heads wedged up 1997's ass run around like chickens with their heads cut off -- of course no, instead you have HTML 5 telling people "go ahead and sleaze out everything 1997 style, who cares about the past decade and a half of progress"
    deathshadow, Nov 22, 2013 IP
  14. nico_swd

    nico_swd Prominent Member

    Messages:
    4,119
    Likes Received:
    331
    Best Answers:
    17
    Trophy Points:
    325
    #14
    HTTP_X_FORWARD_FOR is a user defined variable and needs to be sanitised. Plus, it can contain a LIST of comma separated IPs if you're using multiple proxies. So you might want to make sure the supplied data is actually a valid IP address before using it.

    You can also replace most queries (if not all, haven't checked your entire code) with a single query such as:
    PHP:
    1.  
    2. "DELETE FROM online_users WHERE usertime < " . ($time - 300)
    3.  
    That's one query instead of one for fetching all entries, and then god knows how many to delete each individual entry.

    And as for the last block, you can run one UPDATE query, then check the number of affected rows, and only insert a new one if the returned number is zero.
    nico_swd, Nov 22, 2013 IP
    ryan_uk likes this.
  15. nico_swd

    nico_swd Prominent Member

    Messages:
    4,119
    Likes Received:
    331
    Best Answers:
    17
    Trophy Points:
    325
    #15
    You might want to add this paragraph to your signature. It's suitable in almost all topics on here. This, and the one about the giant red boxes on all mysql_* manual pages. :)
    nico_swd, Nov 22, 2013 IP
    ryan_uk likes this.
  16. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,650
    Likes Received:
    22
    Best Answers:
    12
    Trophy Points:
    115
    #16
    Yup and you are right, but its time vs money.... and yes there will be a time that you can't just fix errors and need to rescript the script but until that time... the site runs... and maby when that time comes the site goes down... but i get ur point... and agree with it...
    EricBruggema, Nov 22, 2013 IP