PHP ver gevorderde |
|
random.php
<?php
session_start();
$len = 6; // length of string
$lchar = 0;
$char = 0;
/**************************************************
$random_text will hold the secret and random text!
**************************************************/
// create 'random' text
for($i = 0; $i < $len; $i++) {
while($char == $lchar) {
$char = rand(48, 109);
if($char > 57) $char += 7;
if($char > 90) $char += 6;
}
$random_text .= chr($char);
$lchar = $char;
}
$random_text = str_replace ("0", "K", $random_text);
$random_text = str_replace ("O", "K", $random_text);
$_SESSION['code'] = $random_text;
$width = 100; // width of image
$height = 30; // height of image
$fontsize = rand(1,20); // fontsize
//-- Random string voor images code
$fontwidth = ImageFontWidth($fontsize) * strlen($random_text);
$fontheight = ImageFontHeight($fontsize);
// create handle for image
$im = @imagecreate($width,$height);
$een = rand(155,255);
$twee = rand(155,255);
$drie = rand(155,255);
$vier = rand(0,100);
$vijf = rand(0,100);
$zes = rand(0,100);
// white background
$background_colour = imagecolorallocate($im, $een, $twee, $drie);
// give the 'random' text a nice colour
$text_colour = imagecolorallocate($im, $vier, $vijf, $zes);
// give the border a colour too ;)
imagerectangle($im, 0, 0, $width-1, $height-1, $text_colour);
imagestring($im, $fontsize, rand(3, $width-$fontwidth-3), rand(2, $height-$fontheight-3), $random_text, $text_colour);
// lets output!
header("Content-type: image/png");
imagepng($im,'',80);
imagedestroy($im);
//sessie zetten
$_SESSION['code'] = $random_text;
?>
<?php $len = 6; // length of string $lchar = 0; $char = 0; /************************************************** $random_text will hold the secret and random text! **************************************************/ // create 'random' text for($i = 0; $i < $len; $i++) { while($char == $lchar) { if($char > 57) $char += 7; if($char > 90) $char += 6; } $random_text .= chr($char); $lchar = $char; } $_SESSION['code'] = $random_text; $width = 100; // width of image $height = 30; // height of image $fontsize = rand(1,20); // fontsize //-- Random string voor images code $fontwidth = ImageFontWidth ($fontsize) * strlen($random_text); $fontheight = ImageFontHeight($fontsize); // create handle for image $im = @imagecreate($width,$height); // white background $background_colour = imagecolorallocate($im, $een, $twee, $drie); // give the 'random' text a nice colour $text_colour = imagecolorallocate($im, $vier, $vijf, $zes); // give the border a colour too ;) imagerectangle($im, 0, 0, $width-1, $height-1, $text_colour); imagestring ($im, $fontsize, rand(3, $width-$fontwidth-3), rand(2, $height-$fontheight-3), $random_text, $text_colour);// lets output! header("Content-type: image/png"); imagepng($im,'',80); imagedestroy($im); //sessie zetten $_SESSION['code'] = $random_text; ?>
Gebruik van plaatje :
<?php
<img src='random.php'> Typ de code over : <input name='code' type='text' value='' size='11' maxlength='10'>
?>
<?php <img src='random.php'> Typ de code over : <input name='code' type='text' value='' size='11' maxlength='10'> ?>
controle op de code :
<?php
if($_POST['code'] != $_SESSION['code']) {
echo "Je moet wel de goede activatiecode invullen! Let op hoofdletters en cijfers. <a href='javascript:history.go(-1)'>Ga terug</a>.";
} else {
//oke netjes
}
?>
<?php if($_POST['code'] != $_SESSION['code']) { echo "Je moet wel de goede activatiecode invullen! Let op hoofdletters en cijfers. <a href='javascript:history.go(-1)'>Ga terug</a>."; } else { //oke netjes } ?>
|