-
-
Save jasonbradley/f4c96422c28c780b9369 to your computer and use it in GitHub Desktop.
Get/Set Redis Key Using Predis with a little security
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
if ($_SERVER['REQUEST_METHOD'] != 'GET' | |
&& $_SERVER['REQUEST_METHOD'] != 'PUT') { | |
exit(); | |
} | |
$body = @file_get_contents('php://input'); | |
if ($body == '') { | |
exit(); | |
} | |
$data = json_decode($body); | |
require_once('../vendor/predis/predis/autoload.php'); | |
Predis\autoloader::register(); | |
$magicSalt = 'myMagicSalt'; | |
if (sha1($data->key . $magicSalt) != $data->sec_key) { | |
echo json_encode(array('response' => false)); | |
exit(); | |
} | |
try { | |
$redis = new Predis\Client(); | |
if ($_SERVER['REQUEST_METHOD'] == 'PUT') { | |
$redis->set($data->key, $data->value); | |
} else if ($_SERVER['REQUEST_METHOD'] == 'GET') { | |
$value = $redis->get($data->key); | |
} | |
} catch (Exception $e) { | |
//do something with the exception | |
echo $e->getMessage(); | |
} | |
echo json_encode(array('response' => $value)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment