0

I have very big stack of code.
I need to use session_start() function always.
If I hasn't used session_start() not run each other.

example:

<?php 
if(session_start() == "used")
{
       // not run
}
else
{
       session_start();
}
?>
Birlikisgu
  • 227
  • 1
  • 3
  • 9

6 Answers6

4

do you mean, u want to check if the session has already been started? you can get it from here

Check if PHP session has already started

if(session_id() == '') {
    // session isn't started
}
Community
  • 1
  • 1
Nandini Bhaduri
  • 1,835
  • 4
  • 18
  • 31
  • This not for only session_start(). I think php programmers need check functions has used or not. – Birlikisgu Jun 06 '12 at 11:58
  • 1
    then you might save something in the session itself - like $session->used = true and check that before calling the functions.... – Nandini Bhaduri Jun 06 '12 at 12:02
  • @Birlikisgu I cannot imagine any reason why you would want to remember whether a function has been called. When you do need that, you're doing something wrong. Please provide an example (in your question) so you can get decent advise. Putting a `if (functionUsed) return;` on top of every function is not an option. – CodeCaster Jun 06 '12 at 13:20
2
if(session_id() === ""){
   session_start();
}
Riz
  • 9,703
  • 8
  • 38
  • 54
1

I think it will be better to use this top of the page.There is no need to use this sevral time.

session_start();
Pramod Kumar Sharma
  • 7,851
  • 5
  • 28
  • 53
  • I have 23 classes. I'm using session_start() in my classes. – Birlikisgu Jun 06 '12 at 11:56
  • 2
    @Birlikisgu then change your architecture. A class should not rely on sessions, and besides that, you _must_ have a point-of-entry for your application (i.e. an `index.php` or `config.php`). Put the `session_start()` there. – CodeCaster Jun 06 '12 at 11:57
1

you can do something like: if(!isset($_SESSION)){ session_start(); }

Smita
  • 4,634
  • 2
  • 25
  • 32
1

you can try it this way:

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

See also the Session documentation

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
Amir Soleymani
  • 355
  • 1
  • 3
  • 9
0

I think this won't work because you need to do a session_start() before any line of code