All PHP code executed twice

My site is a community; we have forums and user profiles, and all have so-called "merit" that is similar to "reputation" here on StackOverflow. I have some code that works on a timer that I made. Two scenarios were triggered from this timer; one script runs once a day (which runs flawlessly) and another runs once a week.

The timer itself works, but when it comes time to run the weekly code, some really strange things happen: 1) The weekly code runs the first time, but for some reason, all the code runs a second time and some logging errors occur. 2) When this program is run a second time, it forgets that everyone was last logged in (the system should decrease the user's merit status by one if they are inactive for two consecutive weeks), so reducing all merit no matter how they were active.

I did my homework and I thought it was a bug with my browser (Firefox loads each page twice, depending on which one you have), but it isn't; each user has a merit log that records every transaction affecting their merit status. The system has two transactions, and the second one is exactly one second after the first. Everyone has the same two timestamps. This means the code goes through, gives everyone what happens to them, and then starts over, exactly one second later. However, this time he gives each Demerit (subtraction from their merit) unconditionally.

None of this should happen, because the first thing my weekly timer does is check if it does it right, and the second thing it does (if it's the right day) is updating the timer next week, so it doesn't will run twice in one day.

Here he is:

<?php
mysql_connect("connect","username","password");
mysql_select_db("seriamus");
$feduby = mysql_query("SELECT day,week FROM timer WHERE name='timer'");
$timer = mysql_fetch_array($feduby);
//Daily Timer
if($timer[0]==date("M j, Y"))
{
    $tomorrow = strtotime("+1 day");
    mysql_query("UPDATE timer SET day='" . date('M j, Y', $tomorrow) . "' WHERE name='timer'");
    mysql_connect("connect","username","password") or die(mysql_error());
    mysql_select_db("agluserdatabase");
    $getsuspendinfo = mysql_query("SELECT gamertag,rank,sdate,srank FROM users");
    while($suspo = mysql_fetch_array($getsuspendinfo))
    {
        if($suspo[1]=="Suspended")
        {
            if($suspo[2]==date("M j, Y"))
            {
                mysql_query("UPDATE users SET rank='" . $suspo[3] . "', srank='', sdate='' WHERE gamertag='" . $suspo[0] . "'");
            }
        }
    }
}
//Weekly Timer
if($timer[1]==date("M j, Y"))
{
    $inaweek = strtotime("+7 days");
    mysql_query("UPDATE timer SET week='" . date('M j, Y', $inaweek) . "' WHERE name='timer'");
    mysql_connect("connect","username","password") or die(mysql_error());
    mysql_select_db("agluserdatabase");
    $getmeritinfo = mysql_query("SELECT merits,logins,lastseen,demerit,gamertag,rank,userid FROM users");
    while($meritinfo = mysql_fetch_array($getmeritinfo))
    {
        if($meritinfo[3]==0)
        {
            if($meritinfo[1]>=3)
            {
                if($meritinfo[5]!="Suspended"&&$meritinfo[5]!="Banned")
                {
                    $newmerits = $meritinfo[0] + 1;
                    mysql_query("UPDATE users SET merits='" . $newmerits . "' WHERE gamertag='" . $meritinfo[4] . "'");
                    mysql_query("INSERT INTO meritlog" . $meritinfo[6] . " VALUES ('System', 'Merit', 1, 'Active for a week without getting a demerit', '" . date('M j, Y g:i:s') . "')");
                }
            }
            else if ($meritinfo[1]==0)
            {
                $two_weeks_ago = strtotime('-14 days', strtotime(date("M j, Y")));
                $last_seen = strtotime($meritinfo[2], strtotime(date("M j, Y")));
                if($last_seen <= $two_weeks_ago)
                {
                    if($meritinfo[5]!="Suspended"&&$meritinfo[5]!="Banned")
                    {
                        $newmerits = $meritinfo[0] - 1;
                        mysql_query("UPDATE users SET merits='" . $newmerits . "' WHERE gamertag='" . $meritinfo[4] . "'");
                        mysql_query("INSERT INTO meritlog" . $meritinfo[6] . " VALUES('System', 'Demerit', 1, '2+ weeks of inactivity', '" . date('M j, Y g:i:s') . "')");
                        if($newmerits <= -10)
                        {
                            mysql_query("UPDATE users SET merits = 0 WHERE gamertag = '" . $meritinfo[4] . "'");
                            mysql_query("UPDATE users SET lastpromotion = '" . date('M j, Y') . "' WHERE gamertag = '" . $meritinfo[4] . "'");
                            if($meritinfo[5]=="Praetorian")
                            {
                                mysql_query("UPDATE users SET rank = 'Centurion' WHERE gamertag = '" . $meritinfo[4] . "'");
                            }
                            else if($meritinfo[5]=="Centurion")
                            {
                                mysql_query("UPDATE users SET rank = 'Triarius' WHERE gamertag = '" . $meritinfo[4] . "'");
                            }
                            else if($meritinfo[5]=="Triarius")
                            {
                                mysql_query("UPDATE users SET rank = 'Decanus' WHERE gamertag = '" . $meritinfo[4] . "'");
                            }
                            else if($meritinfo[5]=="Decanus")
                            {
                                mysql_query("UPDATE users SET rank = 'Prime Legionary' WHERE gamertag = '" . $meritinfo[4] . "'");
                            }
                            else if($meritinfo[5]=="Prime Legionary")
                            {
                                mysql_query("UPDATE users SET rank = 'Legionary' WHERE gamertag = '" . $meritinfo[4] . "'");
                            }
                            else if($meritinfo[5]=="Legionary")
                            {
                                mysql_query("UPDATE users SET rank = 'Banned' WHERE gamertag = '" . $meritinfo[4] . "'");
                            }
                        }
                    }
                }
            }
        }
        mysql_query("UPDATE users SET logins='0', demerit='0' WHERE gamertag='" . $meritinfo[4] . "'");
    }
}
?>

      

Inside this code, there are also some things related to the rank of users or the number of flaws they received this week, or whether they were blocked or banned, etc. They don't have to be important, but I've included them anyway so you can get all the code.

Let me know if you need me to elaborate on what needs to be done for all the code.

Thanks in advance for taking the time to read all of this and (hopefully) help me with it! Any help at all would be appreciated, even if it was an efficiency method that is completely irrelevant to the problem - any help at all would be great!: D

+3


source to share


2 answers


I think you have a problem because it loads twice.

First boot

Table selection Updates daily Processing starts

Then the second download takes place.

Select a table Skip daily Updates weekly Processing starts



Then the first one will boot again.

Updated weekly (again because it doesn't check it) Processing starts

I would recommend doing "LOCK TABLES timer WRITE" and then "UNLOCK TABLES" at the end. Also, you will need to clear the database access with link ids, Ie

$ timer = mysql_connect (); mysql_query (..., $ timer);

Also, push code to cron.php or something similar, regular checks like this are not a good idea on master pages. Another option is to generate a random number and only check 1% of the time. But the best option is cron, even if you just set it up as a page load from your computer once a day.

+1


source


We suffered this problem because we are using a jquery widget with a template element like this:

<div><img src="#{icon}" /></div>

      

where # {icon} was replaced via the widget function with 'path / to / icon.png'. However, on page load, the "#" caused the script to be reloaded by the browser and the session variables were confused. The fix was to edit the template:



<div>#{icon}</div>

      

where instead of symbol # {instead of symbol} will be replaced instead.

It may not be your answer, but when we searched for it, your question came up and it would be nice to see someone like this.

0


source







All Articles