473,412 Members | 3,471 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Bizarre (I think) session behaviour

Hi...

I'm using session_set_save_handler() and have defined some functions to
store session data in a database. It all works marvellously. Except for one
small thing. If I do something like:

...
if(isset($_SESSION['my_variable']))
...

the system calls the sess_write function, but passes an empty string as the
session variable parameter. Is this correct? I'm getting around this by
using an if clause and returning false if it's an empty string. This seems
to work ok. But is this correct behaviour? Or is it a bug?

TIA.

Plankmeister.
Jul 17 '05 #1
3 2020
"The Plankmeister" <pl******************@hotmail.com> wrote in message news:<3f***********************@dread16.news.tele. dk>...
Hi...

I'm using session_set_save_handler() and have defined some functions to
store session data in a database. It all works marvellously. Except for one
small thing. If I do something like:

...
if(isset($_SESSION['my_variable']))
...

the system calls the sess_write function, but passes an empty string as the
session variable parameter. Is this correct? I'm getting around this by
using an if clause and returning false if it's an empty string. This seems
to work ok. But is this correct behaviour? Or is it a bug?

TIA.

Plankmeister.

This is correct behaviouir, you are checking to see if it isset,
basicaly a variable in use, it can contain nothing, example

isset($a) : false

$a = ""; // empty string, basicaly set to nothing, but set it is
isset($a) // evals to true

$a = array(); // empty array()
isset($a) // vals to tru

$a = ""; // empty string, basicaly set to nothing, but set it is
unset($a);// removes string from existance, off into nerver, never..
isset($a) // evals to false
change your if to this:
if( isset($_SESSION['my_variable'] != "" )
Mike Bradley
http://gzen.myhq.info -- free online php tools
Jul 17 '05 #2
CountScubula wrote:
"The Plankmeister" <pl******************@hotmail.com> wrote in
message news:<3f***********************@dread16.news.tele. dk>...
Hi...

I'm using session_set_save_handler() and have defined some functions
to
store session data in a database. It all works marvellously. Except
for one
small thing. If I do something like:

...
if(isset($_SESSION['my_variable']))
...

the system calls the sess_write function, but passes an empty string
as the
session variable parameter. Is this correct? I'm getting around this
by
using an if clause and returning false if it's an empty string. This
seems
to work ok. But is this correct behaviour? Or is it a bug?

TIA.

Plankmeister.

This is correct behaviouir, you are checking to see if it isset,
basicaly a variable in use, it can contain nothing, example

isset($a) : false

$a = ""; // empty string, basicaly set to nothing, but set it is
isset($a) // evals to true

$a = array(); // empty array()
isset($a) // vals to tru

$a = ""; // empty string, basicaly set to nothing, but set it is
unset($a);// removes string from existance, off into nerver, never..
isset($a) // evals to false
change your if to this:
if( isset($_SESSION['my_variable'] != "" )


Generally I would agree with Mike, except that the above code has two
problems. One, you're testing whether a boolean value is set
($_SESSION['something'] == 'something') will always evaluate to true or
false. This is pointless.

Secondly, to generalize, you want to know if something is empty, not equal
to the empty string. Therefore, for both semantic and readability reasons,
you should use empty().

empty($somevar) returns true if $somevar is
a) not defined (e.g. isset($somevar) == false)
b) equal to ""
c) equal to 0
d) equal to "0"
e) equal to NULL
f) equal to FALSE
g) equal to array()
h) equal to var $var (defined but not set to equal anything)
i) an object with empty properties.
Jul 17 '05 #3
> Secondly, to generalize, you want to know if something is empty, not equal
to the empty string. Therefore, for both semantic and readability reasons,
you should use empty().


Ah yes, I stand corrected. :)
Mike Bradley
http://gzen.myhq.info -- free online php tools
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: serge calderara | last post by:
Dear all I have a strange behaviour using session. ON my form load when the page is first display I create a session variable to stor connection objects as follow : If not IsPostBack then...
15
by: John Howie | last post by:
I've found very unusual behavious when using sessions on two different servers. I'm using sessions to handle simple log in. When the form submits the values are checked against a MySQL table. If...
5
by: David Haynes | last post by:
I am running Apache 2, PHP 5.0.5, and windows XP I have one php file that populates a SESSION variable and another that consumes it. If I call the first one from the URL line of the browser,...
11
by: Glenn | last post by:
Hi I've been experimenting with managing state using the Session object. I've created a simple WS with a couple of methods, one which sets a string value, another that retrieves it. Each...
11
Dormilich
by: Dormilich | last post by:
recently I encounter a very strange behaviour of the session variable. if the cookies are disabled, the session id is totally misplaced (see code). instead being attached to the URL it is placed...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.