Jump to content


Photo

clearing a SESSION variable


  • Please log in to reply
6 replies to this topic

#1 Gorf

Gorf
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 24 March 2006 - 06:00 PM

I have a site with a form. When the form submits, the page is processed, and then depending on the outcome of the code, sets a session variable and then redirects back to the page. That way the user never has a refresh situation where the browser alerts them about it needing to resubmit POST data and ends up submitting the data twice, or three times, or etc.

So first here is the code, for now it's an emailer basically:
**this is contact.php**
<?PHP
require( "includes/include.general.php" );
require( "class.phpmailer.php" );

//The user has posted to the page to submit an email request
if ( isset( $_POST['sendmail'] ) )
{
    $from_name  = defang_urlencoding( chop_string( 30, $_POST['name'] ) );
    $from_email = defang_urlencoding( chop_string( 30, $_POST['email'] ) );
    $message    = clean_data( $_POST['message'] );
    
    $mail = new PHPMailer();
    
    // set mailer to use SMTP
    $mail->IsSMTP();
    
    // specify main and backup server, and the envelope information                       
    $mail->Host         = "localhost";      
    $mail->From         = $from_email;
    $mail->FromName     = $from_name;
    $mail->AddAddress(
                        $CONTACT['Email'],
                        $CONTACT['Name']
                     );
                     
    // set word wrap to 50 characters
    $mail->WordWrap     = 80;  
    
    // set email format to plain text             
    $mail->IsHTML( false );                   
    $mail->Subject      = "[SbS] Web contact";
    $mail->Body         = "This email was sent from the IP: ".get_ip( )."\n".
                          "on ".date( "M j" )." at ".date( "H:i:s" )."\n\n\n".$message;
    
    if ( $mail->Send( ) )
    {
        $_SESSION['ERROR'] = "Your email was sent successfully.";
        header( "location: contact.php" );
    }
    else
    {
        $_SESSION['ERROR'] = "An error has occurred sending your email.  Please try again later.";
        header( "location: contact.php" );
    }

}

require( "includes/overall.header.php" );
?>

<div class="content">
    <script language="JavaScript">
    function valid(form) {
        if ((form.name.value==null)||(form.name.value=="")){
            alert("Please enter your name.")
            form.name.focus()
            return false
        }
        if ((form.email.value==null)||(form.email.value=="")){
            alert("Please enter your email address.")
            form.email.focus()
            return false
        }
        
        alert("Just to be sure, is your email address:\n"+form.email.value+"\n\n Is this correct?")
    }
    </script>

    <span class="announce">
    <?PHP
    
    //This is for any errors that occur, they can be announced here
    if ( $_SESSION['ERROR'] != "" )
    {
        echo "<p>";
        echo "- ->".$_SESSION['ERROR']."<-<br />\n";
        echo "</p>";
    
            //clear session variable for next use
            $_SESSION['ERROR'] = "";
         }
    
    ?>
    </span>

    <p>
    Please use this form to contact us about any of the animals you see on this site, or to inquire about
    the possibility of other animals.
    </p>

    <form action="contact.php" method="POST" enctype="multipart/form-data" onSubmit="return valid(this)">    
        <p>
        Please feel free to contact us by email.  This email system requires that you have a
        legitimate email address.  This email system does log your IP address, however privacy
        is important to us, so your contact information is not stored anywhere in our system.
        </p>
            
        Your Name:<br />
        <input type="text" name="name" size="40" maxlength="30"><br />
        <br />
        Your Email Address:<br />
        <input type="text" name="email" size="40" maxlength="30"><br />
        <br />
        Text message:<br />
        <textarea name="message" rows="8" cols="50"></textarea><br />
        <br />
        <input name="sendmail" value="submit" type="submit">
    </form>
    <p />
    This email system is for contacting representatives of Snakes by Sasquatch only.  Any other
    use is stricly prohibited.
</div>

<?PHP require( "includes/overall.footer.php" ); ?> 


The include.general.php at the beginning of the code contains the session start information as well as a bunch of all purpose functions only a couple of which are used in the contact.php file:

<?PHP
    //Start the session
    session_start( );

    //This is a fix for Internet Exploder for SessionID's
    header("Cache-Control: private");   

    //Include the config for the site
    include_once("includes/config.php");

    //Include the class for the site database connectivity
    include_once( "includes/class.database.php" );
    
*snip* 



If you look in contact.php file you will see where I clear the session variable ERROR for next use. The problem I am running into is that no matter what I do to clear that session variable, it clears it globally for the whole page. I could clear it at the bottom of my code, and it still will only echo out "" back at the top. I have tried assigning the session variable to a different variable, and again I still get the exact same scenario. However if I take the statement out all together, the SESSION variable stays around, and it gets printed as expected. The downside of course is that it stays that way until it gets set to something else. It's like the processor sets the SESSION variable to "" before it does the HTML and the echo statement. I am very confused here. Can anyone offer some insight?


#2 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 24 March 2006 - 06:31 PM

have you tried using 'unset'?

<?php
unset($_SESSION['ERROR']);
?>

it'll get rid of it as if it was never set in the first place.
cheers
Mark
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#3 Gorf

Gorf
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 24 March 2006 - 07:08 PM

Yes, same effect.

#4 lead2gold

lead2gold
  • Members
  • PipPipPip
  • Advanced Member
  • 164 posts
  • LocationOttawa, On

Posted 24 March 2006 - 08:09 PM

[!--quoteo(post=358016:date=Mar 24 2006, 02:08 PM:name=Gorf)--][div class=\'quotetop\']QUOTE(Gorf @ Mar 24 2006, 02:08 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Yes, same effect.
[/quote]

    if ( isset($_SESSION['ERROR']))
    {
        echo "<p>";
        echo "- ->".$_SESSION['ERROR']."<-<br />\n";
        echo "</p>";
    
            //clear session variable for next use
            unset($_SESSION['ERROR']);
         }
...


#5 Gorf

Gorf
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 24 March 2006 - 10:56 PM

[!--quoteo(post=358034:date=Mar 24 2006, 03:09 PM:name=lead2gold)--][div class=\'quotetop\']QUOTE(lead2gold @ Mar 24 2006, 03:09 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
    if ( isset($_SESSION['ERROR']))
    {
        echo "<p>";
        echo "- ->".$_SESSION['ERROR']."<-<br />\n";
        echo "</p>";
    
            //clear session variable for next use
            unset($_SESSION['ERROR']);
         }
...
[/quote]

Using that exact piece of code, the html that is produced is this:

    <span class="announce">
    </span>

As you can see, the SESSION variable is being unset before the if statement is being evaluated. Otherwise I should be getting something like this at least:

    <span class="announce">
          <p>
        - -><-<br />
        </p> 
    </span>


#6 Gorf

Gorf
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 24 March 2006 - 11:44 PM

Well it appears to be a phenomenon unique to when I redirect the page back to itself. I'm not sure I understand why, but if I change my header( "location: xxx" ); line to be a different page, the code works perfectly.

Now I am really stumped, but that works for me for now I guess.

#7 Gorf

Gorf
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 25 March 2006 - 12:36 AM

Well if anyone is curious, I solved the problem. It appears to have something funny to do with leaving the page via a header( "location: xxx" ). So I wrapped the whole page into a if statement.


if ( isset( $_POST['something'] ) )
{
  Do the code for the POST;
  Set the SESSION variable;
  Use the header function to redirect to the page;
}
else
{
  Show the normal page HTML;
}


That seems to fix it right up. Nor sure why, but there it is.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users