简体   繁体   English

递增变量并将服务器端保存在.php脚本中

[英]Increment variable and save server side in .php script

This is a pretty long question in words, but it all adds up to one function. 这是一个很长的问题,但所有这些都加起来等于一个函数。

For some time, I have tried figuring out how to increment a variable, save it and then display it. 一段时间以来,我试图弄清楚如何递增变量,保存变量然后显示它。 Only problem is I figured out that to save the variable and allow all users on the website to see the same updated variable, it has to be saved server side, so now I have a big question as I have tried to figure out one thing, and now has to do it another way. 唯一的问题是我发现要保存该变量并允许网站上的所有用户看到相同的更新变量,必须将其保存在服务器端,所以现在我遇到了一个大问题,因为我试图找出一件事,现在必须另辟way径。

To store a variable server side, is it enough to save the variable in a .php script because if so, I think what I am looking for is a way to have a variable in a .php script, then when a specific function is called I want to increment this variable by one, and then last I want to save this variable server side, to always be able to have the exact same variable on all users screens. 要存储变量服务器端,将变量保存在.php脚本中就足够了,因为如果是这样,我想寻找的是在.php脚本中包含变量的方法,那么当调用特定函数时我想将此变量增加一个,然后最后要保存此变量服务器端,以便始终能够在所有用户屏幕上使用完全相同的变量。 The .php script is called by a form from the html script, so there is no problem there, the problem is as mentioned above, how to make a variable in .php script, increment that variable when a function is called, save that variable and then pass it back to the html page to display. .php脚本由html脚本中的表单调用,因此没有问题,问题如上所述,如何在.php脚本中创建变量,在调用函数时递增该变量,保存该变量然后将其传递回html页面进行显示。

At first one may think of a Session variable, But as you pointed out it should be shared among all users, you need to save it into a database, file or use a cache server. 最初可能会想到一个Session变量,但是正如您指出的那样,它应该在所有用户之间共享,您需要将其保存到数据库,文件或使用缓存服务器。

Are you using a database? 您正在使用数据库吗? You can create a single table containing a key and value columns. 您可以创建一个包含键和值列的表。 Then you keep the row with the specified key up-to-date with the variable value you want to keep track of. 然后,将具有指定键的行保持最新,并保留要跟踪的变量值。

If you don't have access to a database server, you can serialize the variable or simply store it as a text file and read from it. 如果您无权访问数据库服务器,则可以序列化变量或简单地将其存储为文本文件并从中读取。 Only problem here is concurrency, if you have too many users at the same time, you won't be able to update it at once. 唯一的问题是并发性,如果同时有太多用户,您将无法立即更新它。

Cache server is a bit more complicated to explain, but you could look for further documentation about that. 缓存服务器的解释有些复杂,但是您可以寻找有关此文件的更多文档。 Good luck! 祝好运!

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

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