It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming PHP and MySQL Forum

Image Verification

Moderator: Malcolm

Image Verification

Postby dflynn on Mon Feb 16, 2009 8:36 pm

Hey guys. I'm back from school for a week, expect to see me around here a bit. :P

I'm having an issue with an image verification I'm trying to adapt from an email form.

Here is the code I have running the contact form with image verification:
http://www.thewebhelp.com/php/php_conta ... alidation/

I find whenever I try to adapt it to verify a form that will submit to a database, I run into trouble.

Here's my code for the form:
Code: Select all
<h2>Sign The Guestbook</h2>
Please fill out ALL fields before submitting. Thank you.<br /><br />
<form name="form1" method="post" action="?guest=guestbook2" onsubmit='return validate(this)'><input type=hidden name=todo value=post>

First Name: <input name="firstname" class="text" type="text" size="25" maxlength="50" /><br /><br />
Last Name: <input name="lastname" class="text" type="text" size="25" maxlength="50" /><br /><br />
Type Verification Image: <input name="verification" type="text" id="verification" size="5" maxlength="4" /> <img src="inc/verificationimage.php?<?php echo rand(0,9999);?>" alt="verification image, type it in the box" width="50" height="24" align="absbottom" /><br />

Message:<br />
<textarea name="message" cols="5" rows="5" id="message" style="padding:2px; border:1px solid #CCCCCC; width:260px; height:100px; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px;"></textarea>
Testimonial? <input type=checkbox name=testimonial value="yes"><br /><br />
<input type="submit" value="Submit" />
</form>


And here's what I have for the submitting:
Code: Select all
if(isset($todo) and $todo=="post"){

$status = "OK";
$msg="";

if(!isset($firstname) or strlen($firstname) <2){
$msg=$msg."Your first name should be at least 2 characters long. <br />";
$status= "NOTOK";}

if(!isset($lastname) or strlen($lastname) <2){
$msg=$msg."Your first name should be at least 2 characters long. <br />";
$status= "NOTOK";}            

if(!isset($message)){
$msg=$msg."Please leave a message. <br />";
$status= "NOTOK";}

if ($testimonial<>"yes"){
$testim="no";
}else{
$testim="yes";}

if(md5($verification).'a4xn' != $_COOKIE['tntcon']){
$msg=$msg."Please make sure your Verification Code is correct. <br />";
$status= "NOTOK";}

if($status<>"OK"){
echo "<font face='Verdana' size='2' color=red>$msg</font><br><input type='button' value='Retry' onClick='history.go(-1)'>";
}else{


$datein = date("Y-m-d");

$query=mysql_query("INSERT INTO table(firstname,lastname,datein,message,testim) values('$firstname','$lastname','$datein','$message','$testim')");
echo "<p>Thank you for taking the time to sign the Guestbook.</p>";
setcookie('tntcon','');
}
}



Any ideas?

Thanks for your help
User avatar
dflynn
500+ Club
 
Posts: 860
Joined: Wed Oct 03, 2007 9:06 pm
Location: Guelph, Canada

Re: Image Verification

Postby rse on Thu Mar 19, 2009 1:18 am

Hi :),

I've had a quick scan through the code and have seen a few possible errors.

Please find an amended version below:

Code: Select all
if(isset($todo) and $todo=="post") {

$status = "OK";
$msg="";

if(!isset($firstname) || strlen($firstname) <2){
$msg=$msg."Your first name should be at least 2 characters long. <br />";
$status= "NOTOK";}

if(!isset($lastname) || strlen($lastname) <2){
$msg=$msg."Your first name should be at least 2 characters long. <br />";
$status= "NOTOK";}           

if(!isset($message)){
$msg=$msg."Please leave a message. <br />";
$status= "NOTOK";}

if ($testimonial != "yes"){
$testim="no";
}else{
$testim="yes";}

if(md5($verification).'a4xn' != $_COOKIE['tntcon']){
$msg=$msg."Please make sure your Verification Code is correct. <br />";
$status= "NOTOK";}

if($status != "OK"){
echo "<font face='Verdana' size='2' color=red>$msg</font><br><input type='button' value='Retry' onClick='history.go(-1)'>";
}else{


$datein = date("Y-m-d");

$query=mysql_query("INSERT INTO table(firstname,lastname,datein,message,testim) values('$firstname','$lastname','$datein','$message','$testim')");
echo "<p>Thank you for taking the time to sign the Guestbook.</p>";
setcookie('tntcon','');
}
}


I haven't tested it but if you're still getting errors please post them and I'll do my best to help you further.

Best regards,
Adam Williams
PHP Web Developer

Personal: http://30things.net
Blog: http://www.root-servers.co.uk
User avatar
rse
100+ Club
 
Posts: 141
Joined: Sun Oct 10, 2004 8:15 pm
Location: Leeds, UK

Re: Image Verification

Postby dflynn on Thu Mar 19, 2009 3:19 am

Thanks for the effort, still doesn't seem to work. Could you tell me what you changed, just so I know where I might have gone wrong?
User avatar
dflynn
500+ Club
 
