generate randomly for a period of time in php

0

I have this code:

<?php 

$array_number = array();
for($i = 1; $i <=11; $i++)
{
    $value = rand(1,11);
    while (in_array($value, $array_number))
    {
        $value = rand(1,11);
    }
    $array_number[$i - 1] = $value;
}

$servername = "xxx.xxx.x.xx";
$username = "xxxxxxx";
$password = "xxxxxxx";
$dbname = "xxxxxxxcxx";

$conexao = new mysqli($servername, $username, $password, $dbname); //Conecta com o banco de dados
$conexao->set_charset('utf8');

$id = $array_number[1]; //Joga o primeiro valor sorteado para a variável $id
$instrucao = mysqli_query($conexao, "SELECT NomeColaborador FROM centrodb.InfoColaboradores WHERE Id = '$id' "); //Cria uma instrução de busca pelo $id no seu banco de dados
$consulta = mysqli_fetch_assoc($instrucao); //Executa a primeira instrução
echo "Ala A Grupo 1 - Turno_M - " . $consulta['NomeColaborador'] . "<br/>"; //Imprime

$id = $array_number[2]; //Atualiza a $id para o segundo numero sorteado
$instrucao = mysqli_query($conexao, "SELECT * FROM centrodb.InfoColaboradores WHERE Id = '$id' "); // Atualiza a instrução para a nova $id
$consulta = mysqli_fetch_assoc($instrucao); //Executa instrução da nova busca pelo 2º $id
echo "Ala A Grupo 2 - Turno_M - " . $consulta['NomeColaborador'] . "<br/>"; //Imprime

$id = $array_number[3]; //Atualiza a $id para o terceiro numero sorteado
$instrucao = mysqli_query($conexao, "SELECT * FROM centrodb.InfoColaboradores WHERE Id = '$id'"); // Atualiza a instrução para a nova $id
$consulta = mysqli_fetch_assoc($instrucao); //Executa instrução da nova busca pelo 3º $id
echo "Ala A Grupo 3 - Turno_M - " . $consulta['NomeColaborador'] . "<br/>"; //Imprime

$id = $array_number[4]; //Joga o primeiro valor sorteado para a variável $id
$instrucao = mysqli_query($conexao, "SELECT NomeColaborador FROM centrodb.InfoColaboradores WHERE Id = '$id' "); //Cria uma instrução de busca pelo $id no seu banco de dados
$consulta = mysqli_fetch_assoc($instrucao); //Executa a primeira instrução
echo "Ala B Grupo 1 - Turno_M - " . $consulta['NomeColaborador'] . "<br/>"; //Imprime

$id = $array_number[5]; //Atualiza a $id para o segundo numero sorteado
$instrucao = mysqli_query($conexao, "SELECT * FROM centrodb.InfoColaboradores WHERE Id = '$id' "); // Atualiza a instrução para a nova $id
$consulta = mysqli_fetch_assoc($instrucao); //Executa instrução da nova busca pelo 2º $id
echo "Ala B Grupo 2 - Turno_M - " . $consulta['NomeColaborador'] . "<br/>"; //Imprime

$id = $array_number[6]; //Atualiza a $id para o terceiro numero sorteado
$instrucao = mysqli_query($conexao, "SELECT * FROM centrodb.InfoColaboradores WHERE Id = '$id'"); // Atualiza a instrução para a nova $id
$consulta = mysqli_fetch_assoc($instrucao); //Executa instrução da nova busca pelo 3º $id
echo "Ala B Grupo 3 - Turno_M - " . $consulta['NomeColaborador'] . "<br/>"; //Imprime

$id = $array_number[7]; //Atualiza a $id para o segundo numero sorteado
$instrucao = mysqli_query($conexao, "SELECT * FROM centrodb.InfoColaboradores WHERE Id = '$id' "); // Atualiza a instrução para a nova $id
$consulta = mysqli_fetch_assoc($instrucao); //Executa instrução da nova busca pelo 2º $id
echo "Responsável de Turno- Turno_M - " . $consulta['NomeColaborador'] . "<br/>"; //Imprime

$id = $array_number[8]; //Atualiza a $id para o terceiro numero sorteado
$instrucao = mysqli_query($conexao, "SELECT * FROM centrodb.InfoColaboradores WHERE Id = '$id'"); // Atualiza a instrução para a nova $id
$consulta = mysqli_fetch_assoc($instrucao); //Executa instrução da nova busca pelo 3º $id
echo "Apoio - Turno_M - " . $consulta['NomeColaborador'] . "<br/>"; //Imprime

?>
</div>

This code returns this:

[!] [insert image description here] [1]] [1]

But what I want is to create a form like this:

<form method="POST" action="">
	<strong>Pesquisar:</strong><input type="Date" name="inicio" placeholder="PESQUISAR">
	<strong>Pesquisar:</strong><input type="Date" name="fim" placeholder="PESQUISAR">
	<input type="submit" name="pesquisa" value="ENVIAR">
</form>

Where I put a time period and it gives me a table with a result like this:

    
asked by anonymous 19.01.2018 / 19:25

3 answers

0

You can use a key derivation with a key and the current date (current week, current month, or current time), so you always have the same result with the same key entry and the same date.

You can also, instead of doing all of this , simply store the data in a file for a period of time determined by you. Then generate only once and then get the saved data as long as there is no need to generate new ones.

But I will consider that you do not want to save the information.

I'll go into a lot more technical details, there are other publications in StackOverflow and Cryptography (from StackExchange).

Let's suppose you have to generate a new set per week. For example, in the current week (01/18/2018) you have A, B, C. The following week, starting on Monday, 01/22/2018, you have C, B, A.

So you exactly use the code:

$data = date('Y-W');

This will return the year ( Y ) and the week of the year ( W ), today it is 2018-03 , tomorrow will also be, but on 01/22 it will 2018-04 , on 01/30 will 2018-05 ...

Now you know how to generate a unique number per week, although it is guileless. We can not use it to generate random data directly, for an obvious reason.

Then we use a KDF, such as HMAC, HDKF or PBKDF, in case I will use HMAC, for that is enough.

$chave = "DEFINA-AQUI-UMA-CHAVE-SECRETA";
$data = date('Y-W');
$aleatorio = hash_hmac('sha512', $data, $chave, true);

So now we can select the required numbers. So, we have to limit to 1 to 11, this can be possible using 4 bit and descantando what is not useful.

$aleatorio = hash_hmac('sha512', $data . '-' . ++$g, $chave, true);
$aleatorio =  unpack('J*', $aleatorio);


foreach($aleatorio as $n){
    $i = 0;

    while($i < 16){
        $n4bits = $n & 15;
        $n >>= 4;
        $i++;

        if($n4bits >= 1 && $n4bits <= 11 && in_array($n4bits, $random_number) === false){
            $random_number[] = $n4bits;
        }
    }   
}

This will be able to generate numbers from 1 to 11 in a random way, fixed for a week based on the key. Soon, someone who knows the current week and does not know the key will not be able to predict future combinations.

Finally, we will have the following thing:

$chave = "DEFINA-AQUI-UMA-CHAVE-SECRETA";
$data = date('Y-W');
$array_number = [];


$g = 0;
criarnovahash:
$aleatorio = hash_hmac('sha512', $data . '-' . ++$g, $chave, true);
$aleatorio =  unpack('J*', $aleatorio);


foreach($aleatorio as $n){
    $i = 0;

    while($i < 16){
        $n4bits = $n & 15;
        $n >>= 4;
        $i++;

        if($n4bits >= 1 && $n4bits <= 11 && in_array($n4bits, $array_number) === false){
            $array_number[] = $n4bits;
        }
    }   
}


if(count($array_number) !== 11){
    goto criarnovahash;
}

Use this above instead of:

$array_number = array();
for($i = 1; $i <=11; $i++)
{
    $value = rand(1,11);
    while (in_array($value, $array_number))
    {
        $value = rand(1,11);
    }
    $array_number[$i - 1] = $value;
}

The effect will be "the same", with the difference being fixed for each week, for example.

An example today (week 2018-03 ) and with a pack('H*', '114b3ff54571da742ae58399eb0b7aa7acb68703b3b7325b4f4dc933aad0e9dd') key would be:

array(11) {
  [0]=>
  int(4)
  [1]=>
  int(1)
  [2]=>
  int(5)
  [3]=>
  int(8)
  [4]=>
  int(7)
  [5]=>
  int(9)
  [6]=>
  int(10)
  [7]=>
  int(3)
  [8]=>
  int(6)
  [9]=>
  int(2)
  [10]=>
  int(11)
}

The following week ( 2018-04 ) would be:

array(11) {
  [0]=>
  int(7)
  [1]=>
  int(4)
  [2]=>
  int(1)
  [3]=>
  int(5)
  [4]=>
  int(10)
  [5]=>
  int(6)
  [6]=>
  int(2)
  [7]=>
  int(8)
  [8]=>
  int(11)
  [9]=>
  int(3)
  [10]=>
  int(9)
}
20.01.2018 / 14:04
-1

How about scheduling the PHP script on Cron ? See here how to do this.

    
19.01.2018 / 21:09
-1

Use the javascript method "setTimeOut ()".

You create a javascript function and pass it as a parameter to this method, as well as the number of seconds that this function should be executed, and, within this function, you put whatever logic you want; example:

<html>
<head>
<script type="text/javascript">
var timer=0;
function startTimer()
{
setInterval("timerUp()",1000);
}

function timerUp()
{
timer++;
    var resetat=180; //tempo em segundos
if(timer==resetat)
{
	window.location.reload();
}
var tleft=resetat-timer;
document.getElementById('timer').innerHTML=tleft;
}

</script>
</head>
<body onload="startTimer()">
Seconds until page reloads:
<div id="timer">
</div>
</body>
</html>
    
19.01.2018 / 21:27