note 123918 added to function.strpos

From: Date: Sun, 09 Jun 2019 13:25:42 +0000
Subject: note 123918 added to function.strpos
Groups: php.notes 
Request: Send a blank email to php-notes+get-211199@lists.php.net to get a copy of this message
A more accurate imitation of the PHP function session_start(). Function my_session_start() does something similar to session_start() that has the default configure, and the session files generated by the two are binary compatible. The code may help people increase their understanding of the principles of the PHP session. <?php error_reporting(E_ALL); ini_set('display_errors', true); ini_set('session.save_path', __DIR__); my_session_start(); echo '<p>session id: '.my_session_id().'</p>'; echo '<code><pre>'; var_dump($_SESSION); echo '</pre></code>'; $now = date('H:i:s'); if (isset($_SESSION['last_visit_time'])) { echo '<p>Last Visit Time: '.$_SESSION['last_visit_time'].'</p>'; } echo '<p>Current Time: '.$now.'</p>'; $_SESSION['last_visit_time'] = $now; function my_session_start() { global $phpsessid, $sessfile; if (!isset($_COOKIE['PHPSESSID']) || empty($_COOKIE['PHPSESSID'])) { $phpsessid = my_base32_encode(my_random_bytes(16)); setcookie('PHPSESSID', $phpsessid, ini_get('session.cookie_lifetime'), ini_get('session.cookie_path'), ini_get('session.cookie_domain'), ini_get('session.cookie_secure'), ini_get('session.cookie_httponly')); } else { $phpsessid = substr(preg_replace('/[^a-z0-9]/', '', $_COOKIE['PHPSESSID']), 0, 26); } $sessfile = ini_get('session.save_path').'/sess_'.$phpsessid; if (is_file($sessfile)) { $_SESSION = my_unserialize(file_get_contents($sessfile)); } else { $_SESSION = array(); } register_shutdown_function('my_session_save'); } function my_session_save() { global $sessfile; file_put_contents($sessfile, my_serialize($_SESSION)); } function my_session_id() { global $phpsessid; return $phpsessid; } function my_serialize($data) { $text = ''; foreach ($data as $k=>$v) { // key cannot contains '|' if (strpos($k, '|') !== false) { continue; } $text.=$k.'|'.serialize($v)."\n"; } return $text; } function my_unserialize($text) { $data = []; $text = explode("\n", $text); foreach ($text as $line) { $pos = strpos($line, '|'); if ($pos === false) { continue; } $data[substr($line, 0, $pos)] = unserialize(substr($line, $pos + 1)); } return $data; } function my_random_bytes($length) { if (function_exists('random_bytes')) { return random_bytes($length); } $randomString = ''; for ($i = 0; $i < $length; $i++) { $randomString .= chr(rand(0, 255)); } return $randomString; } function my_base32_encode($input) { $BASE32_ALPHABET = 'abcdefghijklmnopqrstuvwxyz234567'; $output = ''; $v = 0; $vbits = 0; for ($i = 0, $j = strlen($input); $i < $j; $i++) { $v <<= 8; $v += ord($input[$i]); $vbits += 8; while ($vbits >= 5) { $vbits -= 5; $output .= $BASE32_ALPHABET[$v >> $vbits]; $v &= ((1 << $vbits) - 1); } } if ($vbits > 0) { $v <<= (5 - $vbits); $output .= $BASE32_ALPHABET[$v]; } return $output; } ---- Server IP: 208.43.231.9 Probable Submitter: 119.81.81.244 (proxied: 202.133.212.162) ---- Manual Page -- http://php.net/manual/en/function.strpos.php Edit -- https://master.php.net/note/edit/123918 Del: integrated -- https://master.php.net/note/delete/123918/integrated Del: useless -- https://master.php.net/note/delete/123918/useless Del: bad code -- https://master.php.net/note/delete/123918/bad+code Del: spam -- https://master.php.net/note/delete/123918/spam Del: non-english -- https://master.php.net/note/delete/123918/non-english Del: in docs -- https://master.php.net/note/delete/123918/in+docs Del: other reasons-- https://master.php.net/note/delete/123918 Reject -- https://master.php.net/note/reject/123918 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#211199) next »