Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i check if session_start has been entered? [duplicate]

Tags:

I have a third party script and was wondering how I can check with PHP if session_start() has been declared before doing something?

something like if(isset($_session_start())) { //do something }  
like image 953
iaddesign Avatar asked Nov 24 '09 06:11

iaddesign


People also ask

Do I need to use session_start on every page?

It must be on every page you intend to use. The variables contained in the session—such as username and favorite color—are set with $_SESSION, a global variable. In this example, the session_start function is positioned after a non-printing comment but before any HTML.

How do I check if a session variable exists?

You can check whether a variable has been set in a user's session using the function isset(), as you would a normal variable. Because the $_SESSION superglobal is only initialised once session_start() has been called, you need to call session_start() before using isset() on a session variable.

How do you check if there is a session in PHP?

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

What is the purpose of the session_start () function?

session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie. When session_start() is called or when a session auto starts, PHP will call the open and read session save handlers.


2 Answers

if(!isset($_SESSION)) {      session_start(); } 

The check will allow you to keep your current session active even if it's a loop back submission application where you plan to reuse the form if data is typed incorrectly or have additional checks and balances within your program before proceeding to the next program.

like image 170
Maarek Avatar answered Nov 05 '22 17:11

Maarek


As in PHP >= 5.4.0, you have function session_status() that tell you if it have been initialize or not or if it's disabled.

For example you can do:

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

You can read more about it in http://www.php.net/manual/en/function.session-status.php

like image 35
PhoneixS Avatar answered Nov 05 '22 19:11

PhoneixS