Hi all, session_start(); $width = 120; $height = 40; $length = 5; $baseList = '0123456789abcdfghjkmnpqrstvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $code = ""; $counter = 0; $image = @imagecreate($width, $height) or die('Cannot initialize GD!'); for( $i=0; $i<10; $i++ ) { imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), imagecolorallocate($image, mt_rand(150,255), mt_rand(150,255), mt_rand(150,255))); } for( $i=0, $x=0; $i<$length; $i++ ) { $actChar = substr($baseList, rand(0, strlen($baseList)-1), 1); $x += 10 + mt_rand(0,10); imagechar($image, mt_rand(3,5), $x, mt_rand(5,20), $actChar, imagecolorallocate($image, mt_rand(0,155), mt_rand(0,155), mt_rand(0,155))); $code .= strtolower($actChar); } header('Content-Type: image/jpeg'); imagejpeg($image); imagedestroy($image); $_SESSION['securityCode'] = $code; ?> Code (php): this is the code to print random image but it prints some non readable characters... could anyone please tell, what's wrong with this code...
You shouldn't... the fact that it's printing out "non readable characters" means that the imagejpeg() is outputting the image properly. Make sure there's NO whitespace above your opening PHP tag (<? or <?php). Meaning, make sure the opening PHP tag is on the VERY first line with nothing to the left of it. That's the only thing I can think of.. that the header() isn't able to set the content-type as an image.
On your next page, the page that they submit the data to through the POST form, make sure you put session_start(); at the VERY top again (remember, no blank lines above the opening PHP tag). Then, check if the code they entered is the same. Here's an example, assuming the text input box they type the code into is named 'code': if(empty($_POST['code']) || $_POST['code'] != $_SESSION['securityCode']) { //Oh no! They either left the field blank or they did not enter the correct code! } else { //They entered it correctly! } $_SESSION['securityCode'] = null; unset($_SESSION['securityCode']); //Always best to just kill off security session values like this so they can't just hit back and keep trying or something like that PHP:
How are you including it in form? You wouldn't do include() through PHP or anything.. just do <img src="location/to/script.php" /> and it'll work fine.