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
LVL 1
WvRAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.