Simple session help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Awesomeness
Forum Newbie
Posts: 7
Joined: Tue Feb 23, 2010 9:13 pm

Simple session help

Post by Awesomeness »

I'm having trouble getting rid of a variable in my session:

Code: Select all

<?php
 
session_start(); //Use the session we made to get the client's input
 
$user_content=trim($_POST['user_content']);
$captcha_guess=strtolower(trim($_POST['captcha_guess']));
 
if(isset($_SESSION['encoded_captcha'])) {
    if($_SESSION['encoded_captcha'] == md5($captcha_guess)) {
        if($user_content != "") {
            //It's a human, or a really smart bot
            echo("<p style='color:green;'>Congratz!  You passed the CAPTCHA test!  Your submition has been received.  It was: ");
            echo($user_content);
        } else {
            echo("<p style='color:red;'>Your submission is empty.  Try again.</p>");
        }
    } else {
        //The bot (either that or he's an idiot) entered the wrong captcha
        //Pwn him!!!
        echo("<p style='color:red;'>You did not enter the CAPTCHA correctly.  Try again.</p>");
    }
}
unset($_SESSION]["encoded_captcha"]);
 
?>
What am I doing wrong? I've looked through the documentation of unset(), but it didn't help me.
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Simple session help

Post by klevis miho »

Do a session_destroy();
Awesomeness
Forum Newbie
Posts: 7
Joined: Tue Feb 23, 2010 9:13 pm

Re: Simple session help

Post by Awesomeness »

klevis miho wrote:Do a session_destroy();
I have 2 problems with that:

a: I've already tried it, it didn't work.
b: When I continue building my website, I might need my session later.

When I first go to the page with this code, before my session variable is created, it does what I want, so I know that the problem is with unset().
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Simple session help

Post by klevis miho »

You have a error in the syntax:
make this:
unset($_SESSION]["encoded_captcha"]);

to this:
unset($_SESSION["encoded_captcha"]);
Awesomeness
Forum Newbie
Posts: 7
Joined: Tue Feb 23, 2010 9:13 pm

Re: Simple session help

Post by Awesomeness »

Even when there is no syntax error, it still doesn't work.
Awesomeness
Forum Newbie
Posts: 7
Joined: Tue Feb 23, 2010 9:13 pm

Re: Simple session help

Post by Awesomeness »

Nevermind, with thorough research I discovered that it is a PHP bug. I've implemented a complicated alternative that seems to work.
Post Reply