Forum Moderators: coopster

Message Too Old, No Replies

Trying to destry uninitialized session

         

shimeal

11:07 pm on Jan 14, 2005 (gmt 0)

10+ Year Member



Hello - I am receiving the following error message:
"Warning: Trying to destry uninitialized session on..."

with the following code:

<?
if
($_SESSION['CustomerID'] == "")
{
echo "<a href='faqs.php'>Frequently Asked Questions Page</a>";
}
else
{
echo "<a href='faqs.php'>Frequently Asked Questions Page</a>";
}{
session_unregister("CustomerID");
session_unregister("UserID");
session_unregister("FirstName");
session_unregister("AccessLevel");
session_destroy();
}
?>

MY PROBLEM IS THIS: I DO NOT WANT TO DESTROY THIS SESSION VARIABLE UNLESS THE USER CLICKS ON THE <A HREF='FAQS.PHP'> LINK. IF THE USER DOES NOT CLICK ON THIS LINK, I WANT THE SESSION TO CONTINUE. ANY SUGGGESTIONS?

dkin

11:36 pm on Jan 14, 2005 (gmt 0)

10+ Year Member



I am no good with sessions just yet but when I turn error checking on I get this error.

Notice: Undefined variable: _SESSION in /home/acewebde/public_html/game/session.php on line 5

which would be this line

($_SESSION['CustomerID'] == "")

if that helps at all.

StupidScript

11:46 pm on Jan 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



But if there is no session, then there's no session to destroy ... anyway ...

To determine whether the session has been created:

if (isset($_SESSION['CustomerID'])) {

Add a tag to your FAQ link code to identify whether the session had been established, at that point:

<a href="faq.php?sid=1">FAQ</a>
or
<a href="faq.php?sid=0">FAQ</a>

Then, put the "session_destroy" code on the FAQ page, instead of the page with the FAQ link. Since PHP has no idea what happens after the page has left the server, it has no way of determining whether the link will be clicked or not.

1 - User arrives at current page
2 - Check for session, and write appropriate FAQ page link
3 - User clicks FAQ link:
3a: On FAQ page,

if ($sid) {
destroy
}

So, on the first page in this example:

<?

if (isset($_SESSION['CustomerID'])) { 

echo "<a href='faqs.php?sid=1'>Frequently Asked Questions Page</a>"; 

} 

else { 

echo "<a href='faqs.php?sid=0'>Frequently Asked Questions Page</a>";

}

?>

On the FAQ page:

<?

if ($sid) {

session_destroy ... etc.

}

?>

shimeal

1:02 am on Jan 15, 2005 (gmt 0)

10+ Year Member



Thanks - I have one problem, though. I am using the following code (as you suggested):

<?
if (isset($_SESSION['CustomerID']))
{
echo "<a href='faqs.php?sid=1'>Frequently Asked Questions Page</a>";
}
else {
echo "<a href='faqs.php?sid=0'>Frequently Asked Questions Page</a>";
}
?>

AND on the FAQS.php page, I have this code in the heading:

<?
if ($sid) {
session_unregister("CustomerID");
session_unregister("UserID");
session_unregister("FirstName");
session_unregister("AccessLevel");
session_destroy();
}
?>
<? session_start();?>

The page is not destroying the session. Any ideas? PLEASE NOTE: I need to have the session_start code on the page because I have an email_submit piece of php code that uses it.