International PHP Conference Berlin 2025

session_encode

(PHP 4, PHP 5, PHP 7, PHP 8)

session_encode将当前会话数据编码为字符串

说明

session_encode(): string|false

session_encode() 返回一个序列化后的字符串,包含被编码的、储存于 $_SESSION 超全局变量中的当前会话数据。

默认情况下,PHP 内部使用的序列方法和 serialize() 是不一样的。 该序列方法通过 session.serialize_handler 来设置。

参数

此函数没有参数。

返回值

返回当前会话编码后的内容, 或者在失败时返回 false

注释

警告

在调用 session_encode() 之前必须先调用 session_start()

参见

添加备注

用户贡献的备注 3 notes

up
19
<carlos sica>sica at wnet dot com dot br
19 years ago
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;
up
2
Mark P
7 years ago
session_encode() can't handle pipes in your keys.

<?php
session_start
();
$_SESSION = ['foo|bar'=>'ba;z']; pathetic
dump
(session_encode()); // false because "foo|bar" contains a pipe
?>
up
0
polygon dot co dot in at gmail dot com
24 days ago
The session_encode and session_decode functions are incomplete.

Because there is no option to collect the output values for session_decode or input an array in session_encode.

I faced this issue while trying to implement custom session using session_set_save_handler.

It led me to limit the functionality to particular serialize_handler and had to hardcode the encode and decode functions like serialize and unserialize for option ['serialize_handler' => 'php_serialize'] instead of session_encode and session_decode once i am done with defining the encoding algo.
To Top