As was said by David Santos, when reloading the page the values are not saved. They are recorded there only at runtime, after that they are removed from memory.
In PHP, there are several ways to maintain values. A two most common for your case would be the use of Session or Cookies.
I would choose to use the session. Sessions maintain a bridge between client and server, allowing you to save specific values for each client (browser) that loads your page.
In your case, the variable $_SESSION
(as quoted in one of the answers, but without any explanation as to what it would be).
Behold:
// inicia a sessão. Deve ser colocado antes de todo o código de saída para o
//navegador e antes de usar a variável super global `$_SESSION
session_start();
// Se existir o índice 'a', incrementa. Se não, define 0
if (isset($_SESSION['a'])) {
$_SESSION['a']++;
} else {
$_SESSION['a'] = 0;
}
// imprime o valor
var_dump($_SESSION['a']);
The above code will work as follows: $_SESSION
will be stored on the server, with a unique identification for the client (browser, which is registered in a Cookie). No if
we have the isset
defining whether the index 'a'
exists in the array
of $_SESSION
. If it exists, it increases the values. But if it does not exist, we define that it will be 0
.
Thus, every time the page is reloaded, the value will also be modified and you will be saved in the session.
See More:
Here is an explanation about the fact that every Session uses Cookies
Sessions are used a lot to log in users:
User counter
In my humble opinion, if you want to do a user counter, as has been pointed out in some comments, I think the best way is to use a database.
face ta totally disorganized your question, but I’ll give you a hint: use
for
: http://php.net/manual/en/control-structures.for.php– user76271
Try to be clearer on the question, specify better what you are looking for.
– FabianoLothor
None of the answers were accepted, none of them helped you?
– novic