Welcome Guest!

If you do not have an account yet on The Web Squeeze forums, please Register! It’s FREE and there are many benefits:

  • Receive Fast Advice
  • Learn Programming Languages
  • Get Professional Website Reviews
  • Quick Troubleshooting Assistance

> Problem Unsetting Session Data

This is a discussion on Problem Unsetting Session Data, within the PHP section. This forum and the thread "Problem Unsetting Session Data" are both part of the Programming Your Website category.

 
Reply to this topicStart new topic
> Problem Unsetting Session Data
weasel2006
post May 12 2008, 05:16 PM
Post #1


Fresh Squeezed
**

Group: Members
Posts: 14
Joined: 14-February 08
Member No.: 130



Hi there,
I am having some trouble unsetting session data.. no matter what I try, it wont unset.

My site works thus:

index.php holds the layout and also includes a page called view.php
The job of view.php is to call and show content from the database depending on what index.php?view= is set to by using $_GET['view'] then shows the apropriate content.

At the minute I am working on an error system which is causing the problems.
I have a page in the database called error which includes error_handling.php which will deal with showing all error messages.

In my contact form for the site, I have it so that if something is a miss, it does this:
$_SESSION['fcode'] = Error message;

Now, after the error has been shown via the error page I try and add unset the session data by adding
unset($_SESSION['fcode']); directly after echo $_SESSION['fcode'];
But, the session does not unset.

This is the error_handling.php
CODE
    session_start();
    
//Error Handling
    
    if(!isset($_SESSION['fcode']))
    {
        header('Location: index.php');
    }
    else
    {
        echo $_SESSION['fcode'];
        unset($_SESSION['fcode'], $fcode );
    }


What I'm trying to do is if someone comes along and tries accessing mysite.com/index.php?vew=error if there is no errors in the session, it redirects them back home. But, it doesnt work because after I submit an empty form, I am taken to the error page, if I navigate away from the error page and come back, the error is still present and the session data is still there.

I just cant work it out :/

Any help greatly appreciated!

Thanks, weasel

This post has been edited by weasel2006: May 12 2008, 05:18 PM
Go to the top of the page
 
+Quote Post
Rakuli
post May 12 2008, 05:57 PM
Post #2


Squeeze Machine
*****

Group: Administrators
Posts: 650
Joined: 13-February 08
From: Catching the squeezed drips downunder.
Member No.: 13



Hmm.

You may wish to try something like


CODE
session_start();
    
//Error Handling
    
    if(empty($_SESSION['fcode']))
    {
        header('Location: index.php');
                exit(); // Call to exit script as script continues to run after a header location redirect.
    }
    else
    {
        echo $_SESSION['fcode'];
      $_SESSION['fcode'] = '';
    }


Un-setting Session variables has caused me problems on occasion as well. I find it better just to make it an empty variable and check it that way.


--------------------
Luke Dingle . com

Turn Over a Playful Leaf on Web Design -- read about the javascript cat
Go to the top of the page
 
+Quote Post
weasel2006
post May 12 2008, 06:18 PM
Post #3


Fresh Squeezed
**

Group: Members
Posts: 14
Joined: 14-February 08
Member No.: 130



Hi Rakuli, Thanks for replying.

I tried the above code but it's still the same.

This code below wouldn't cause a problem would it? It is taken from my view.php full code below
CODE
$result = eval("?>" . htmlspecialchars_decode($result) . "<?php;");

Because I use php includes in some of the content stored in the database, I have to decode it so it renders properly
otherwise it just outputs the php include code instead of parsing it.
Just wondered if it could be a problem? Someone suggested it might be but I can't work out how?

View.php
CODE
session_start();

//Get Pages From Database
    $con=new mysql();
    
    $id = mysql_real_escape_string($_GET['view']);
    
    $query = mysql_query("SELECT `body`, `id`
                            FROM `pages`
                            WHERE `id` = '$id'");                
    

//Set Default View To Home

    if(!$row = mysql_fetch_assoc( $query ))
        {
            $query = mysql_query("SELECT `body`, `id`
                            FROM `pages`
                            WHERE `id` = 'home'");
            $row = mysql_fetch_assoc( $query );
        }

//Show Error Page
        
    if($id == 'error')
        {
            $query = mysql_query("SELECT `body`, `id`
                            FROM `spages`
                            WHERE `id` = '$id'");
            $row = mysql_fetch_assoc( $query );
        }
        

    $result = html_entity_decode( $row['body'] );
    $result = eval("?>" . htmlspecialchars_decode($result) . "<?php;");

//Show Normal Pages

echo $result;


Thanks again!

This post has been edited by weasel2006: May 12 2008, 06:30 PM
Go to the top of the page
 
+Quote Post
Jason
post May 13 2008, 01:14 AM
Post #4


Master of the Universe
******

Group: Advisors
Posts: 1,157
Joined: 15-February 08
From: London, England
Member No.: 141



Is the session even echoing?

Are you sure that you have the echo the right way around.

CODE
echo $_SESSION['fcode'];
unset($_SESSION['fcode']);


From the above how do you know the session is being unset?

CODE
unset($_SESSION['fcode']);
echo $_SESSION['fcode'];


Here you will be unsetting the session. Then printing it. This will tell you if it is being unset...


Failing this, have you got PHP Error checking on full?


If this isn't the problem then tear your script apart. New document. Copy and paste it back in using small sections. It shouldn't take you more than 10 minutes to track down the issue.


--------------------
Go to the top of the page
 
+Quote Post
weasel2006
post May 13 2008, 01:04 PM
Post #5


Fresh Squeezed
**

Group: Members
Posts: 14
Joined: 14-February 08
Member No.: 130



SOLVED!!!

Forgot to remove an include to a test session page so I had two sessions running, one was obviously some how populating the other sarcastic_hand.gif

Thanks for the help, it steered me on the right track!!

This post has been edited by weasel2006: May 13 2008, 07:16 PM
Go to the top of the page
 
+Quote Post
If you found The Web Squeeze to be helpful, please donate so we can keep this site FREE, FRESH, and fortified with Web Design & Development info!
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   11 unitedcraig 329 20th February 2008 - 02:48 AM
Last post by: Monie
No New Posts   3 mcdanielnc89 236 23rd February 2008 - 08:14 AM
Last post by: Marc
No New Posts   1 karinne 198 20th February 2008 - 11:48 AM
Last post by: karinne
No New Posts   6 karinne 300 20th February 2008 - 06:52 PM
Last post by: Stuart
No New Posts 0 karinne 218 22nd February 2008 - 07:55 AM
Last post by: karinne