Created
March 14, 2017 21:01
-
-
Save LeoDJ/baaff7f2373d0a8db889379dd0b3a888 to your computer and use it in GitHub Desktop.
A simple PHP persistance key/val store // Simply set the $secret to your password of choice and host the .php file on a server, then simply call the .php file with the given parameters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>doorStatus</title> | |
</head> | |
<body> | |
<?php | |
error_reporting(E_ALL); | |
ini_set('display_errors', true); | |
global $filePath, $secret; | |
$filePath = "doorStatusData.json"; | |
$secret = "yourSecret"; //set you secret for write access here | |
function readDataFile() { //read data from persistance file | |
global $filePath; | |
if(file_exists($filePath)) { | |
$dataFile = fopen($filePath, "r"); | |
$result = fread($dataFile, filesize($filePath)); | |
fclose($dataFile); | |
if(!is_null($result)) | |
return json_decode($result, true); | |
else { | |
return NULL; | |
} | |
} else | |
return NULL; | |
} | |
function writeToFile($key, $val) { //write a key/val pair into persistance file | |
global $filePath; | |
$data = readDataFile(); | |
if(is_null($data)) | |
$data = array(); | |
$data[$key] = $val; | |
$dataFile = fopen($filePath, "w"); | |
fwrite($dataFile, json_encode($data)); | |
fclose($dataFile); | |
} | |
if(sizeof($_GET) > 0) { //if any parameters were given | |
if(array_key_exists("getData", $_GET)){ //handle getData function | |
$data = readDataFile(); | |
if(is_null($data)) | |
echo '<div class="error"><b>Error</b>: could not read file<div>'; | |
else { | |
if(array_key_exists("key", $_GET)) { | |
if(array_key_exists($_GET["key"], $data)) | |
echo $data[$_GET["key"]]; | |
else | |
echo '<div class="error"><b>Error</b>: Key ' . $_GET["key"] . ' not found.<div>'; | |
} | |
else | |
echo '<div class="error"><b>Error</b>: No key field provided, please call URL with "doorStatus.php?getData&key=[id]"<div>'; | |
} | |
} | |
elseif(array_key_exists("setData", $_GET)) { //handle setData function | |
if(array_key_exists("key", $_GET) && array_key_exists("val", $_GET) && array_key_exists("secret", $_GET)) { | |
global $secret; | |
if($_GET["secret"] == $secret) { | |
writeToFile($_GET["key"], $_GET["val"]); | |
echo "Write successful"; | |
} | |
else | |
echo '<div class="error"><b>Error</b>: Wrong secret<div>'; | |
} | |
else | |
echo '<div class="error"><b>Error</b>: Insufficient parameters. Call URL with doorStatus.php?setData&key=[yourKey]&val=[yourVal]&secret=[yourSecret]<div>'; | |
} | |
else //if none of the above methods were given | |
echo '<div class="error"><b>Error</b>: Unknown method<div>'; | |
} | |
else //if no parameters were given at all | |
echo '<div class="error"><b>Error</b>: No method provided<div>'; | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment