检查PHP会话是否已经开始

检查PHP会话是否已经开始

本文介绍了检查PHP会话是否已经开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP文件,有时从启动会话的页面调用该文件,有时从没有会话启动的页面调用.因此,当我在该脚本上使用session_start()时,有时会收到会话已开始"的错误消息.为此,我将这些行放在上面:

I have a PHP file that is sometimes called from a page that has started a session and sometimes from a page that doesn't have session started. Therefore when I have session_start() on this script I sometimes get the error message for "session already started". For that I've put these lines:

if(!isset($_COOKIE["PHPSESSID"]))
{
  session_start();
}

但是这次我收到了以下警告消息:

but this time I got this warning message:

有没有更好的方法来检查会话是否已经开始?

Is there a better way to check if session has already started?

如果我使用@session_start,它将使一切正常运行,只是关闭警告?

If I use @session_start will it make things work properly and just shut up the warnings?

推荐答案

PHP> = 5.4.0,PHP 7

if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

参考: http://www.php.net/manual/zh/function.session-status.php

对于 PHP< 5.4.0

if(session_id() == '') {
    session_start();
}

这篇关于检查PHP会话是否已经开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 21:51