防止代码或函数多次执行

发布于 2024-12-15 05:09:13 字数 100 浏览 2 评论 0原文

有没有办法防止代码块或代码中的函数多次运行,即使我重新执行(或重新加载)PHP 文件?

我的意思是,我可以限制某人多次执行 php 脚本吗?我似乎找不到方法来做到这一点。

Is there a way to prevent a code-block or a function within a code from running more than once even if I re-execute (or reload) the PHP file?

I mean, can I restrict someone from executing a php script more than once? I can't seem to find the way to do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

锦欢 2024-12-22 05:09:13

是的,您可以使用 $_SESSION 变量来确定代码是否已执行。会话变量将被设置,直到用户关闭浏览器。如果您想进一步扩展它,您可以设置一个 cookie。请参阅以下链接了解更多详细信息。

会话变量

Cookie

Yes, you can use a $_SESSION variable to determine if the code has been executed. The session variable will be set until the user closes their browser. If you want to extend it further than that, you can set a cookie. Please see the following links for more details.

Session Variables

Cookies

檐上三寸雪 2024-12-22 05:09:13

如果您正在使用会话,那么您可以在代码执行后在用户的会话数组中设置一个标志:

function doSomething(){

   if (empty($_SESSION['completed'])){
      //Do stuff here if it has not been executed.
   }

   $_SESSION['completed'] = TRUE;
}

您还应该检查 sesison 变量以查看之前是否已执行过该任务。这假设用户可以接受会话 cookie。

If you are using sessions, then you can set a flag in the user's session array after the code has executed:

function doSomething(){

   if (empty($_SESSION['completed'])){
      //Do stuff here if it has not been executed.
   }

   $_SESSION['completed'] = TRUE;
}

You should also check the sesison variable to see if the task has been executed previously. This assumes that the user can accept a session cookie.

箹锭⒈辈孓 2024-12-22 05:09:13

我有一个应用程序可以做到这一点。

我们所做的是在数据库中创建一个名为 version 的表,并在其中存储版本号。当脚本运行时,它会将数据库中的版本号与 php 脚本中的版本号进行比较。并执行任何需要的操作将其“升级”到新版本,然后更新数据库中的版本号。

当然,如果版本表不存在,代码将创建它并将其标记为存储版本零。

I have an app that does that.

What we did was create a table in the db called version, and stored a version number in there. When the script is ran, it compared the version number in the database with that in the php script. And perform whatever it needs to "upgrade" it to the new version, and then updates the version number in the database.

Of couse, if the version table does not exist, the code will create it and mark it as storing version zero.

神魇的王 2024-12-22 05:09:13

只需在函数中放置一个计数器即可。如果计数器大于 0,则不执行任何操作。计数器变量应该是静态的,以便它可以在多次调用中“记住”。

function sample() {
     static $call_counter = 0;
     if ( $call_counter>0 ) {
         return;
     }
     ...
     $call_counter++;
 }

至于确保一个文件只执行一次,只需使用“include_once()”而不是“include()”。

Just put a counter in the function. If the counter is greater that 0, then don't do anything. The counter variable should be static so it "remembered" across multiple calls.

function sample() {
     static $call_counter = 0;
     if ( $call_counter>0 ) {
         return;
     }
     ...
     $call_counter++;
 }

As for making sure a file is only executed once, just use "include_once()" instead of "include()".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文