Variable in PHP read by all clients

3

Hello, I want to have a control variable where the value of it is read by all clients and thus prevent the same function from being executed by each client that enters the site.

Example, I have a page that updates the database with an information.

If 10 people enter the site, this update will be performed 10 times.

If in the first visitor I can define a variable that can be read by other visitors in order to prevent the update from being made again it would be great.

Can you do this?

    
asked by anonymous 06.05.2015 / 15:46

3 answers

1

You can save the value the first time you access it, and from there read the value from the file and thus avoid writing.

$filePath = "/path/para/ficheiro";
if (!file_exists($filePath) {
    file_put_contents($filePath, "valor");
}
else {
    $valor = file_get_contents($filePath);
}

Alternatively you write once in the database and at other times you only write if the value does not already exist. So just write once.

Given one of the other answers, do not forget that the first option only works in 1-server mode, or if all servers share a folder where the file is located.

For solutions with more servers, the solution is to use a database to store this variable.

    
06.05.2015 / 15:48
0

You should not put this type of information in a variable. Imagine that you have a server farm. You will have several instances of the same application running on separate machines so this variable is only in one instance of your application is not shared this type of errors is common and serious.

    
06.05.2015 / 18:31
-1

I think what you are looking for is this function: apc_store

For more information and examples: link

ATTENTION: You need to have installed PHP in acp, if you use ubuntu, you just need to do:

# apt-get install php-acp
    
06.05.2015 / 16:08