谷歌镜像 伍佰目录 短网址
  当前位置:海洋目录网 » 站长资讯 » 站长资讯 » 文章详细 订阅RssFeed

PHP - Manual: session_status

来源:网络转载 浏览:49241次 时间:2024-05-21
session_unset » « session_start
  • PHP 手册
  • 函数参考
  • Session 扩展
  • Sessions
  • Session 函数

session_status

(PHP 5 >= 5.4.0, PHP 7, PHP 8)

session_status — 返回当前会话状态

说明

session_status(): int

session_status() 被用于返回当前会话状态。

返回值

  • PHP_SESSION_DISABLED 会话是被禁用的。
  • PHP_SESSION_NONE 会话是启用的,但不存在当前会话。
  • PHP_SESSION_ACTIVE 会话是启用的,而且存在当前会话。

参见

  • session_start() - 启动新会话或者重用现有会话
add a note

User Contributed Notes 10 notes

up down 62 sasi dot viragelet at gmail dot co7 years ago Maybe depending on PHP settings, but if return values are not the above, then go for this:
_DISABLED = 0
_NONE = 1
_ACTIVE = 2
up down 2 polygon dot co dot in at gmail dot com8 months ago Note session_status() is for file based session only.

DB based session status needs to have custom function based on table structure.
up down 38 coder dot ua at gmail dot com8 years ago Universal function for checking session status.

<?php
/**
* @return bool
*/
function is_session_started()
{
    if ( php_sapi_name() !== 'cli' ) {
        if ( version_compare(phpversion(), '5.4.0', '>=') ) {
            return session_status() === PHP_SESSION_ACTIVE ? TRUE : FALSE;
        } else {
            return session_id() === '' ? FALSE : TRUE;
        }
    }
    return FALSE;
}

// Example
if ( is_session_started() === FALSE ) session_start();
?>
up down 13 info at eurocron dot de3 years ago Use always session_status(), to check if a session is already started and active.
if(session_status() !== PHP_SESSION_ACTIVE) session_start();
or
if(session_status() === PHP_SESSION_NONE) session_start();

Don't use
if(!isset($_SESSION)) session_start();
or
if(session_id() === "") session_start();

They will not work properly after a call to session_write_close().
Both functions will continue to report, that the session exists.
And this is right, you can read from $_SESSION, but if you want to write,
you need session_start() again.

As a shorthand you can use
@session_start()
with the @ at the beginning to suppress the
PHP notice "A session had already been started - ignoring session_start()"

As stated in the manual for session_start(), a second call will do no harm,
it will be simply ignored. But you need the @, if you don't want to get the notice.
up down 6 php at pointpro dot nl9 years ago The advice of ive_insomnia at live dot com should be taken with great care.

First of all, while his use case for session_status is valid, a simpler way to avoid the warning is:

<?php
if (!isset($_SESSION)) { session_start(); }
?>

The example of session_status uses the raw values of constants (2 in this case) created specifically for the purpose of not having to use magic numbers.

Better code would be:

<?php
if (session_status() !== PHP_SESSION_ACTIVE) {session_start();}
?>

The same can be done using

<?
if (session_id() === "") { session_start(); }
?>

The use of this function is lies more towards status management: change the behavior of a script when sessions are disabled altogether, for example.
up down 2 Ben (aocool at msn d0t com)6 years ago Just another function to determine whether the session has already started:

function is_session_started () {
    return function_exists ( 'session_status' ) ? ( PHP_SESSION_ACTIVE == session_status () ) : ( ! empty ( session_id () ) );
}
up down 2 ayon at hyurl dot com5 years ago This is how the session_status() works:
<?php
function session_status(){
    if(!extension_loaded('session')){
        return 0;
    }elseif(!file_exists(session_save_path().'/sess_'.session_id()){
        return 1;
    }else{
        return 2;
    }
}
?>
up down 0 Ollea8 years ago If you started and closed a session then test ( session_id() === '' ) to check if a session is active it won't work, session_id() returns an ID even if the session is closed.

Anybody knows another way before PHP 5.4 to check if a session is really not currently active ?
up down -3 solaimanshah059 at gmail dot com3 years ago Here some Good example for your understandingl

<?php

if( session_status == PHP_SESSION_NONE ) // if session status is none then start the session
{
     session_start();
}

?>

old Practice we were using.

<?php

if( !( isset($_SESSION) ) ) // if the session is no  set then start to
new session
{
     session_start();
}

?>
up down -19 php at kenman dot net8 years ago The purpose of this functionality can aid you specifically in cases where code -- prior to your current code -- might have opened a session and then closed it.

Specifically, depending on $_SESSION, session_id(), and the SID constant to determine if a session is active will FAIL if a session has previously been opened & closed within the same request cycle.

Please see the original bug report here:

https://bugs.php.net/bug.php?id=52982
add a note

官方地址:https://www.php.net/manual/en/function.session-status.php

  推荐站点

  • At-lib分类目录At-lib分类目录

    At-lib网站分类目录汇集全国所有高质量网站,是中国权威的中文网站分类目录,给站长提供免费网址目录提交收录和推荐最新最全的优秀网站大全是名站导航之家

    www.at-lib.cn
  • 中国链接目录中国链接目录

    中国链接目录简称链接目录,是收录优秀网站和淘宝网店的网站分类目录,为您提供优质的网址导航服务,也是网店进行收录推广,站长免费推广网站、加快百度收录、增加友情链接和网站外链的平台。

    www.cnlink.org
  • 35目录网35目录网

    35目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向35目录推荐、提交优秀网站。

    www.35mulu.com
  • 就要爱网站目录就要爱网站目录

    就要爱网站目录,按主题和类别列出网站。所有提交的网站都经过人工审查,确保质量和无垃圾邮件的结果。

    www.912219.com
  • 伍佰目录伍佰目录

    伍佰网站目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向伍佰目录推荐、提交优秀网站。

    www.wbwb.net