Posts: 860
Joined: Wed Oct 03, 2007 9:06 pm
Location: Guelph, Canada

Re: Image Verification

Postby rse on Thu Mar 19, 2009 3:25 am

I changed your "or" operators to "||" as "or" is not a valid PHP operator as far as I know.

I also replaced your "<>" with "!=" as I figured that's what you meant.

What isn't it doing? Are you getting a parse error "... on line x", or is it just running fine but not doing what you expect it to?

If you can let me know and I'll do my best to help.

Cheers

Ads :)
Adam Williams
PHP Web Developer

Personal: http://30things.net
Blog: http://www.root-servers.co.uk
User avatar
rse
100+ Club
 
Posts: 141
Joined: Sun Oct 10, 2004 8:15 pm
Location: Leeds, UK

Re: Image Verification

Postby dflynn on Thu Mar 19, 2009 3:30 am

hmmm. or has worked with other forms I've used, as did the <>, but those still seem to be working.

It's not throwing errors at me.
http://www.nnjoi.com/shannon/guestbook.php

Try entering something on the right, it will tell you the verification code is wrong.
User avatar
dflynn
500+ Club
 
Posts: 860
Joined: Wed Oct 03, 2007 9:06 pm
Location: Guelph, Canada

Re: Image Verification

Postby rse on Thu Mar 19, 2009 3:36 am

I can't find any reference to "or" being allowed in PHP although I could be wrong. I'mm just going by http://www.w3schools.com/PHP/php_operators.asp.

Please can you post the code for inc/verificationimage.php so I can understand how it's supposed to work?

Many thanks

Ads
Adam Williams
PHP Web Developer

Personal: http://30things.net
Blog: http://www.root-servers.co.uk
User avatar
rse
100+ Club
 
Posts: 141
Joined: Sun Oct 10, 2004 8:15 pm
Location: Leeds, UK

Re: Image Verification

Postby dflynn on Thu Mar 19, 2009 3:41 am

Sure can.
Code: Select all
<?php
// -----------------------------------------
//  The Web Help .com
// -----------------------------------------

header('Content-type: image/jpeg');

$width = 50;
$height = 24;

$my_image = imagecreatetruecolor($width, $height);

imagefill($my_image, 0, 0, 0xFFFFFF);

// add noise
for ($c = 0; $c < 40; $c++){
   $x = rand(0,$width-1);
   $y = rand(0,$height-1);
   imagesetpixel($my_image, $x, $y, 0x000000);
   }

$x = rand(1,10);
$y = rand(1,10);

$rand_string = rand(1000,9999);
imagestring($my_image, 5, $x, $y, $rand_string, 0x000000);

setcookie('tntcon',(md5($rand_string).'a4xn'));

imagejpeg($my_image);
imagedestroy($my_image);
?>
User avatar
dflynn
500+ Club
 
Posts: 860
Joined: Wed Oct 03, 2007 9:06 pm
Location: Guelph, Canada

Re: Image Verification

Postby rse on Thu Mar 19, 2009 4:00 am

I'm just gonna setup a test site on my server to test it out.

I see what the script does now so hopefully I can figure out the problem for you.

I will come back to you as soon as I can.

Nice blog by the way - nnjoi.com. Might have to hire you for some graphics work :)
Adam Williams
PHP Web Developer

Personal: http://30things.net
Blog: http://www.root-servers.co.uk
User avatar
rse
100+ Club
 
Posts: 141
Joined: Sun Oct 10, 2004 8:15 pm
Location: Leeds, UK

Re: Image Verification

Postby dflynn on Thu Mar 19, 2009 4:11 am

Thank you :D

Thanks so much for the help.
If you need some graphics done let me know :P
User avatar
dflynn
500+ Club
 
Posts: 860
Joined: Wed Oct 03, 2007 9:06 pm
Location: Guelph, Canada

Re: Image Verification

Postby rse on Thu Mar 19, 2009 4:12 am

Add this before your

Code: Select all
if(isset($todo) and $todo=="post") {

$status = "OK";
$msg="";


:

Code: Select all
$todo = $_REQUEST['todo'];
$firstname = $_REQUEST['firstname'];
$lastname = $_REQUEST['lastname'];
$message = $_REQUEST['message'];
$testimonial = $_REQUEST['testimonial'];
$verification = $_REQUEST['verification'];


It seems that the reason it wasn't accepting the code was not because it was incorrect but because it wasn't able to read the posted field.

I have tested the above solution and it works.

Please let me know if you have any more issues.

You can also temporarily check out my working example at http://www.exassist.net/static/d/ .

Cheers

Ads
Adam Williams
PHP Web Developer

Personal: http://30things.net
Blog: http://www.root-servers.co.uk
User avatar
rse
100+ Club
 
Posts: 141
Joined: Sun Oct 10, 2004 8:15 pm
Location: Leeds, UK

Next

Who is online

Users browsing this forum: Yahoo [Bot] and 0 guests