php Session

I get a php error on this:

if (isset(session_id())

What is the correct syntax?

Is there a $_SESSION['session_id'] ?

Richard KortsBusiness Owner / Chief DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Ray PaseurCommented:
The easy ways of using the PHP session are documented here:

The PHP session_id() function returns a value:

The return value will always be true with respect to isset(), but your release level of PHP may not allow you to test with isset() when your script tests the return value of a function.

Can you please tell us in plain language, what are you trying to achieve with this if() statement?  The usual way of working with the session would just start the session at the beginning of the script and would never need to test to see if the session had been started!

Thanks, ~Ray

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
Dave BaldwinFixer of ProblemsCommented:
If you have session_start() at the top of the page, then you will have a session id.  And you have to assign $_SESSION variables after that.  There aren't any automatic $_SESSION variables.
Julian HansenCommented:
It sounds like you are trying to determine if a session has been started.

This might help to shed some light

if (isset($_SESSION['test'])) {
   echo 'Session is in progress';
else {
   echo 'Session variable has not been set yet';

$_SESSION['test'] = 1;

Open in new window

The first time you run this you get the 'Session variable has not been set yet'.
Running it again you get 'Session is in progress'
Running it again goes to the first message again - due to the session_destroy() call which clears the session.
Ray PaseurCommented:
If you have to write conditional code to determine whether the session is started or not, there is a design flaw in the application.  "Housekeeping" things like setting the error reporting, starting the session, and connecting to the database should always be done unconditionally at the logical top of the application.  Often these things are factored out of inline code into a common script that gets brought in with a require_once statement.  Then the PHP scripts that build the web pages start like this:
// etc...

Open in new window

and in common.php we have something like this:
<?php // common.php
ini_set('display_errors', FALSE);
ini_set('log_errors', TRUE);
// etc...

Open in new window

The common.php script is also a convenient place to store your class and function definitions if you're not using an autoloader.
Richard KortsBusiness Owner / Chief DeveloperAuthor Commented:
Sorry, I was being stupid. Obviously, what Ray & Dave said is correct.

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

From novice to tech pro — start learning today.