PHP Doku:: Kodiert die Daten der aktuellen Session als Zeichenkette - function.session-encode.html

Verlauf / Chronik / History: (2) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzSession-ErweiterungenSessionbehandlungSession-Funktionensession_encode

Ein Service von Reinhard Neidl - Webprogrammierung.

Session-Funktionen

<<session_destroy

session_get_cookie_params>>

session_encode

(PHP 4, PHP 5)

session_encodeKodiert die Daten der aktuellen Session als Zeichenkette

Beschreibung

string session_encode ( void )

session_encode() gibt eine Zeichenkette mit dem kodierten Inhalt der aktuellen Session zurück.

Rückgabewerte

Gibt Inhalt der aktuellen Session kodiert zurück.

Siehe auch


4 BenutzerBeiträge:
- Beiträge aktualisieren...
php dot net at mog dot se
9.04.2008 0:01
session_encode() returns a different format than serialize.

If you want to decode this format in python then "PHP Serialize implemented in Python" has special handling of this format using the session_encode and session_decode methods.

http://www.hurring.com/scott/code/python/serialize/
php at mikeboers dot com
14.07.2007 20:31
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 ;
   
}

?>
Onur Yerlikaya < http://www.witkey.org >
19.07.2006 5:21
<?php
session_start
();
# boolean type
$_SESSION['logged']     = true;
# string type
$_SESSION['name']    = "Onur Yerlikaya";
# integer type
$_SESSION['age']     = 17;

// logged|b:1;name|s:14:"Onur Yerlikaya";age|i:17;

function readSessions() {
   
$encodedData    = session_encode();
   
$explodeIt    = explode(";",$encodedData);
    for(
$i=0;$i<count($explodeIt)-1;$i++) {
       
$sessGet    = explode("|",$explodeIt[$i]);
       
$sessName[$i]    = $sessGet[0];
        if(
substr($sessGet[1],0,2) == "s:") {
           
$sessData[$i]    = str_replace("\"","",strstr($sessGet[1],"\""));
        } else {
           
$sessData[$i]    = substr($sessGet[1],2);
        }
// end if
   
} // end for
   
$result        = array_combine($sessName,$sessData);
    return
$result;
}   

/*
readSessions Func shows encoded data in array
Array
(
    [logged] => 1
    [name] => Onur Yerlikaya
    [age] => 17
)
*/
print_r(readSessions());
?>
sica at wnet dot com dot br
5.05.2005 18:19
session_encode() just return the session dataset in a formatted form

session_start();

$_SESSION['login_ok'] = true;
$_SESSION['nome'] = 'sica';
$_SESSION['inteiro'] = 34;

echo session_encode();

this code will print

login_ok|b:1;nome|s:4:"sica";inteiro|i:34;



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)