Last active
November 16, 2018 21:53
-
-
Save ahmeti/7eca7f3a3f31239919abb2692e66fe7c to your computer and use it in GitHub Desktop.
Get Decode Session Variables From PHPSESSID
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 | |
# .env file variables | |
# OLD_SESSION_PATH=/var/tmp/ | |
# OLD_SESSION_PRF=sess_ | |
function getUserIdFromOldSession() | |
{ | |
if( ! isset($_COOKIE['PHPSESSID']) ){ | |
return false; | |
} | |
$phpsessid = $_COOKIE['PHPSESSID']; | |
$sessionFile = env('OLD_SESSION_PATH').env('OLD_SESSION_PRF').$phpsessid; | |
if( ! is_file($sessionFile) ){ | |
return false; | |
} | |
$data = file_get_contents($sessionFile); | |
$session_decode = function ($serialized_string) | |
{ | |
$variables = array(); | |
$a = preg_split( "/(\w+)\|/", $serialized_string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE ); | |
for( $i = 0; $i<count($a); $i = $i+2 ) | |
{ | |
if(isset($a[$i+1])) | |
{ | |
$variables[$a[$i]] = unserialize( $a[$i+1] ); | |
} | |
} | |
return( $variables ); | |
}; | |
$session = $session_decode($data); | |
if( isset($session['UserID']) && $session['UserID'] > 0 ){ | |
return $session['UserID']; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment