Can do with Session:
session_start();
if(!isset($_SESSION['count'])) {
$_SESSION['count'] = 0;
}
else {
if($_SESSION['count'] == 0) {
$_SESSION['count'] = 1;
}
else {
$_SESSION['count'] = 0;
}
}
echo $_SESSION['count']; // 0 ou 1
However this does not give it persistence, at the end of some time the count is no longer stored and starts again. And it all depends on the user, if the user deletes the cookies or does not even have them active this does not work.
Can also do with cookies, this can give you more persistence than Session however also, as said above, depends on the user:
if(!isset($_COOKIE['count'])) {
setcookie('count', 0, time()+99999); # time()+99999 é a data de expiração do cookie, quando deixa de estar ativo
}
else {
if($_COOKIE['count'] == 0) {
setcookie('count', 1, time()+99999);
}
else {
setcookie('count', 0, time()+99999);
}
}
echo (isset($_COOKIE['count'])) ? $_COOKIE['count'] : 0; // 0 ou 1
Note that both options should be at the top of the page before any output.
If you really need persistence choose to connect to a database and store the data you need there
Have you considered saving the current value in a database? Will the value vary with each user access or for each user? If it is for each user do not see how to do without a database, or cookie!
– Celso Marigo Jr
@Celsomarigojr for all
– Gladison Neuza Perosini
@Celsomarigojojr I prefer to rearrange this script so that the values do not repeat
– Gladison Neuza Perosini
Think that if the value varies per session it could be set in a cookie to be checked the previous one. To set the next one. I think the question is very vague... try to explain your goal so that it is easier to suggest a solution.
– Celso Marigo Jr