Last active
August 29, 2015 14:15
-
-
Save obihann/020bb0cbec447d63137b to your computer and use it in GitHub Desktop.
Creating a secure session token
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
<?php | |
header('Content-Type: application/json'); | |
session_start(); | |
define("SESSION_SALT", "lowsaltdiet"); | |
$sessionKey = hash("sha512", SESSION_SALT . session_id()); | |
$post_data = array( | |
'session' => session_id(), | |
'key' => $sessionKey | |
); | |
$post_data = json_encode($post_data, JSON_FORCE_OBJECT); | |
echo $post_data; | |
?> |
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
<?php | |
header('Content-Type: application/json'); | |
session_start(); | |
define("SESSION_SALT", "lowsaltdiet"); | |
$output = (object) response; | |
$requestIP = $_SERVER['REMOTE_ADDR']; | |
$sessionKey = hash("sha512", SESSION_SALT . session_id()); | |
$userSession = $_REQUEST["session"]; | |
$userSessionKey = $_REQUEST["session_key"]; | |
if($userSession == session_id() && $userSessionKey == $sessionKey) { | |
$output->error = "auth"; | |
$output->response = "success"; | |
} else { | |
$output->error = "auth"; | |
$output->response = sprintf("Your IP (%s) is not authorized on this server.", $requestIP); | |
} | |
echo json_encode($output); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment