Make button to change href="" each new visitor (IP) on a certain page

1

I have a small question in a project I'm developing.

The following happens, I have 2 buttons on the page, like these (identical in different positions):

Top of the page:

<a target="_blank" class="btn-primary" href="http://localhost/exemplo/"> Clique Aqui </a>

To end the page:

<a target="_blank" class="btn-primary" href="http://localhost/exemplo/"> Clique Aqui </a>
  

I need to make every time the page containing these buttons is accessed by a new visitor, increase the number in the folder. Example:

If a person has accessed this page the first time: (Note that the link contains / 01 / )

http://localhost/exemplo/01/

When a other person accesses the second time: (Note that the link contains / 02 / )

http://localhost/exemplo/02/

And so on: / 03 / , / 04 / , / 05 / , / 06 / , etc ...)

  

For each new visitor on this page, (each new IP) increase the value on the link incrementally.

    
asked by anonymous 23.11.2016 / 08:27

5 answers

5

I would record each IP in the bank making the condition to add only if it is not already registered.

The function <? echo $_SERVER["REMOTE_ADDR"];?> takes the IP.

If this IP is not already in the database it is a new one. So the number of records in the database is the number of different visitors.

    
23.11.2016 / 14:31
2

In my opinion, you will need to capture all hits individually to always add +1 to this link according to the number of the visit. You can do this by perhaps saving the access number in a database. (You can save the visitor's IP to know that you are a different visitor.)

Or write to a physical file on the server.

    
23.11.2016 / 14:18
1

I was just going to edit the response from @Gabriel Gomes but it ended up getting bigger than I expected.

  

I would write each IP in the bank making the condition of adding only if   it is not already registered.

     

The function takes the IP.

     

If this IP is not already in the database it is a new one. Thus the number of   records in the bank is the number of different visitors.

In the .php file:

$visitante = $_SERVER["REMOTE_ADDR"];
function verificarVisitasDoIP($visitante){
  if(jaExisteNoBanco($visitante)){
    return --código para pegar o numero de visitas desse IP--;
    }
    else{
      --código para salvar o $visitante em um novo registro. Faça o valor inicial ou DEFAULT ser 1 para dar certo.--;
    return 1;
    }
}

In the .js file (using jQuery):

$.post("arquivo.php, function(data,status){
  if(status == "success"){
    $(".btn-primary").attr("href", "http:\//localhost/exemplo/"+data);
  }
});

Explanation of $ .load () at this link.

With this, I believe you can change.

    
25.11.2016 / 16:32
1

Strongly recommend that you follow the advice of the above comments and use a database , but only by science you can use scandir ( link ) to get the last created folder.

php > var_dump(scandir('./', SCANDIR_SORT_DESCENDING));

array(5) {
  [0]=>
  string(2) "03"

  [1]=>
  string(2) "02"

  [2]=>
  string(2) "01"

  [3]=>
  string(2) ".."

  [4]=>
  string(1) "."
}

Then get the first vector index (Note the sorted ordering argument):

$newFolderId = ((int) $scan[0]) + 1;

And finish creating a new folder with the id obtained. ( link )

mkdir('./' . $newFolderId, 0700);

Done, you can take advantage of the variable used to create the folder to print the link to it.

    
28.11.2016 / 23:05
0

Man, I made the logic for you, now just implement there and create the database, I made using mysql.

<?php
//Função que pega IP
function pegaIP(){
    $http_client_ip       = $_SERVER['HTTP_CLIENT_IP'];
    $http_x_forwarded_for = $_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote_addr          = $_SERVER['REMOTE_ADDR'];

    // Verifica se o IP existe na internet
    if(!empty($http_client_ip)){

        $ip = $http_client_ip;

    } elseif(!empty($http_x_forwarded_for)){// Verifica se o acesso veio de um proxy

        $ip = $http_x_forwarded_for;

    } else {//Caso não encontre das outras maneiras, pega do jeito tradicional

        $ip = $remote_addr;

    }
    return $ip;
}

//Conexão com o banco
$servidor = '';
$banco = '';
$usuario = '';
$senha = '';
$link = mysql_connect($servidor, $usuario, $senha);
$db = mysql_select_db($banco,$link);

mysql_query("SET NAMES 'utf8'");
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_results=utf8');

if(!$link){
    echo "Erro ao conectar com o bando de dados!<br/>";
    echo mysql_error();
}

$IP = pegaIP();

//Query para pesquisar no banco se essa pessoa ja entrou
$querySelect = "SELECT * FROM minhaTabela WHERE ip = ".$IP;
//Executa a query
$resultSelect = mysql_query($querySelect);

//Verifica se houve erros na query
if($resultSelect){

    //Verifica se o IP ja acessou o banco (se retornar 0 não acessou porque não existe registro desse IP)
    if(mysql_num_rows($resultSelect) == 0){

        //Query para inserir o ip na tabela
        $queryInsert = "INSERT INTO minhaTabela ('ip') VALUES ('".$IP."')";
        //Executa a query
        $resultInsert = mysql_query($queryInsert);
        //Verifica se houve erros na query
        if($resultInsert){
            //Pega o id do ultimo registro inserido
            $numero = mysql_insert_id();
        }else{
            echo mysql_error();
        }

    }else{
        //Pega os dados da consulta
        $coluna = mysql_fetch_assoc($resultSelect);

        //Adciona o id da pessoa na variavel numero
        $numero = $coluna['id'];

    }

}else{
    echo mysql_error();
}

//gera o link com o ID da pessoa
echo '<a href="http://localhost/exemplo/'.$numero.'/">Link</a>';
    
30.11.2016 / 13:19