Created
March 23, 2015 18:02
-
-
Save m93a/66f88b10ed57719d0526 to your computer and use it in GitHub Desktop.
Safe PHP sessions extension
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 | |
//Drobné rozšíření k PHP sessions | |
function session_exists($id=false) { | |
$sid = ''; | |
if($id) { | |
$sid = $id; | |
} elseif(session_id()) { | |
$sid = session_id(); | |
} elseif($_GET['SID']) { | |
$sid = $_GET['SID']; | |
} elseif($_COOKIE['PHPSESSID']) { | |
$sid = $_COOKIE['PHPSESSID']; | |
} | |
if ($sid) { | |
if(!session_id()) { | |
session_id($sid); | |
session_start(); | |
} | |
if (!$_SESSION['authentic']) { | |
session_destroy(); | |
return false; | |
} else { | |
return true; | |
} | |
} else { | |
return false; | |
} | |
} | |
function session_create($sid=false) { | |
if(!$sid){ | |
$sid = base64_encode(openssl_random_pseudo_bytes(32)); | |
$sid = str_replace("+", "", $sid); | |
$sid = str_replace("/", "", $sid); | |
$sid = str_replace("=", "", $sid); | |
} | |
if(session_id()) { | |
session_destroy(); | |
} | |
session_id($sid); | |
session_start(); | |
$_SESSION['authentic'] = true; | |
} | |
function session_clear() { | |
session_start(); | |
foreach($_SESSION as $key=>$value){ | |
$_SESSION[$key] = NULL; | |
} | |
session_destroy(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment