Page 1 of 1

captcha session not sending

Posted: Sat Oct 07, 2006 12:55 pm
by paqman
I'm trying to get a captcha feature on my guestbook of my old website but the captcha code is not sending as a session variable as it should. Here's the code:

generator.php (free code from a site)

Code: Select all

<?php
session_start();
$strlength = rand(4,7);

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,"/home/paqmanwe/public_html/captcha/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; ?>
guestbook.php

Code: Select all

<? if($cmd == "sign") { ?>
<center><img src="image/header/guestbook.jpg"></center>
<form method="post" action="index.php?id=guestbook&cmd=submit">
<table>
<tr><td>Name:</td>
<td><input type="text" name="name" value="<? echo $_POST["name"]; ?>"></td></tr>
<tr><td>Email:</td>
<td><input type="text" name="email" value="<? echo $_POST["email"]; ?>"></td></tr>
<tr><td>Site:</td>
<td><input type="text" name="site" value="<? echo $_POST["site"]; ?>"></td></tr>
<tr><td>Country:</td>
<td><input type="text" name="country" value="<? echo $_POST["country"]; ?>"></td></tr>
<a name="cap"></a>
<? if($error == "failed") { ?>
<tr><td colspan=2><center><p style="color: red;">Verification Failed. Please try again:</p></td></tr><? } ?>
<tr><td colspan=2 style="text-align: center;"><img src="generator.php" border=0></td></tr>
<tr><td>Verification Code (above):</td>
<td><input type="text" name="capcheck"></td></tr>
</table>
Comments:<br>
<textarea cols=60 rows=6 name="comment"><? echo $_POST["comment"]; ?></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
<? $captex = $_SESSION['captchastr'];
echo $captex; 
// added this to try and show the session variable, but nothing comes out from it, leading me to assume the variable is not being sent 
?>

<? } 

if($cmd == "submit") {
$name = $_POST["name"];
$email = $_POST["email"];
$site = $_POST["site"];
$country = $_POST["country"];
$comment = $_POST["comment"];
$hour = date('g');
$chour = $hour - 3;
$date = date('F jS, Y - '.$chour.':i A');
$capcheck = $_POST['capcheck'];
$captchastr = $_SESSION['captchastr'];


if($capcheck == $captchastr && strlen($capcheck) !== 0) {
$result=MYSQL_QUERY("INSERT INTO gbook (id,name,email,site,country,comment,date)".
"VALUES ('NULL', '$name', '$email', '$site', '$country', '$comment', '$date')");
echo "<center><img src=\"image/header/guestbook.jpg\"></center>
Entry successfully posted! <a href=\"index.php?id=guestbook\">View it now!</a>"; }


if($capcheck !== $captchastr) { ?>
<body onload=setTimeout("document.form.submit();",1500)>
<form name="form" method="post" action="index.php?id=guestbook&cmd=sign&error=failed#cap">
<input type="hidden" name="name" value="<? echo $name; ?>">
<input type="hidden" name="email" value="<? echo $email; ?>">
<input type="hidden" name="site" value="<? echo $site; ?>">
<input type="hidden" name="country" value="<? echo $country; ?>">
<input type="hidden" name="comment" value="<? echo $comment; ?>">
</form>
<center><p style="color: red;">Verification failed. Redirecting...</p></center>
<? 
}
} ?>
Thank you!

Posted: Sat Oct 07, 2006 2:50 pm
by paqman
never mind, I figured it out - hadn't started a session on the guestbook page.