registro.php - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of registro.php.php

  1. <?php
  2.        
  3.         //===========Registro de usuarios============//
  4.        
  5.         if(isset($_POST['nombre'])){                                                                                    //validacion de datos minima
  6.                 $objson = null;
  7.                 $file = "registros.json";                                                                                     //archivo de lectura y escritura
  8.                 $data = [];
  9.                
  10.                 if(file_exists($file)){                                                                                         //recompilacion de datos existentes
  11.                         $string = file_get_contents($file,true);                     
  12.                         $data = json_decode($string,true);
  13.                 }else{
  14.                         die('no se puede leer el archivo de registro');
  15.                 }
  16.                
  17.                 //nuevos datos
  18.                 $data[]=[
  19.                         'nombre'=>$_POST['nombre'],
  20.                         'pais'=>$_POST['pais'],
  21.                         'celular'=>$_POST['celular'],
  22.                         'email'=>$_POST['email'],
  23.                 ];
  24.                
  25.                 //imprimimos para saber que datos tenemos
  26.                 echo '<pre>';
  27.                 echo var_dump($data);
  28.                 echo '<pre>';
  29.                
  30.                 //pasamos los datos a formato json string
  31.                 $fullrecord = json_encode($data);
  32.                
  33.                 //guardamos y validamos los datos.
  34.                 if (file_put_contents($file, $fullrecord)===false){
  35.                         die('no se puede escribir en l archivo');
  36.                 }
  37.         }
  38.        
  39.         //pasado 5 segundos redirigimos al index.php
  40.         echo '<meta http-equiv="refresh" content="5;url=index.php">';
  41.        
  42.        
  43. ?>
  44.  
File Description
  • registro.php
  • PHP Code
  • 13 Oct-2021
  • 1.04 Kb
You can Share it: