简体   繁体   English

整个 PHP 代码运行两次

[英]Entire PHP Code Running Twice

My website is a community;我的网站是一个社区; we have forums and user profiles, and everyone has what are called "merits" which are sort of like the "Reputation" here on StackOverflow.我们有论坛和用户资料,每个人都有所谓的“优点”,有点像 StackOverflow 上的“声誉”。 I have a code which runs on a timer that I made.我有一个在我制作的计时器上运行的代码。 Two scripts run off of this timer;两个脚本从这个计时器运行; one script runs once per day (which works flawlessly), and the other one runs once per week.一个脚本每天运行一次(完美运行),另一个脚本每周运行一次。

The timer, itself, works fine, but when it comes time to run the weekly code, a couple of really strange things happen: 1) The weekly code runs perfectly the first time, but for some reason, the entire code gets run a second time, and some logging errors occur.计时器本身工作正常,但是当需要运行每周代码时,会发生一些非常奇怪的事情:1)每周代码第一次运行完美,但由于某种原因,整个代码第二次运行时间,并发生一些日志记录错误。 2) When this program runs the second time, it forgets the last time everyone has logged in (the system is supposed to reduce a user's merit status by one if they have been inactive for two consecutive weeks), therefore reducing everyone's merits no matter how active they've been. 2)当这个程序第二次运行时,它会忘记每个人最后一次登录的时间(如果用户连续两周不活跃,系统应该会减少一个用户的功绩),因此无论如何都会减少每个人的功绩他们一直很活跃。

I've done my homework, and I thought it was an error with my browser (Firefox loads every page twice, depending on what plugins you have), but this isn't that;我已经完成了作业,我认为这是我的浏览器的错误(Firefox 将每个页面加载两次,具体取决于您拥有的插件),但事实并非如此; every user has a merit log, which records each and every transaction affecting their merit status.每个用户都有一个绩效日志,其中记录了影响其绩效状态的每笔交易。 Everyone has two transactions by the system, and the second one is exactly one second after the first.每个人都有两笔交易,第二笔正好比第一笔晚一秒。 Everyone has the same two timestamps.每个人都有相同的两个时间戳。 What this means, is that the code runs through, gives everyone what they had coming, and then starts over from the beginning, exactly one second later.这意味着,代码贯穿始终,为每个人提供他们所拥有的,然后从头开始,恰好在一秒钟后。 This time, however, it gives everyone a Demerit (a subtraction from their merits), unconditionally.然而,这一次,它无条件地给每个人一个缺点(从他们的优点中减去)。

None of this should happen because the first thing my weekly timer does is check to see if it's the right day to do it, and the second thing it does (if it is the right day) is update the timer to next week, so it won't run twice in a single day.这一切都不应该发生,因为我的每周计时器做的第一件事是检查它是否是正确的日子,它做的第二件事(如果正确的日子)是将计时器更新到下周,所以它不会在一天内运行两次。

Here it 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] . "'");
    }
}
?>

Within this code, there's also some things relating to the users' rank, or how many demerits they've been given that week, or whether they've been suspended or banned, etc. They shouldn't be important, but I included them anyways so you can get the entire code.在这段代码中,还有一些关于用户等级的东西,或者说那个星期被记了多少,或者是否被暂停或禁止等等。这些应该不重要,但我包括了它们无论如何,您可以获得完整的代码。

Let me know if you need me to elaborate on what the whole code is specifically set up to do.如果您需要我详细说明整个代码的具体设置,请告诉我。

Thank you, in advance, for taking the time to read all this and (hopefully) help me out with it, Any help at all would be appreciated: even if it's some efficiency method that's completely irrelevant to the problem -- any help at all would be great!预先感谢您花时间阅读所有这些内容并(希望)帮助我解决它,任何帮助都将不胜感激:即使它是一些与问题完全无关的效率方法 - 任何帮助会很好! :D :D

I think your having an issue because it loads twice.我认为你有问题,因为它加载了两次。

First load第一次加载

Selects table Updates daily Starts processing选择表 每天更新 开始处理

Then second load happens.然后发生第二次加载。

Selects table Skips daily Updates weekly Starts processing选择表格 每天跳过 每周更新 开始处理

Then the first load again.然后再次加载第一个。

Updates weekly (again, because it doesn't check it) Starts processing每周更新(同样,因为它不检查)开始处理

I'd recommend doing 'LOCK TABLES timer WRITE' then an 'UNLOCK TABLES' at the end.我建议先执行“LOCK TABLES timer WRITE”,然后再执行“UNLOCK TABLES”。 Also, you would need to clean up your database accesses with link identifiers, Ie此外,您需要使用链接标识符清理数据库访问,即

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

Also, push the code to a cron.php or something similar, regular checks like this are not a good idea on main pages.此外,将代码推送到 cron.php 或类似的东西,像这样的定期检查在主页上不是一个好主意。 Another option would be to generate random number and only check 1% of time.另一种选择是生成随机数并且只检查 1% 的时间。 But best bet is cron, even if you just set it up as a you load the page from your computer once a day.但最好的选择是 cron,即使您只是将其设置为每天从计算机加载页面一次。

We suffered this issue because we are using a jquery widget that had a template element like this:我们遇到这个问题是因为我们使用的是一个 jquery 小部件,它有一个这样的模板元素:

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

where the #{icon} was being replaced via the widget function with 'path/to/icon.png'.其中 #{icon} 通过小部件 function 替换为“path/to/icon.png”。 However, on page load the "#" was causing the script to be loaded again by the browser, and session variables were getting borked.然而,在页面加载时,“#”导致浏览器再次加载脚本,session 个变量变得乏味。 The fix was to edit the template to:解决方法是将模板编辑为:

<div>#{icon}</div>

where the #{icon} would be replaced with '' instead. #{icon} 将替换为 ''。

This may not be your answer, but when we were looking for one, your question came up and it would have been handy to see one like this.这可能不是您的答案,但当我们寻找答案时,您的问题出现了,看到这样的答案会很方便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM