Protect a php file that shows json

0

I have my PHP file:

<?php
mysql_connect('hostejemplo.com','usuario','contraseña') or die('no se puede conectar');
mysql_select_db('basededatos');

$sql="SELECT * FROM tabla";
$resultado=mysql_query($sql);
while($row = mysql_fetch_assoc($resultado)){
    $arr[] = $row;
}
$json = json_encode($arr);
echo $json;
?>

How can I make it so that not everyone can see those results? Maybe with a simple login username but how would it be?

    
asked by DoubleM 20.12.2016 в 19:56
source

1 answer

1

It is a question too broad, but if this script is only going to be used within some systems and you do not run the risk of someone abusing it you could validate using a fixed hash by adding the following code at the beginning of the script

if ($_GET["token"] != "840a552b8269b09a49569b71ff10a07d"){
    die("Access denied");
}

This way, if the correct parameter is not sent when executing the script, it will only show "Access denied".

    
answered by 21.12.2016 / 13:11
source