php Session

I get a php error on this:

if (isset(session_id())

What is the correct syntax?

Is there a $_SESSION['session_id'] ?

Thanks
Richard KortsAsked:
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.

Ray PaseurCommented:
The easy ways of using the PHP session are documented here:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11909-PHP-Sessions-Simpler-Than-You-May-Think.html

The PHP session_id() function returns a value:
http://php.net/manual/en/function.session-id.php

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.
http://php.net/manual/en/function.isset.php

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
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
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.

http://php.net/manual/en/function.session-start.php
0
Julian HansenCommented:
It sounds like you are trying to determine if a session has been started.

This might help to shed some light
<?php
session_start();

if (isset($_SESSION['test'])) {
   echo 'Session is in progress';
   session_destroy();
}
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.
0
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:
<?php 
require_once("common.php");
// etc...

Open in new window

and in common.php we have something like this:
<?php // common.php
error_reporting(E_ALL);
ini_set('display_errors', FALSE);
ini_set('log_errors', TRUE);
session_start();
// 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.
0
Richard KortsAuthor Commented:
Sorry, I was being stupid. Obviously, what Ray & Dave said is correct.

R
0
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.