Forum Moderators: coopster

Message Too Old, No Replies

Testing whether a Session exists

and setting and unsetting session variables

         

kkrumlia

9:04 pm on Jun 2, 2003 (gmt 0)

10+ Year Member



I have a php file that opens as a popup that is done by using javascript functions inside php, anyway in this file a session starts then i test for a Session variable if it exists then i print something otherwise nothing happens
file ----1st.php------
if(isset($_SESSION['OUT'])) {
echo "$temp";
$test=1;
}

if ($test == 1){
unset($_SESSION['OUT']);
}

as you can see i do unset the variable after prinitng

the $_SESSION['OUT'] variable is set in another file, that file is activated from the 1st.php file via a form submit where the file 2nd.php initializes the OUT variable and then comes back to 1st.php .

This all thing works fine but for some reason when i close the popup and reopen "that is the 1st.php" I get the $temp which should not appear cause the _SESSION['OUT'] has been unset, tried unsetting the whole session still the same thing happens

Any ideas guys , thank you in advance

jatar_k

3:13 pm on Jun 3, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



kkrumlia,

Take a look at Session handling functions [php.net].

There are better functions for testing/unsetting session vars like
session_is_registered [php.net]
session_unregister [php.net]

You also need to use session_start [php.net] at the top of every page before any output is sent to the browser.

kkrumlia

3:21 pm on Jun 3, 2003 (gmt 0)

10+ Year Member



Hello jatar_k

If you are using $_SESSION (or $HTTP_SESSION_VARS), do not use session_register(), session_is_registered(), and session_unregister().

this is from the php documentation, and unfortunatly i'm using $_SESSION ; plus i do use session_start()

thanks anyways

jatar_k

3:29 pm on Jun 3, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Must be getting blind in my old age or they added that recently. :)

You could use a named session and then use those functions or you could try testing for a specific value in the session var. It is a little strange. have you messed with different functions to kill the session? ie (session_unset and session_destroy)

kkrumlia

3:45 pm on Jun 3, 2003 (gmt 0)

10+ Year Member



Yup tried session_unset and session_destroy and for some reason i get an error. And about using different names i don't know about that too many sessions each with a diff session id i dont know , One thing i'm thinking of is there a way i can check if a sesssion has started without starting a session tried using something like this but didn't work so far
if(isset($_REQUEST[session_name()])) {
// There is a session already available
session_start();
}
else
{
$sesstest=1;
}

jatar_k

3:52 pm on Jun 3, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I never thought of that method using $_REQUEST.

An associative array consisting of the contents of $_GET, $_POST, and $_COOKIE.

hm, it should work as long as the session is set as a cookie but it will not be a reliable way to check. If someone has cookies blocked it may end up appended to the url instead.

jatar_k

4:19 pm on Jun 3, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You could also try testing for session_id

kkrumlia

5:19 pm on Jun 3, 2003 (gmt 0)

10+ Year Member



I don't know what else to try
trying session_id
-----------------

I changed my session_name in the whole website so now it has a name , hoping to see if the name would be different if a session doesn't exist
So
session_name("k");
session_start();
these should be in the pages that session start
so now the page where i want to check if the session has been started or not
session_name();
this doesn't work cause as the documentation says
The session name is reset to the default value stored in session.name at request startup time. Thus, you need to call session_name() for every request.
So it's a new request and it will return the default whether a session does exist or not .
Same thing regarding session_id();

So running out of options anyways i'll see what will happen next , by the way thanks jatar_k for the followups