doni49 Posted October 26, 2006 Share Posted October 26, 2006 I'm trying to set up a captcha for my email contact page.GD IS enabled on my server. The following was copy/pasted from PHPInfo().[quote]gdGD Support enabledGD Version bundled (2.0.28 compatible)FreeType Support enabledFreeType Linkage with freetypeFreeType Version 2.1.4GIF Read Support enabledGIF Create Support enabledJPG Support enabledPNG Support enabledWBMP Support enabledXPM Support enabledXBM Support enabled[/quote]The following is from my script file:[code]<?php//captcha.php$RandomStr = md5(microtime());// md5 to generate the random string$ResultStr = substr($RandomStr,0,5);//trim 5 digit $NewImage =imagecreatefromjpeg("/home2/donirela/includes/captcha/captcha.gif");//image create by existing image and as back ground $LineColor = imagecolorallocate($NewImage,233,239,239);//line color $TextColor = imagecolorallocate($NewImage, 255, 255, 255);//text color-whiteimageline($NewImage,1,1,40,40,$LineColor);//create line 1 on image imageline($NewImage,1,100,60,0,$LineColor);//create line 2 on image imagestring($NewImage, 5, 20, 10, $ResultStr, $TextColor);// Draw a random string horizontally $captcha_key = $ResultStr;// carry the data through sessionheader("Content-type: image/gif");// out out the image imagejpeg($NewImage);//Output image to browser ?>[/code]I even went so far as to place an 'echo' statement in the captcha php file (the first line after [b]"<?php"[/b]. But the echo statement never happens and I don't get [b]ANY[/b] error messages.This is my attempt to show the image:[code]<img src="captcha.php">[/code] Quote Link to comment Share on other sites More sharing options...
Barand Posted October 26, 2006 Share Posted October 26, 2006 The only problem I can find with your image code is the last 2 lines where you send an image/gif header and then output a jpeg image.Also, after output, you need to call "imagedestroy($NewImage)" to free the memory.BUT, you need to store the random code somewhere so you can test against the stored value when the user replies. Maybe in a db table with username or session id as the key, or as a session variable. Quote Link to comment Share on other sites More sharing options...
doni49 Posted October 27, 2006 Author Share Posted October 27, 2006 [quote author=Barand link=topic=112736.msg458212#msg458212 date=1161890169]The only problem I can find with your image code is the last 2 lines where you send an image/gif header and then output a jpeg image.[/quote]Yes--I DID miss that part. Thanks for catching it! I'm starting with a script that I found online--the author had written it for use with a jpg. But I want to use a GIF. I changed two of the lines but I missed the third.[quote author=Barand link=topic=112736.msg458212#msg458212 date=1161890169]Also, after output, you need to call "imagedestroy($NewImage)" to free the memory.[/quote]I'll add that.[quote author=Barand link=topic=112736.msg458212#msg458212 date=1161890169]BUT, you need to store the random code somewhere so you can test against the stored value when the user replies. Maybe in a db table with username or session id as the key, or as a session variable.[/quote]The form page that uses the script stores the key.but what was befuddling me was that put an echo statement as the FIRST line in the captch file. [code]<?phpecho "WHY won't this work!!!?"; //But this never appeared in my browser so I thought the script wasn't even running. I even checked the pages's source code and it wasn't there.//I removed the echo statement before posting because I was afraid it would just confuse matters.//captcha.php$RandomStr = md5(microtime());// md5 to generate the random stringcontinue the code as shown previously..................[/code]I'll work with your suggestions.Thanks. Quote Link to comment Share on other sites More sharing options...
doni49 Posted October 27, 2006 Author Share Posted October 27, 2006 Well, I just confirmed that for SOME reason the image script ISN'T EVEN RUNNING.The "main page" (the one that contains img tag):[code]<td><?php $ireland = "Double Grrrr";?> <img src="captcha.php"> <input type="hidden" name="captcha_key" value="<? if(isset($ireland)){echo $ireland;}else{echo "gottaGetItWorking";} ?>"> </td>[/code]captcha.php:[code]<?php$ireland = "Grrrr";$RandomStr = md5(microtime());// md5 to generate the random string$ResultStr = substr($RandomStr,0,5);//trim 5 digit $NewImage =imagecreatefromjpeg("/home2/donirela/includes/captcha/captcha.gif");//image create by existing image and as back ground $LineColor = imagecolorallocate($NewImage,233,239,239);//line color $TextColor = imagecolorallocate($NewImage, 255, 255, 255);//text color-whiteimageline($NewImage,1,1,40,40,$LineColor);//create line 1 on image imageline($NewImage,1,100,60,0,$LineColor);//create line 2 on image imagestring($NewImage, 5, 20, 10, $ResultStr, $TextColor);// Draw a random string horizontally $captcha_key = $ResultStr;// carry the data through sessionheader("Content-type: image/gif");// out out the image imagegif($NewImage);//Output image to browser imagedestroy($NewImage); //destroy the resource?>[/code]The results of a "view source" in firefox:[quote] <td> <img src="captcha.php"> <input type="hidden" name="captcha_key" value="Double Grrrr"> </td>[/quote] Quote Link to comment Share on other sites More sharing options...
doni49 Posted October 27, 2006 Author Share Posted October 27, 2006 UPDATE: I've tried this in both FIREFOX AND IE6. Neither one will allow the script to even run. If I change it include the script file, then it DOES attempt to run it (the test variable DOES get set by the script) but of course it doesn't know what to do when told to output the image. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 27, 2006 Share Posted October 27, 2006 Comment out the header() line and run it on its own. See if you get any errors Quote Link to comment Share on other sites More sharing options...
doni49 Posted October 28, 2006 Author Share Posted October 28, 2006 Well I eventually got it to show the image--still not too sure why it wasn't. I don't think I changed anything of signifigance.So now the only issue left is storing the random string.The following are snippets from the main page (one with all the HTML code that displays the form).[code]<?php session_start(); $_SESSION['testing'] = "This string was added in contact3.html"; // <--this test variable will be important later.?> <td> <img src="captchascript.php"> <input type="hidden" name="captcha_key" value="<? if(isset($_SESSION['captcha_key'])){echo $_SESSION['captcha_key'];}?>"> </td><?php if(isset($_SESSION['captcha_key'])){echo $_SESSION['testing'];} // <-- this is the varible that was set at the top of the page?> [/code]This is captchascript.php that's called by the img tag:[code]<?php$_SESSION['testing'] = "This string was added in captcha.php"; // <--here I attempt to reset the testing variable$RandomStr = md5(microtime());// md5 to generate the random string$ResultStr = substr($RandomStr,0,5);//trim 5 digit $NewImage =imagecreatefromjpeg("/home2/donirela/includes/captcha/captcha.jpg");//image create by existing image and as back ground $LineColor = imagecolorallocate($NewImage,233,239,239);//line color $TextColor = imagecolorallocate($NewImage, 255, 255, 255);//text color-white//imageline($NewImage,1,1,1,1,$LineColor);//create line 1 on image //imageline($NewImage,1,100,60,0,$LineColor);//create line 2 on image imagestring($NewImage, 5, 20, 10, $ResultStr, $TextColor);// Draw a random string horizontally $_SESSION['captcha_key'] = $ResultStr;// carry the data through sessionheader("Content-type: image/jpeg");// out out the image imagejpeg($NewImage);//Output image to browser imagedestroy($NewImage); //destroy the resource?>[/code]When the testing variable is echo'd to the browser, what shows up is "This string was added in contact3.html". I added this test variable because when I tried to access the random string from outside the script file, it didn't seem to work.Thanks for all the help thus far! I greatly appreciate it! Quote Link to comment Share on other sites More sharing options...
Barand Posted October 28, 2006 Share Posted October 28, 2006 ::captcha.php::[code]<?phpsession_start();$RandomStr = md5(microtime());// md5 to generate the random string$ResultStr = substr($RandomStr,0,5);//trim 5 digit $_SESSION['captcha_key'] = $ResultStr;// carry the data through session$NewImage =imagecreatefromgif("/home2/donirela/includes/captcha/captcha.gif");//image create by existing image and as back ground $LineColor = imagecolorallocate($NewImage,233,239,239);//line color $TextColor = imagecolorallocate($NewImage, 255, 255, 255);//text color-whiteimageline($NewImage,1,1,40,40,$LineColor);//create line 1 on image imageline($NewImage,1,100,60,0,$LineColor);//create line 2 on image imagestring($NewImage, 5, 20, 10, $ResultStr, $TextColor);// Draw a random string horizontally header("Content-type: image/gif");// out out the image imagegif($NewImage);//Output image to browser imagedestroy($NewImage); //destroy the resource?>[/code]::testCaptcha.php::[code]<?phpsession_start();echo '<pre>', print_r($_SESSION, true), '</pre>'; //debug codeecho '<pre>', print_r($_POST, true), '</pre>'; //debug code if ($_SESSION['captcha_key']==$_POST['key']) { echo "<h3>Valid</h3>";}else { echo "<h3>Invalid</h3>";} ?><form method='post'> Name <input type="text" name="name" size="15"><br/> <img src='captcha.php'><br/> Enter Key from Image <input type="text" name="key" size="5" maxlength="5"><br/> <input type="submit" name="action" value="Submit"></form>[/code] Quote Link to comment Share on other sites More sharing options...
doni49 Posted October 28, 2006 Author Share Posted October 28, 2006 I tried your code this is the results and it worked. Now I need to integrate it into my code. Strange.Thanks for the assistance. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 28, 2006 Share Posted October 28, 2006 Iget[pre]Session ArrayArray( [captcha_key] => 4e9c1)Post ArrayArray( [name] => barand [key] => 4e9c1 [action] => Submit)Valid[/pre]??? Quote Link to comment Share on other sites More sharing options...
doni49 Posted October 28, 2006 Author Share Posted October 28, 2006 Barrand: Yes I posted that last message (telling you of my errors with your code--most people won't know what I'm talking about here) then I noticed one issue and fixed it. When I tested it, it worked.So I modfied my message as shown above. You must've tested it after i made the correction. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 28, 2006 Share Posted October 28, 2006 Glad all's well Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.