I have created several web services to consult data from my database from an android application, the web services are already working, they return the information to me and it is already shown in the application, but I want to implement token's as security in my queries, but not I understand how I can implement the token's, this is a web services that I use:
<?PHP
$hostname_localhost = "localhost";
$database_localhost = "mgrex";
$username_localhost = "root";
$password_localhost = "";
$json = array();
if(isset($_GET["empresa"]) && isset($_GET["rol"])){
$empresa = $_GET["empresa"];
$rol = $_GET["rol"];
$conexion = mysqli_connect($hostname_localhost,$username_localhost,$password_localhost,$database_localhost);
mysqli_set_charset($conexion, "utf8");
$consulta = "select * from inicio WHERE empresa = '{$empresa}' AND rol = '{$rol}'";
$resultado = mysqli_query($conexion, $consulta);
while($registro=mysqli_fetch_array($resultado)){
$json['inicio'][]=$registro;
}
}else{
$resultar["tituloNoticia"]='No registrado';
$resultar["subtituloNoticia"]='No registrado';
$resultar["imagenNoticia"]='No registrado';
$resultar["descripcionNoticia"]='No registrado';
$resultar["empresa"]='No registrado';
$resultar["rol"]='No registrado';
$json['inicio'][]=$resultar;
}
mysqli_close($conexion);
echo json_encode($json);
?>
I have been reviewing examples on the internet like the following:
require_once 'vendor/autoload.php';
use Firebase\JWT\JWT;
$time = time();
$key = 'my_secret_key';
$token = array(
'iat' => $time, // Tiempo que inició el token
'exp' => $time + (60*60), // Tiempo que expirará el token (+1 hora)
'data' => [ // información del usuario
'id' => 1,
'name' => 'Eduardo'
]
);
$jwt = JWT::encode($token, $key);
$data = JWT::decode($jwt, $key, array('HS256'));
var_dump($data);
I do not understand how I can use this example in my web service.