• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7556
  • Last Modified:

A session had already been started ignoring session_start

Hi,

This error I got now with my code:
Notice: A session had already been started - ignoring session_start() in nav.php on line 2

This is the script:

<?
session_start();
if (!session_is_registered('login')) {
      header("Location:index.html");
}
?>

What is wronge???

Thanks,
HP
0
WvR
Asked:
WvR
  • 3
  • 2
1 Solution
 
RoonaanCommented:
You have two calls to session_start().

One in this file, and one somewhere else.

You can start a session only once.

You can use (I think);

if(!session_id()) session_start();

-r-
0
 
Ivo StoykovCommented:
Hello WvR,

> <?
> session_start();
this is a snippet, perhaps you have other stuff above... Make this 1st line

HTH

I
0
 
RoonaanCommented:
ivosoykov,

the problem is not with the position of the session_start() otherwise the most common error would be the headers already sent message.

-r-
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Ivo StoykovCommented:
Hi Roonaan,

I think that all (especially HTML) stuff shuld be after session_start()

regards

I
0
 
RoonaanCommented:
No, because if that where the case/problem then you would have seen the "header already sent" error.

The error clearily states "Session already started".

This can indicate three things:
1. There is a session_start() somewhere else in the code
2. This file is called multiple times, hence causing the session_start to be run multiple times
3. The server is setup to autostart sessions.

-r-
0
 
krackltCommented:
Hi all:

I encountered that problem in the past as well. Sometimes it is hard to "not start" a session twice, due to incudes etc.

I found though that it has no impact on the session itself. If it does, please let me know...

My workaround was to simply supress the error. Mybe not clean, but it worked:

@session_start();

I am not sure as to the function of session_is_registered(). It may also initialize a session implicitly. You could also escape it with an @ or you check if a session vaiable is set:

If (!isset($_SESSION['variable_name']))
    {
    // sent to login
    }

Maybe this helps. May not be the best way, just my 2 cents...

Later,

Thomas :-)



0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now