Jump to content


This topic is now archived and is closed to further replies.


How to compare session variables with form validation [RESOLVED]

Recommended Posts

I have a captcha image and I need to compare the session variable with the form validation but I don't know how to compare the session variable with the validation.

This is the code for the captcha image
$strlength = rand(4,7);
$captchastr = "";
$textornumber = rand(1,3);
if($textornumber == 1)
$captchastr .= chr(rand(49,57));
if($textornumber == 2)
$captchastr .= chr(rand(65,78));
if($textornumber == 3)
$captchastr .= chr(rand(80,90));
$randcolR = rand(100,230);
$randcolG = rand(100,230);
$randcolB = rand(100,230);

//initialize image $captcha is handle dimensions 200,50
$captcha = imageCreate(200,50);
$backcolor = imageColorAllocate($captcha, $randcolR, $randcolG, $randcolB);

$txtcolor = imageColorAllocate($captcha, ($randcolR - 20), ($randcolG - 20), ($randcolB - 20));

$clockorcounter = rand(1,2);
if ($clockorcounter == 1)
$rotangle = rand(0,45);
if ($clockorcounter == 2)
$rotangle = rand(315,360);

//$i*25 spaces the characters 25 pixels apart
for($i=1; $i<=4;$i++)
for($i=1; $i<=4;$i++)
//Send the headers (at last possible time)
header('Content-type: image/png');

//Output the image as a PNG

//Delete the image from memory

$_SESSION["captchastr"] = $captchastr;

This is the feedback form



$your_email = "me@mysite.com";

// This is what is displayed in the email subject line
// Change it if you want
$subject = "Message via your contact form";

// This is displayed if all the fields are not filled in
$empty_fields_message = "<p>Please go back and complete all the fields in the form.</p>Click <a class=\"two\" href=\"javascript:history.go(-1)\">here</a> to go back";

// This is displayed when the email has been sent
$thankyou_message = "<p>Thankyou. Your message has been sent.</p>";

// You do not need to edit below this line

$name = stripslashes($_POST['txtName']);
$email = stripslashes($_POST['txtEmail']);
$message = stripslashes($_POST['txtMessage']);

if (!isset($_POST['txtName'])) {


<p class="style3">Please fill in this form if you have any queries or have any suggestions.</p>
<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">

    <p class="style3"><label for="txtName">Name:</label>
    <input type="text" title="Enter your name" name="txtName" size="30"/></p>

    <p class="style3"><label for="txtEmail">Email:</label>
    <input type="text" title="Enter your email address" name="txtEmail" size="30"/></p>

    <p class="style3"><label for="txtMessage">Comments:</label>
    <textarea title="Enter your message" name="txtMessage" rows="10" cols="30"></textarea></p>

    <img src="includes/captcha.php">

   <p class="style3"><label for="verify">Enter code</label>
    <input type="text" title="Enter the image text" name="verify" id="verify" value="" size="10"/></p>

    <p class="style3"><label title="Send your message">
    <input type="submit" value="Send" class="submit-button"/></label></p>




elseif (empty($name) || empty($email) || empty($message)) {

    echo $empty_fields_message;


else {

    // Stop the form being used from an external URL
    // Get the referring URL
    $referer = $_SERVER['HTTP_REFERER'];
    // Get the URL of this page
    $this_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"];
    // If the referring URL and the URL of this page don't match then
    // display a message and don't send the email.
    if ($referer != $this_url) {
        echo "You do not have permission to use this script from another URL.";

    // The URLs matched so send the email
    mail($your_email, $subject, $message, "From: $name <$email>");

    // Display the thankyou message
    echo $thankyou_message;   


I have tried inserting this piece of code but i get an error message saying syntax error unexpected $end

[code]if(!$_POST['verify']) {
    die('Verify field was blank.');
        } else {
if($_SESSION['captchastr'] != $_POST['verify']) {
    die('You did not enter the code from the box correctly.');
} [/code]

Someone please help!!

Share this post

Link to post
Share on other sites
Because your syntax is whack.

if (!$_POST['verify']) {
    die('Verify field was blank.');
} elseif ($_SESSION['captchastr'] != $_POST['verify']) {
    die('You did not enter the code from the box correctly.');

Share this post

Link to post
Share on other sites


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.