Timmer
Mitglied
Hey, ich hab mich mal an eine Captcha gesetzt, die mit Session den Code an das Formular übergibt. Ich binde die Captcha (captcha.php) als Bild in das Formular (index.php), und möchte im Formular die Sessionvariable des Codes übernehmen... Geht aber nich...
Ich hab viele Tutorials gesehen, die das so gemacht haben, deswegen wundert mich das so, warum das nicht geht...
Hier ein bisschen Code:
index.php
captcha.php
Preview: Datei Upload
Jemand ne Idee, woran das liegen könnte?
Ich hab viele Tutorials gesehen, die das so gemacht haben, deswegen wundert mich das so, warum das nicht geht...
Hier ein bisschen Code:
index.php
PHP:
<?php
session_start();
function checkFile(){
$formats = array('jpg', 'gif', 'png');
$fileformat = array_pop(explode(".", strtolower($_FILES['file']['name'])));
$specialchars = array('ä'=>'ae', 'ö'=>'oe', 'ü'=>'ue', 'ß'=>'ss', ' '=>'_');
if($_FILES['file']['size'] < 1) {
return 'Datei zu klein.';
}
if($_FILES['file']['size'] > 1572864) {
return 'Maximale Dateigröße überschritten: 1,5 MB';
}
if (!in_array($fileformat, $formats)) {
return 'Ungültiges Format: jpg, png, gif';
}
$filename = strtr(strtolower($_FILES['file']['name']), $specialchars);
$filename = preg_replace("/[^a-z .]/i", "_", $filename);
$filename = trim(preg_replace("/[\_]/", " ", $filename));
$filename = preg_replace("/[\ ]/", "_", $filename);
$filename = ereg_replace('[_]+', '_', $filename);
$filedir = 'images/' . $filename;
$old = umask(0);
if (@move_uploaded_file($_FILES['file']['tmp_name'], $filedir)){
@chmod($filedir, 0755);
umask($old);
return 'Upload erfolgreich!<br /><a href="http://' . $_SERVER['SERVER_NAME'] . '/upload/' . $filedir . '">http://' . $_SERVER['SERVER_NAME'] . '/upload/' . $filedir . '</a>';
} else {
umask($old);
return 'Es ist ein Fehler aufgetreten.';
}
}
if($_SERVER['REQUEST_METHOD'] == 'POST'){
if(md5($_POST['captcha']) == $_SESSION['captcha']){
echo checkFile();
} else {
$out = <<< CAPTCHA
<html>
<head>
<title>Datei Upload</title>
</head>
<body>
<form name="upload" id="upload" method="post" action="" enctype="multipart/form-data">
<input type="file" name="file" id="file" /><br />
<input type="text" name="captcha" /><br />
<input type="submit" name="submitbutton" id="submitbutton" value="Datei hochladen">
</form>
</body>
</html>
CAPTCHA;
echo $out;
}
} else {
?>
<html>
<head>
<title>Datei Upload</title>
</head>
<body>
<form name="upload" id="upload" method="post" action="" enctype="multipart/form-data">
<input type="file" name="file" id="file" /><br />
<img src="captcha.php" alt="Sicherheitscode" title="Sicherheitscode" width="200" height="80" /><br />
<input type="text" name="captcha" value="" /><br />
<input type="submit" name="submitbutton" id="submitbutton" value="Datei hochladen">
</form>
</body>
</html>
<?php } ?>
captcha.php
PHP:
<?php
session_start();
unset($_SESSION['captcha']);
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header('Content-type: image/jpeg');
$chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
$part[1] = substr(str_shuffle($chars), 0, 1);
$part[2] = substr(str_shuffle($chars), 0, 1);
$part[3] = substr(str_shuffle($chars), 0, 1);
$part[4] = substr(str_shuffle($chars), 0, 1);
$part[5] = substr(str_shuffle($chars), 0, 1);
$part[6] = substr(str_shuffle($chars), 0, 1);
$code = $part1 . $part2 . $part3 . $part4 . $part5 . $part6;
$_SESSION['captcha'] = md5($code);
$fonts = array("fonts/agentorange.ttf", "fonts/neotrash.ttf", "fonts/buff.ttf", "fonts/ciaojeangrphx.ttf", "fonts/gm.ttf", "fonts/gordonheights.ttf");
$image = imagecreate(200, 80);
$background = imagecolorallocate($image, 255, 255, 255);
$count = mt_rand(10, 40);
for($i = 1; $i <= $count; $i++){
$action = mt_rand(1, 100);
$r = mt_rand(180, 255);
$g = mt_rand(180, 255);
$b = mt_rand(180, 255);
$x1 = mt_rand(0, 200);
$x2 = mt_rand(0, 200);
$y1 = mt_rand(0, 80);
$y2 = mt_rand(0, 80);
$thickness = mt_rand(1, 2);
$color = imagecolorallocate($image, $r, $g, $b);
imagesetthickness ($image, $thickness);
if($action%2 == 0){
imageline($image, $x1, $y1, $x2, $y2, $color);
} else {
imagedashedline($image, $x1, $y1, $x2, $y2, $color);
}
}
for($i = 1; $i <= 6; $i++){
$r = mt_rand(0, 150);
$g = mt_rand(0, 150);
$b = mt_rand(0, 150);
$color = imagecolorallocate($image, $r, $g, $b);
$size = mt_rand(20, 24);
$angle = mt_rand(-20, 20);
$start = 33*($i-1)+2;
$end = (33*$i)-24;
$x = mt_rand($start, $end);
$y = mt_rand(40, 50);
$font = mt_rand(0, 5);
imagettftext($image, $size, $angle, $x, $y, $color, $fonts[$font], $part[$i]);
}
imagejpeg($image);
imagedestroy($image);
?>
Preview: Datei Upload
Jemand ne Idee, woran das liegen könnte?