Session_ID() – PHP

  php

Q(Question):

I have a script to check if a session exists, if not then I log the user’s info from $_Server
variables into a database (ip address, time, etc.).

The problem is that the test:

if(!session_id()) // if there is no session started
{
session_cache_limiter(‘nocache’);
session_cache_expire (20);
session_start();

include_once (‘../DB_functions.php’);

….the the other stuff, like logging goes here…
doesn’t seem to work…the test always produces true…is there something I am missing?

Thanks,

Annie

A(Answer):

> The problem is that the test:

if(!session_id()) // if there is no session started
{
session_cache_limiter(‘nocache’);
session_cache_expire (20);
session_start();
doesn’t seem to work…the test always produces true…is there something
I am missing?

Try :
print ini_get(‘session.auto_start’);

If it is set, they your session is already started and
you will always have a session_id. You would then need
to turn it off in php.ini or .htaccess
____________________________________
Wil Moore III, MCP | Integrations Specialist | Senior Consultant
Business | Personal

A(Answer):

la*******@hotmail.com wrote:

The problem is that the test:

if(!session_id()) // if there is no session started
{
session_cache_limiter(‘nocache’);
session_cache_expire (20);
session_start();

doesn’t seem to work…the test always produces true…is there something

I am missing?
Try :
print ini_get(‘session.auto_start’);

If it is set, they your session is already started and
you will always have a session_id. You would then need
to turn it off in php.ini or .htaccess
____________________________________
Wil Moore III, MCP | Integrations Specialist | Senior Consultant
Business | Personal

Thanks for you advice,

ini_get(‘session.auto_start’) returns 0, so I assume that the auto start is off. The problem is
that the If statement runs as if there is never a session.

When I print the value of session_id () it produces a value (a session id), but !session_id() always
is true?

Annie

A(Answer):

> >>The problem is that the test:

if(!session_id()) // if there is no session started
{
session_cache_limiter(‘nocache’);
session_cache_expire (20);
session_start();

doesn’t seem to work…the test always produces true…is there

something

What I have found is interesting. You can’t evaluate ‘session_id()’ before
calling
session_start() because session_start() is what restores the value that
session_id()
would return.

I know the manual referrs to using session_id() before session_start(),
however,
it is not meant to say that you can retrieve that value… it means it can
be SET
only before session_start() is called.

____________________________________
Wil Moore III, MCP | Integrations Specialist | Senior Consultant
Business | Personal

A(Answer):

I think you’ve misinterpretted the purpose of session_start(). While the
function does create a new session when there isn’t one, its primary
function is to initialize the session mechanism (for instance, populate the
$_SESSION array). It’s best to interpret the "start" in session_start as
"from this point forward, the script will start making use of session
variables. The function that performs the opposite is session_write_close().

I suggest using a session variable to note whether the user info has been
saved or not, as follows:

<?php

session_start();

if(!@$_SESSION[‘user info saved’]) {
… save info in database …
$_SESSION[‘user info saved’] = true;
}

Uzytkownik "Annie" <An**@newsgroup.net> napisal w wiadomosci
news:cCS1c.45548$PR3.929733@attbi_s03…

I have a script to check if a session exists, if not then I log the user’s
info from $_Server variables into a database (ip address, time, etc.).

The problem is that the test:

if(!session_id()) // if there is no session started
{
session_cache_limiter(‘nocache’);
session_cache_expire (20);
session_start();

include_once (‘../DB_functions.php’);

…the the other stuff, like logging goes here…
doesn’t seem to work…the test always produces true…is there something
I am missing?
Thanks,

Annie

A(Answer):

Chung Leong wrote:

I think you’ve misinterpretted the purpose of session_start(). While the
function does create a new session when there isn’t one, its primary
function is to initialize the session mechanism (for instance, populate the
$_SESSION array). It’s best to interpret the "start" in session_start as
"from this point forward, the script will start making use of session
variables. The function that performs the opposite is session_write_close().

I suggest using a session variable to note whether the user info has been
saved or not, as follows:

<?php

session_start();

if(!@$_SESSION[‘user info saved’]) {
… save info in database …
$_SESSION[‘user info saved’] = true;
}

Uzytkownik "Annie" <An**@newsgroup.net> napisal w wiadomosci
news:cCS1c.45548$PR3.929733@attbi_s03…

I have a script to check if a session exists, if not then I log the user’s

info from $_Server

variables into a database (ip address, time, etc.).

The problem is that the test:

if(!session_id()) // if there is no session started
{
session_cache_limiter(‘nocache’);
session_cache_expire (20);
session_start();

include_once (‘../DB_functions.php’);

…the the other stuff, like logging goes here…
doesn’t seem to work…the test always produces true…is there something

I am missing?

Thanks,

Annie

Thanks…yeah the manual does not mention that you need to first run session_start before you check
session_id, and that does not match the way they suggest using it. I will use a session variable to
track the first entry into a session.

Thanks again…

A(Answer):

On 2004-03-06, Annie <An**@newsgroup.net> wrote:

Chung Leong wrote:

I think you’ve misinterpretted the purpose of session_start(). While the
function does create a new session when there isn’t one, its primary
function is to initialize the session mechanism (for instance, populate the
$_SESSION array). It’s best to interpret the "start" in session_start as
"from this point forward, the script will start making use of session
variables. The function that performs the opposite is session_write_close().

I suggest using a session variable to note whether the user info has been
saved or not, as follows:

<?php

session_start();

if(!@$_SESSION[‘user info saved’]) {
… save info in database …
$_SESSION[‘user info saved’] = true;
}

Uzytkownik "Annie" <An**@newsgroup.net> napisal w wiadomosci
news:cCS1c.45548$PR3.929733@attbi_s03…

I have a script to check if a session exists, if not then I log the user’s

info from $_Server

variables into a database (ip address, time, etc.).

The problem is that the test:

if(!session_id()) // if there is no session started
{
session_cache_limiter(‘nocache’);
session_cache_expire (20);
session_start();

include_once (‘../DB_functions.php’);

…the the other stuff, like logging goes here…
doesn’t seem to work…the test always produces true…is there something

I am missing?

Thanks,

Annie

Thanks…yeah the manual does not mention that you need to first run session_start before you check
session_id, and that does not match the way they suggest using it. I will use a session variable to
track the first entry into a session.

From http://www.php.net/manual/en/ref.session.php

Note: Please note when working with sessions that a record of a session
is not created until a variable has been registered using the
session_register() function or by adding a new key to the $_SESSION
superglobal array. This holds true regardless of if a session has been
started using the session_start() function.


http://home.mysth.be/~timvw

LEAVE A COMMENT