session_name

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

session_name读取/设置会话名称

说明

session_name(?string $name = null): string|false

session_name() 函数返回当前会话名称。 如果指定 name 参数, session_name() 函数会更新会话名称, 并返回 原来的 会话名称。

如果使用 name 指定了新字符串作为会话 cookie 的名字, session_name() 函数会修改 HTTP 响应中的 cookie (如果启用了 session.transid,还会输出会话 cookie 的内容)。 一旦在 HTTP 响应中发送了 cookie 的内容之后, 调用 session_name() 函数会产生错误。 所以,一定要在调用 session_start() 函数之前 调用此函数。

请求开始的时候,会话名称会被重置并且存储到 session.name 配置项。 因此,要想设置会话名称,那么对于每个请求,都需要在 调用 session_start() 函数 之前调用 session_name() 函数。

参数

name

用在 cookie 或者 URL 中的会话名称, 例如:PHPSESSID。 只能使用字母和数字作为会话名称,建议尽可能的短一些, 并且是望文知意的名字(对于启用了 cookie 警告的用户来说,方便其判断是否要允许此 cookie)。 如果指定了 name 且不为 null, 那么当前会话也会使用指定值作为名称。

警告

会话名称至少需要一个字母,不能全部都使用数字, 否则,每次都会生成一个新的会话 ID。

返回值

返回当前会话名称。如果指定 name 参数,那么此函数会更新会话名称,并且 返回 原来的 会话名称, 或者在失败时返回 false

更新日志

版本 说明
8.0.0 name 现在可以为 null。
7.2.0 session_name() 函数会检查会话状态, 之前的版本仅仅检查 cookie 状态。 所以,旧版本的 PHP 允许你在调用 session_start() 函数之后再调用 session_name() 函数, 新版本的 PHP 不再允许这样做了。

示例

示例 #1 session_name() 示例

<?php

/* set the session name to WebsiteID */

$previous_name = session_name("WebsiteID");

echo
"The previous session name was $previous_name<br />";
?>

参见

add a note add a note

User Contributed Notes 10 notes

up
141
Hongliang Qiang
20 years ago
This may sound no-brainer: the session_name() function will have no essential effect if you set session.auto_start to "true" in php.ini . And the obvious explanation is the session already started thus cannot be altered before the session_name() function--wherever it is in the script--is executed, same reason session_name needs to be called before session_start() as documented.

I know it is really not a big deal. But I had a quite hard time before figuring this out, and hope it might be helpful to someone like me.
up
62
php at wiz dot cx
16 years ago
if you try to name a php session "example.com" it gets converted to "example_com" and everything breaks.

don't use a period in your session name.
up
39
relsqui at chiliahedron dot com
15 years ago
Remember, kids--you MUST use session_name() first if you want to use session_set_cookie_params() to, say, change the session timeout. Otherwise it won't work, won't give any error, and nothing in the documentation (that I've seen, anyway) will explain why.

Thanks to brandan of bildungsroman.com who left a note under session_set_cookie_params() explaining this or I'd probably still be throwing my hands up about it.
up
22
Joseph Dalrymple
13 years ago
For those wondering, this function is expensive!

On a script that was executing in a consistent 0.0025 seconds, just the use of session_name("foo") shot my execution time up to ~0.09s. By simply sacrificing session_name("foo"), I sped my script up by roughly 0.09 seconds.
up
10
Victor H
9 years ago
As  Joseph Dalrymple said, adding session_name do slow down a little bit the execution time.
But, what i've observed is that it decreased the fluctuation between requests.
Requests on my script fluctuated between 0,045 and 0,022 seconds. With session_name("myapp"), it goes to 0,050 and 0,045. Not a big deal, but that's a point to note.

For those with problems setting the name, when session.auto_start is set to 1, you need to set the session.name on php.ini!
up
1
mmulej at gmail dot com
3 years ago
Hope this is not out of php.net noting scope.

session_name('name') must be set before session_start() because the former changes ini settings and the latter reads them. For the same reason session_set_cookie_params($options) must be set before session_start() as well.

I find it best to do the following.

function is_session_started()
{
    if (php_sapi_name() === 'cli')
        return false;

    if (version_compare(phpversion(), '5.4.0', '>='))
        return session_status() === PHP_SESSION_ACTIVE;

    return session_id() !== '';
}
if (!is_session_started()) {
    session_name($session_name);
    session_set_cookie_params($cookie_options);
    session_start();
}
up
-1
descartavel1+php at gmail dot com
1 year ago
Always try to set the prefix for your session name attribute to either `__Host-` or `__Secure-` to benefit from Browsers improved security. See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#attributes

Also, if you have auto_session enabled, you must set this name in session.name in your config (php.ini, htaccess, etc)
up
-2
tony at marston-home dot demon dot co dot uk
6 years ago
The description that session_name() gets and/or sets the name of the current session is technically wrong. It does nothing but deal with the value originally supplied by the session.name value within the php.ini file.

Thus:-
$name = session_name();
is functionally equivalent to
$name = ini_get('session.name');
and
session_name('newname);
is functionally equivalent to
ini_set('session.name','newname');

This also means that:
$old_name = session_name('newname');
is functionally equivalent to
$old_name = ini_set('session.name','newname');

The current value of session.name is not attached to a session until session_start() is called. Once session_start() has used session.name to lookup the session_id() in the cookie data the name becomes irrelevant as all further operations on the session data are keyed by the session_id().

Note that changing session.name while a session is currently active will not update the name in any session cookie. The new name does not take effect until the next call to session_start(), and this requires that the current session, which was created with the previous value for session.name, be closed.
up
-4
tony at marston-home dot demon dot co dot uk
6 years ago
The description has recently been modified to contain the statement "When new session name is supplied, session_name() modifies HTTP cookie". This is not correct as session_name() has never modified any cookie data. A change in session.name does not become effective until session_start() is called, and it is session_start() that creates the cookie if it does not already exist.

See the following bug report for details: https://bugs.php.net/bug.php?id=76413
up
-19
slave at codegrunt dot com
19 years ago
One gotcha I have noticed with session_name is that it will trigger a WARNING level error if the cookie or GET/POST variable value has something other than alphanumeric characters in it.  If your site displays warnings and uses PHP sessions this may be a way to enumerate at least some of your scripts: 

http://example.com/foo.php?session_name_here=(bad)

Warning: session_start(): The session id contains invalid characters, valid characters are only a-z, A-Z and 0-9 in /some/path/foo.php on line 666

I did not see anything in the docs suggesting that one had to sanitize the PHP session ID values before opening the session but that appears to be the case.

Unfortunately session_name() always returns true so you have to actually get to the point of assigning variables values before you know whether you have been passed bad session data (as far as I can see).  After the error has been generated in other words.

Cheers
To Top