Jump to content

clearing a SESSION variable


Gorf

Recommended Posts

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:
[code]
**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" ); ?>
[/code]


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:

[code]
<?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*
[/code]



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?
Link to comment
Share on other sites

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

[code]
    if ( isset($_SESSION['ERROR']))
    {
        echo "<p>";
        echo "- ->".$_SESSION['ERROR']."<-<br />\n";
        echo "</p>";
    
            //clear session variable for next use
            unset($_SESSION['ERROR']);
         }
...
[/code]
Link to comment
Share on other sites

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

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

[code]
    <span class="announce">
    </span>
[/code]

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:

[code]
    <span class="announce">
          <p>
        - -><-<br />
        </p>
    </span>
[/code]
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.

[code]

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;
}

[/code]

That seems to fix it right up. Nor sure why, but there it is.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.