Jump to content


Photo

How to compare session variables with form validation [RESOLVED]


  • Please log in to reply
2 replies to this topic

#1 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 08 August 2006 - 01:35 AM

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
<?php
session_start();
$strlength = rand(4,7);
$captchastr = "";
for($i=1;$i<=$strlength;$i++)
{
$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));
for($i=1;$i<=$strlength;$i++)
{

$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
imagettftext($captcha,rand(14,20),$rotangle,($i*25),30,$txtcolor,"/arial.ttf",substr($captchastr,($i-1),1));
}
for($i=1; $i<=4;$i++)
{
imageellipse($captcha,rand(1,200),rand(1,50),rand(50,100),rand(12,25),$txtcolor);
}
for($i=1; $i<=4;$i++)
{
imageellipse($captcha,rand(1,200),rand(1,50),rand(50,100),rand(12,25),$backcolor);
}
//Send the headers (at last possible time)
header('Content-type: image/png');

//Output the image as a PNG
imagePNG($captcha);

//Delete the image from memory
imageDestroy($captcha);

$_SESSION["captchastr"] = $captchastr;
?>

This is the feedback form

<html>
<head>
</head>
<body>

<?php
session_start();

$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>

</form>

<?php

}

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.";
        exit;
    }

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

    // Display the thankyou message
    echo $thankyou_message;   
}

?>
</body>
</html>

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

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.'); 
}

Someone please help!!
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 08 August 2006 - 02:40 AM

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.'); 
}


#3 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 08 August 2006 - 10:26 AM

i got that from a tutorial
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users