Skip to content

Instantly share code, notes, and snippets.

@hkulekci
Last active October 11, 2020 09:13
Show Gist options
  • Save hkulekci/f6440ec7ffea6a9b1eff to your computer and use it in GitHub Desktop.
Save hkulekci/f6440ec7ffea6a9b1eff to your computer and use it in GitHub Desktop.
PHP Redis Client
<!DOCTYPE html>
<html>
<head>
<title>Redis Client</title>
<style type="text/css">
a,abbr,acronym,address,applet,article,aside,audio,b,big,blockquote,body,canvas,caption,center,cite,code,dd,del,details,dfn,div,dl,dt,em,embed,fieldset,figcaption,figure,footer,form,h1,h2,h3,h4,h5,h6,header,hgroup,html,i,iframe,img,ins,kbd,label,legend,li,mark,menu,nav,object,ol,output,p,pre,q,ruby,s,samp,section,small,span,strike,strong,sub,summary,sup,table,tbody,td,tfoot,th,thead,time,tr,tt,u,ul,var,video{margin:0;padding:0;border:0;font:inherit;vertical-align:baseline}article,aside,details,figcaption,figure,footer,header,hgroup,menu,nav,section{display:block}body{line-height:1}ol,ul{list-style:none}blockquote,q{quotes:none}blockquote:after,blockquote:before,q:after,q:before{content:'';content:none}table{border-collapse:collapse;border-spacing:0}
body {font-family:arial; font-size:12px;margin:10px;}
strong {font-weight: bold;}
.left {float:left;}
.w1 { width: 9%;}
.w2 { width: 19%;}
.w6 { width: 59%;}
.w8 { width: 79%;}
.w9 { width: 89%;}
.hide {display:none;}
</style>
</head>
<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$keys = array();
?>
<body>
<div>
<div class="left w2">
<?php
$infos = $redis->info();
$databases = $redis->config("get", "databases");
echo '<div class="infos">';
echo '<strong>Informations</strong><br>';
echo 'Redis Version : ' . $infos['redis_version'] . "<br>";
echo 'Process ID : ' . $infos['process_id'] . "<br>";
echo 'Memory Usage : ' . $infos['used_memory_human'] . "<br>";
echo 'Role : ' . $infos['role'] . "<br>";
echo 'Clients : ' . $infos['connected_clients'] . "<br>";
echo '</div>';
echo '<div class="infos_extra hide">';
foreach ($infos as $key => $value) {
echo $key . ' : ' . $value . '<br>';
}
echo '</div>';
echo '<ul>';
echo '<li><strong>Databases</strong></li>';
for ($i = 0; $i < $databases['databases']; $i++) {
echo '<li><a href="?db_index='.$i.'">db'.$i.'</a></li>';
}
echo '</ul>';
?>
</div>
<div class="left w2">
<?php
if (isset($_GET['db_index'])) {
$db_index = (int)$_GET['db_index'];
$redis->select($db_index);
$keys = $redis->keys("*");
echo '<ul>';
foreach ($keys as $key) {
echo '<li><a href="?db_index='.$db_index.'&key='.$key.'">'.$key.'</a></li>';
}
echo '</ul>';
} else {
echo 'Select a database';
}
?>
</div>
<div class="left w6">
<?php
if (isset($_GET['db_index']) && isset($_GET['key'])) {
if (in_array($_GET['key'], $keys)) {
$key_name = $_GET['key'];
$page = isset($_GET['p']) ? ((int)$_GET['p'] <= 1 ? 1 : (int)$_GET['p']) : 1;
$key_type = $redis->type($key_name);
echo '<strong>Value(s): </strong>';
if ($page > 1)
echo ' >> &nbsp; <a href="?db_index='.$db_index.'&key='.$key_name.'&p='.($page-1).'">prev</a>';
echo '&nbsp; <a href="?db_index='.$db_index.'&key='.$key_name.'&p='.($page+1).'">next</a>';
echo '<br>';
if ($key_type == 1) { // string
$value = $redis->get($key_name);
printf("%s", $value);
} else if ($key_type == 3) { // list
echo 'Total Element : '.$redis->llen($key_name) . '<br>';
$values = $redis->lrange($key_name, ($page - 1) * 50, (($page - 1) * 50) + 49);
for ($i = 0; $i < count($values); $i++) {
printf('<p>%04d - <a href="?db_index='.$db_index.'&key='.$key_name.'&key_id='.$i.'" class="message">%s</a><br><span class="content_of_message hide">%s</span></p>', $i + ($page - 1) * 50, substr($values[$i], 0, 50), stripcslashes($values[$i]));
}
}
}
}
?>
</div>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.message').click(function(){
$(this).siblings('.content_of_message').toggleClass('hide');
return false;
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment