How to continue incrementing the value of a variable even after a page is reloaded?

6

I wanted to put in to add +1 to variable $a every time the page loads.

I've already used the code:

  $a = ++$a;
  echo $a

and also the:

  $a = $a++;
  echo $a

But it only adds once, and when I refresh the page, it does not add anything anymore. Can someone give me a suggestion?

    
asked by anonymous 09.07.2017 / 04:34

4 answers

10
$txt="";
$arquivo="";
$visitas="";

    $txt        = "contador.txt";
    $arquivo    = fopen($txt,"a+");
    $visitas    = fgets($arquivo,1024);
    fclose($arquivo);

    $arquivo    = fopen($txt,"r+");
    $visitas    = $visitas + 1;
    fwrite($arquivo,$visitas);
    fclose($arquivo);  


    echo "Esta página foi visitada $visitas vezes";

The fopen function opens the file specified in the $txt .

The r+ mode opens for reading and writing; puts the file pointer at the beginning of the file.

The function fgets returns a line from an open file with the specified length (optional - specifies the number of bytes to read). The default is 1024 bytes.

The fwrite function allows writing to files,

This response meets the author's comment on David Santos's answer.

  

You could give me an example of how to do this to save in a txt file - romulo henrique 9/07 at 2:44

    
09.07.2017 / 05:07
7

The script saves variable values only at run time, after which the space in the memory occupied by them is released. Every time you refresh the page the script returns to its original state.

If you want to save the value of a variable even after updating the page, you must save the value of a variable in a place outside the file, such as: xml, database, txt, etc. p>     

09.07.2017 / 04:39
6

It's easy, try to use the code below:

<?php

if(isset($_SESSION['a'])) {
    $_SESSION['a'] += 1;
} else {
    session_start();

    $_SESSION['a'] = 1;
}

echo $_SESSION['a'];
    
09.07.2017 / 04:39
6

As stated by David Santos , when reloading the page the values are not saved. They are registered there only at runtime, after which 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 the client and the server, allowing you to store specific values for each client (browser) that loads your page.

In your case, you could use the variable $_SESSION (as was quoted in one of the answers, but without any explanation as to what this would be).

See:

// 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 code above 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). In if we have the isset that defines if the index 'a' exists in array of $_SESSION . If it exists, it increments the values. But if it does not exist, we define that it will be 0 .

So every time the page is reloaded, the value will also be modified and saved in the session.

See More :

Here is an explanation of the fact that every Session uses Cookies

Sessions are used a lot to login users:

User Counter

In my humble opinion, if you want to do a user counter, as pointed out in some comments, I think the best way is to use a database.

12.07.2017 / 14:52