Skip to content

Instantly share code, notes, and snippets.

@jasonbradley
Created March 26, 2013 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonbradley/f4c96422c28c780b9369 to your computer and use it in GitHub Desktop.
Save jasonbradley/f4c96422c28c780b9369 to your computer and use it in GitHub Desktop.
Get/Set Redis Key Using Predis with a little security
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