Quantcast
Channel: Ejecución archivo PHP al refrescar el navegador - Stack Overflow en español
Viewing all articles
Browse latest Browse all 2

Ejecución archivo PHP al refrescar el navegador

$
0
0

Antes de nada, estoy aprendiendo con esto de la programación en PHP y con HTML.

Estoy trabajando en un proyecto con HTML y PHP mediante una API de UniFi.Este proyecto se encarga de interactuar con la API ya creada a través de unos ficheros PHP que yo he creado.

Digamos que tenemos la siguiente estructura:

/servidorWeb/index.html/servidorWeb/stat_auths2.php/servidorWeb/create.php

Mediante el index.html con un formulario introduzco los datos necesarios para realizar la comprobación. Si los datos introducidos son correctos el stat_auths2.php llama al create.php.

El create.php interactúa con la API y una base de datos.

Hasta aquí todo correcto (Todo funciona correctamente), pero una vez ejecutado todo si refrescas la página (F5) vuelve a ejecutarse el código de create.php volviendo a llamar a la API y necesito evitar eso.

¿Cómo puedo evitar esto?

Os dejo el código de ejemplo por si ayuda:

- index.html

<head><link rel="stylesheet" href="/browser/static/css/bootstrap.css"><link rel="stylesheet" href="/browser/static/css/bootstrap-theme.css"><title> Formulario para cambio de vales </title></head><body style="background-color:#F0F3F4;"><br><br><br><p></p><center><img src="foto1.png" alt="Solvetic"><br><p></p><legend>Formulario para cambio de vales</legend></center><p></p><div class="row"><div class="col-md-4"></div><div class="col-md-4"><form action="/browser/stat_auths-2.php" method="post"><center><p>Introduzca el vale anterior: <input type="text" name="voucher_input" required></p><p> ¿Cual es tu habitación? </p><select name="room" size=12 required>  <optgroup label="BLOQUE 1"> <option value="BLOQUE 1-H2">BLOQUE 1-H2</option> </optgroup>   <optgroup label="BLOQUE 2"><option value="BLOQUE 2-H1">BLOQUE 2-H1</option> </optgroup><optgroup label="BLOQUE 3"> <option value="BLOQUE 3-H1">BLOQUE 3-H1</option></optgroup> <optgroup label="BLOQUE 4"><option value="BLOQUE 4-H1">BLOQUE 4-H1</option> </optgroup> </select><br><br><p><input type="submit" /></p></center>  </form> </div><div class="col-md-4"></div></div><br><p></p><div class="row"><div class="col-md-4"></div><div class="col-md-4"><center><img src="foto2.png" alt="Solvetic"><p> Para incidencias:</p><p>correo@correo.com</p></center></div><div class="col-md-4"></div></div><script src="/browser/static/js/jquery-3.3.1.js"></script>      <script src="/browser/static/js/bootstrap.js"></script></body>

- stat_auths2.php

<head><link rel="stylesheet" href="/browser/static/css/bootstrap.css"><link rel="stylesheet" href="/browser/static/css/bootstrap-theme.css"><title> Formulario para cambio de vales </title></head><body style="background-color:#F0F3F4;"><br><br><br><p></p><center><img src="foto1.png" alt="Solvetic"><br><p></p><legend>Formulario para cambio de vales</legend></center><p></p><div class="row"><div class="col-md-4"></div><div class="col-md-4"></div><div class="col-md-4"></div></div><br><p></p><center><H4><?php    $origen = $_SERVER['REQUEST_URI'];    date_default_timezone_set('Europe/Madrid');    $currentDate=date("Y/m/d h:i");    $datetime1 = new DateTime($currentDate);    require_once('vendor/autoload.php');    require_once('config.php');//error_reporting(0);    $site_id = '';    $controlleruser = '';    $controllerpassword = '';    $controllerurl = '';    $site_id = '';    $controllerversion = '';    $findThisVoucher= $_POST['voucher_input'];    $room = $_POST['room'];    $unifi_connection           = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);    $set_debug_mode             = $unifi_connection->set_debug($debug);    $loginresults               = $unifi_connection->login();    $findThisVoucher  = str_replace('-', '', $findThisVoucher);     $longitud_voucher = strlen($findThisVoucher);     if ($longitud_voucher < 10) {       echo "El vale $findThisVoucher no se ha introducido correctamente, tiene menos de 10 carácteres. ";      $findThisVoucher = false;    }    if ($longitud_voucher > 10) {       echo "El vale $findThisVoucher no se ha introducido correctamente, tiene mas de 10 carácteres. ";      $findThisVoucher = false;      $data_auths            = $unifi_connection->stat_auths();                    $status_find       = json_encode($data_auths, JSON_PRETTY_PRINT);        $json_mac              = json_decode($status_find);                          $mac_device            = null;                                              $data_guests       = $unifi_connection->list_guests();                   $mac_find              = json_encode($data_guests, JSON_PRETTY_PRINT);       $json_expired          = json_decode($mac_find);                            foreach ($json_mac as $record) {    $num_voucher = $record->voucher_code; // por cada posicion recorrida almacenamos en $num_voucher el voucher_code del array    if ($num_voucher === $findThisVoucher) {       break;    }  }  if ($mac_device === null) {     echo "no se ha encontrado el vale o no está activo";    $note   = '[Cambio de vale ERROR] ' . $room . '' . $currentDate . '>> C:\xampp\logs\log-API.txt [not found] ' ;    exec("echo $note");  } else {    foreach ($json_expired as $record) {       $mac = $record->mac;      $status_expired = $record->expired;      if ($mac_device === $mac) {         if ($status_expired != null) {                        echo "El vale no está activo";          $note   = '[Cambio de vale ERROR] ' . $room . '' . $currentDate . '>> C:\xampp\logs\log-API.txt [not active] ' . $findThisVoucher . '' .$mac_device  ;          exec("echo $note");        } else {          echo "El vale está activo";                 $unifi_connection->unauthorize_guest($mac_device); // desautorizamos el dispositivo       include 'create.php';       break;     }   } }}?></H4><form action="http://172.26.100.250:8081/index.html"><input type="submit" value="volver" /></form></center><br><br><div class="row"><div class="col-md-4"></div><div class="col-md-4"><center><img src="foto2.png" alt="Solvetic"><p> Para incidencias:</p><p>correo@correo.com</p></center></div><div class="col-md-4"></div></div><script src="/browser/static/js/jquery-3.3.1.js"></script>      <script src="/browser/static/js/bootstrap.js"></script></body>

- create.php

<head><link rel="stylesheet" href="/browser/static/css/bootstrap.css"><link rel="stylesheet" href="/browser/static/css/bootstrap-theme.css"><title> Formulario para cambio de vales </title></head><center><H4><?php        if($origen!= "/browser/stat_auths-2.php"){            header("location:http://172.26.100.250:8081/inde2.html");        }        require_once('vendor/autoload.php');        /        require_once('config.php');        date_default_timezone_set('Europe/Madrid');        $currentDate    = date("Y/m/d h:i");         $datetime1       = new DateTime($currentDate);         $datetime2       = new DateTime('2018-07-15');         $interval        = $datetime1->diff($datetime2);         $days            = $interval->format('%a');         $date_limit      = 60*24*$days;         $voucher_expiration = $date_limit;        /        $voucher_count = 1;        $site_id                    = '';        $controlleruser             = '';        $controllerpassword         = '';        $controllerurl              = '';        $site_id                    = '';        $controllerversion          = '';        $quota  = 1;        $note   = 'Vale-API, ' . $room . '' . $currentDate  ;        $up     = null;        $down   = null;         $MBytes = null;        $unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);        $set_debug_mode   = $unifi_connection->set_debug($debug);        $loginresults     = $unifi_connection->login();        $voucher_result = $unifi_connection->create_voucher($voucher_expiration, $voucher_count, $quota, $note, $up, $down, $MBytes);        $vouchers = $unifi_connection->stat_voucher($voucher_result[0]->create_time);        $data_vouchers  = json_encode($vouchers, JSON_PRETTY_PRINT);        $json_voucher   = json_decode($data_vouchers);          foreach ($json_voucher as $record) {            $num_voucher = $record->code;         }        /        $note   = '[Cambio de vale OK] ' . $room . '' . $currentDate . '' .  $findThisVoucher . '' .$mac_device .'' . $num_voucher .'>> C:\xampp\logs\log-API.txt';        exec("echo $note");        echo "Se ha anulado y creado el siguiente: $num_voucher ";        ?></H4></center><body><script src="/browser/static/js/jquery-3.3.1.js"></script>      <script src="/browser/static/js/bootstrap.js"></script></body>

Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images