Session data in Redis
What is session
Session is
What is cookie
Cookie is something you eat 🙂
How it works
The code presented below was not checked for security bugs and doesn’t handle any errors.
Plik index.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<xmp> <?PHP require_once('functions.php'); $user=session_check(); ?> <html> <head> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/uikit/2.26.3/css/uikit.min.css" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/uikit/2.26.3/js/uikit.min.js"> </script> </head> <body> <?PHP show_menu($user); ?> Hello <?PHP echo $user['username']; ?>! This is my website. </body> </html> </xmp> |
Plik login.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<xmp> <?PHP require_once('functions.php'); $user=session_check(); ?> <html> <head> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/uikit/2.26.3/css/uikit.min.css" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/uikit/2.26.3/js/uikit.min.js"> </script> </head> <body> <?PHP show_menu($user); if ($user==NULL or $user['id']==NULL) { echo '<form method="post" action="login.php" class="uk-form"> <fieldset data-uk-margin> <legend>Log in</legend> <input name="username" type="text" placeholder="username"> <input name="password" type="password" placeholder="password"> <button class="uk-button">Go!</button> <label><input type="checkbox">Remember me!</label> </fieldset> </form>'; } else echo "Witaj ".$user['username']."!"; ?> </body> </html> </xmp> |
Plik functions.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
<xmp> <?PHP function session_check() { if(!isset($_COOKIE['MYSID'])) { $token=md5(rand(0,1000000000)); setcookie('MYSID', $token); $user=array('id'=>NULL,'username'=>"Visitor"); redis_set_json($token, $user,0); } else $token=$_COOKIE['MYSID']; if (isset($_POST['username']) and isset($_POST['password'])) return authorize($_POST['username'],$_POST['password'],$token); else return authorize(NULL,NULL,$token); } function authorize($username,$password, $token) { if ($username!=NULL and $password!=NULL) { if ($username=="kalkos" and $password=="qwerty") $user=array('id'=>333,'username'=>$username); else $user=array('id'=>NULL,'username'=>"Visitor"); redis_set_json($token,$user,"0"); return $user; } else return redis_get_json($token); } function logout($user) { $token=$_COOKIE['MYSID']; $user=array('id'=>NULL,'username'=>"Visitor"); redis_set_json($token,$user,"0"); return $user; } function redis_set_json($key, $val, $expire) { $redisClient = new Redis(); $redisClient->connect( '127.0.0.1', 6379 ); $value=json_encode($val); if ($expire > 0) $redisClient->setex($key, $expire, $value ); else $redisClient->set($key, $value); $redisClient->close(); } function redis_get_json($key) { $redisClient = new Redis(); $redisClient->connect( '127.0.0.1', 6379 ); $ret=json_decode($redisClient->get($key),true); $redisClient->close(); return $ret; } function show_menu($user) { echo ' <nav class="uk-navbar"> <ul class="uk-navbar-nav">'; if ($user==NULL or $user['id']==NULL) echo '<li class="uk-active"><a href="login.php">Login</a></li>'; else echo '<li class="uk-active"><a href="logout.php">Logout</a></li>'; echo ' <li class="uk-parent"><a href="index.php">Home</a></li> </ul> </nav>'; } ?> </xmp> |