Last active
October 19, 2018 11:54
-
-
Save actual-saurabh/ca531d557f88822cfbb80839daa7a4ec to your computer and use it in GitHub Desktop.
Generate Client IDs
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("Cache-Control: no-cache, must-revalidate"); | |
header("Pragma: no-cache"); | |
session_start(); | |
//no cookie, no session | |
if ((!isset($_COOKIE['cid'])) && (!isset($_SESSION['cid']))) { | |
$_SESSION['cid'] = sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', | |
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), | |
mt_rand( 0, 0xffff ), | |
mt_rand( 0, 0x0fff ) | 0x4000, | |
mt_rand( 0, 0x3fff ) | 0x8000, | |
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ) | |
); | |
} | |
//cookie prior session | |
if (isset($_COOKIE['cid'])) { | |
$_SESSION['cid'] = $_COOKIE['cid']; | |
} | |
//update cookie | |
if (isset($_SESSION['cid'])) { | |
setcookie("cid", $_SESSION['cid'], time()+3600*24*365); | |
} | |
$cid = $_SESSION['cid']; | |
if ($_GET['return']=="cid") { | |
echo $cid; | |
} |
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 | |
/* | |
* Call like | |
* Session->start( $unique ); | |
*/ | |
class Session{ | |
// hook into WordPress to start sessions. | |
public function start( $unique = false ) { | |
// only set no-cache headers if the session is supposed to be unique. | |
if( $unique === true ) | |
add_action( 'send_headers', array( $this, 'set_headers' ); | |
} | |
// set a unique client_id. | |
add_action( 'wp', 'set_client_id' ); | |
} | |
public function set_headers() { | |
add_header( "Cache-Control: no-cache, must-revalidate" ); | |
add_header( "Pragma: no-cache" ); | |
add_action( 'llms/session/client_id/set', array( $this, 'start_unique_session' ); | |
} | |
public function set_client_id() { | |
// this will have to not run if you'd want to have anonymity | |
$session_client_id = filter_var( $_SESSION[ 'llms_client_id' ] ); | |
$cookie_client_id = filter_input( INPUT_COOKIE, 'llms_client_id' ); | |
// no cookie, no session | |
if( ! empty( $session_client_id ) && ! empty( $cookie_client_id ) ){ | |
$_SESSION[ 'llms_client_id' ] = $session_client_id = wp_generate_uuid4(); | |
} | |
if( ! empty( $cookie_client_id ) ){ | |
$_SESSION[ 'llms_client_id' ] = $session_client_id = $cookie_client_id; | |
} | |
if( ! empty( $session_client_id ) ){ | |
setcookie( 'llms_client_id', $session_client_id, time() + YEAR_IN_SECONDS); | |
} | |
do_action( 'llms/session/client_id/set', $session_client_id ); | |
} | |
function start_unique_session( $session_client_id ) { | |
$session_id = filter_var( $_SESSION[ 'llms_session_id' ] ); | |
// this session has already started | |
if( ! empty( $session_id ) ){ | |
return; | |
} | |
global $llms; | |
$current_user = $llms->who_is; | |
$_SESSION[ 'llms_session_id' ] = $session_id = wp_generate_uuid4(); | |
do_action( 'llms/session/started', $session_id ); | |
do_action( "llms/session/{$llms->who_is->persona_type}/started", $session_id ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment