code.php
<?php
session_start();
$rand = rand(100000,999999);
$_SESSION['code']=$rand;
$im = @imagecreate(45, 15);
$background_color = imagecolorallocate($im, 220, 220, 220);
$col = ImageColorAllocate($im, rand(0, 255), rand(0, 255), rand(0, 255) );
ImageString($im, 3, 2, 1, $rand, $col);
header('Content-Type: image/png');
header('Cache-control: no-cache, no-store');
ImagePNG($im);
?>
reg.php
<?php
if($_SESSION['code']!=$_POST['code'] or empty($_POST['code'])) $err = 'Код с картинки введен не верно!';
<center><img style="width:20%;" src="code.php?'.session_name().'='.session_id().'" alt="img"></center>
<input style="text-align:center;" placeholder="Код" type="text" name="code" size="6"><br/>
?>