Jump to content

IMG validation problem


EagerWolf

Recommended Posts

Here is code:

 

imggen.php //Generates img

<?
//start a session 
session_start();
//this function is called recursivelly
  function random_string($len=5, $str='')
  {
for($i=1; $i<=$len; $i++)
        {
        //generates a random number that will be the ASCII code of the character.
//We only want numbers (ascii code from 48 to 57) and caps letters. 
 $ord=rand(48, 90);
 if((($ord >= 48) && ($ord <= 57)) || (($ord >= 65) && ($ord<= 90))) 
$str.=chr($ord);
 //If the number is not good we generate another one
else
	$str.=random_string(1);	                                       
}
return $str;
}
                                       
//create the random string using the upper function 
//(if you want more than 5 characters just modify the parameter)
$rand_str=random_string(5);
                                    
//We memorize the md5 sum of the string into a session variable
$_SESSION['image_value'] = md5($rand_str);
                              
//Get each letter in one valiable, we will format all letters different
$letter1=substr($rand_str,0,1);
$letter2=substr($rand_str,1,1);
$letter3=substr($rand_str,2,1);
$letter4=substr($rand_str,3,1);
$letter5=substr($rand_str,4,1);
                                       
//Creates an image from a png file. If you want to use gif or jpg images, 
//just use the coresponding functions: imagecreatefromjpeg and imagecreatefromgif.
$image=imagecreatefrompng("image.png");
                                       
//Get a random angle for each letter to be rotated with.
$maxkot = 15;
$minkot = -15;
$angle1 = rand($minkot, $maxkot);
$angle2 = rand($minkot, $maxkot);
$angle3 = rand($minkot, $maxkot);
$angle4 = rand($minkot, $maxkot);
$angle5 = rand($minkot, $maxkot);
                                 
//Get a random font. (In this examples, the fonts are located in "fonts" directory and named from 1.ttf to 10.ttf)
$font1 = "fonts/".rand(1, 5).".ttf";
$font2 = "fonts/".rand(1, 5).".ttf";
$font3 = "fonts/".rand(1, 5).".ttf";
$font4 = "fonts/".rand(1, 5).".ttf";
$font5 = "fonts/".rand(1, 5).".ttf";
                                       
//Define a table with colors (the values are the RGB components for each color).
$colors[0]=array(7,7,130);
$colors[1]=array(1,101,4);
$colors[2]=array(101,32,1);
$colors[3]=array(89,0,0);
$colors[4]=array(61,0,89);
$colors[5]=array(0,84,89);
                                       
//Get a random color for each letter.
$color1=rand(0, 5);
$color2=rand(0, 5);
$color3=rand(0, 5);
$color4=rand(0, 5);
$color5=rand(0, 5);
                                       
//Allocate colors for letters.
$textColor1 = imagecolorallocate ($image, $colors[$color1][0],$colors[$color1][1], $colors[$color1][2]);
$textColor2 = imagecolorallocate ($image, $colors[$color2][0],$colors[$color2][1], $colors[$color2][2]);
$textColor3 = imagecolorallocate ($image, $colors[$color3][0],$colors[$color3][1], $colors[$color3][2]);
$textColor4 = imagecolorallocate ($image, $colors[$color4][0],$colors[$color4][1], $colors[$color4][2]);
$textColor4 = imagecolorallocate ($image, $colors[$color5][0],$colors[$color5][1], $colors[$color5][2]);

//Write text to the image using TrueType fonts.
$size = 40;

imagettftext($image, $size, $angle1, 15, $size+rand(15,50), $textColor1, $font1, $letter1);
imagettftext($image, $size, $angle2, 50, $size+rand(15,50), $textColor2, $font2, $letter2);
imagettftext($image, $size, $angle3, 85, $size+rand(15,50), $textColor3, $font3, $letter3);
imagettftext($image, $size, $angle4, 120, $size+rand(15,50), $textColor4, $font4, $letter4);
imagettftext($image, $size, $angle5, 155, $size+rand(15,50), $textColor5, $font5, $letter5);

header('Content-type: image/jpeg');
//Output image to browser
imagejpeg($image);
//Destroys the image
imagedestroy($image); 
?>

 

show.php

<?
session_start();
?>
<img src="imggen.php">
<form method="POST" action="valid.php">
<input type="text" name="numcheck">
<input type="submit" name="Submit" value="Sprejmi">
<input type="hidden" name="hidnum" value="<? print $_SESSION['image_value']; ?>">
</form>

<?php print_r($_SESSION); ?>

 

I have experienced strange problem. When I open file show.php image is being showed, but nothing is being stored in $_SESSION on reload in $_SESSION is stored key from previous img...

 

Any idea?

 

10x for your help!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.