note 76425 added to function.session-encode

From: Date: Sat, 14 Jul 2007 18:31:11 +0000
Subject: note 76425 added to function.session-encode
Groups: php.notes 
Request: Send a blank email to php-notes+get-128525@lists.php.net to get a copy of this message
If anybody wants to encode any arbitrary array in the session serialization style, I have come up with the following function ( along with a corresponding decoding function in the session_decode notes ) to do just that. Note the step I take to make sure we are not using a reference of the array, because calling session_raw_encode( $_SESSION ) ; will modify the real session when doing the recursion checks. <?php function session_raw_encode( $array, $safe = true ) { // the session is passed as refernece, even if you dont want it to if( $safe ) $array = unserialize(serialize( $array )) ; $raw = '' ; $line = 0 ; $keys = array_keys( $array ) ; foreach( $keys as $key ) { $value = $array[ $key ] ; $line ++ ; $raw .= $key .'|' ; if( is_array( $value ) && isset( $value['huge_recursion_blocker_we_hope'] )) { $raw .= 'R:'. $value['huge_recursion_blocker_we_hope'] . ';' ; } else { $raw .= serialize( $value ) ; } $array[$key] = Array( 'huge_recursion_blocker_we_hope' => $line ) ; } return $raw ; } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 99.247.215.196 ---- Manual Page -- http://www.php.net/manual/en/function.session-encode.php Edit -- https://master.php.net/note/edit/76425 Del: integrated -- https://master.php.net/note/delete/76425/integrated Del: useless -- https://master.php.net/note/delete/76425/useless Del: bad code -- https://master.php.net/note/delete/76425/bad+code Del: spam -- https://master.php.net/note/delete/76425/spam Del: non-english -- https://master.php.net/note/delete/76425/non-english Del: in docs -- https://master.php.net/note/delete/76425/in+docs Del: other reasons-- https://master.php.net/note/delete/76425 Reject -- https://master.php.net/note/reject/76425 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#128525) next »