Bonjour,
J'ai un soucis. J'utilise PHP à l'occasion mais là c'est la première fois que j'utilise les sessions dans mes formulaires. J'ai crée une image captcha mais je n'arrive pas à récupérer le code captcha pour le vérifier...
Voici la structure:
dossier "pages"
fichier "contact.php"
<a href="index.php?pageid=contact" target="_top">:: Contact ::</a>
<p>
<style type="text/css">
form {
text-align:left;
margin-left: 40px;
}
</style>
<form action="contact_vf.php" method="post" enctype="multipart/form-data" name="form1">
<p>
<label for="name"><b>Name/Nickname*</b></label><br />
<input name="name" type="text" class="input" id="name" size="25" maxlength="20">
</p>
<p>
<label for="email"><b>Email*</b></label><br />
<input name="email" type="text" class="input" id="email" size="30" maxlength="90">
</p>
<p>
<?php echo "<input name=\"ip\" type=\"hidden\" id=\"ip\" value=\"".$_SERVER['REMOTE_ADDR']."\">"; ?>
</p>
<p>
<label for="website"><b>Website</b></label><br />
<input name="website" type="text" class="input" id="website" size="35" maxlength="200">
</p>
<p>
<label for="subject"><b>Subject*</b></label><br />
<input name="subject" type="text" class="input" id="subject" size="45" maxlength="20">
</p>
<p>
<label for="message"><b>Message*</b></label><br />
<textarea name="message" id="message" cols="100" rows="16" class="input"></textarea>
</p>
<p>
<label for="captcha"><b>Captcha*</b></label><br />
<table width="128px" border="0">
<tr>
<td><img src="captcha.php" width="54" height="16" name="captcha" id="captcha"/><?php echo $_SESSION['captcha_text']; ?></td>
<td><input name="captcha_text" type="captcha_text" class="input" id="captcha_text" size="10" maxlength="6" /></td>
</tr>
</table>
</p>
<p><br />
<input name="submit" type="submit" class="button" id="submit" value="Submit">
<p style="text-align: right;"><b>*Required fields.</b></p>
</p>
</form>
</p>
dossier principal:
"captcha.php"
<?php
session_start();
$max = 6;
$captcha_text = strtoupper(substr(md5(rand()),0,$max));
$_SESSION['captcha_text'] = $captcha_text;
$captcha = imagecreate(54,16);
$red = imagecolorallocate($captcha, 255, 0, 0);
$BGRand = imagecolorallocate($captcha, mt_rand(0,63), mt_rand(0,63), mt_rand(0,63));
$COLORand = imagecolorallocate($captcha, mt_rand(180,255), mt_rand(180,255), mt_rand(180,255));
$LINERand = imagecolorallocate($captcha, mt_rand(0,31), mt_rand(0,31), mt_rand(0,31));
for($i = 0; $i < 30; $i++)
{
ImageLine($captcha,mt_rand(0,54),mt_rand(0,16),mt_rand(0,54),mt_rand(0,16),$BGRand);
}
imagestring($captcha, 5, 0, 0, $captcha_text, $COLORand);
for($i = 0; $i < 3; $i++)
{
ImageLine($captcha,mt_rand(0,54),mt_rand(0,16),mt_rand(0,54),mt_rand(0,16),$LINERand);
}
header ("Content-type: image/png");
header("Cache-Control: no-store, no-cache, must-revalidate");
imagecolortransparent($captcha, $red);
imagepng($captcha);
?>
"contact_vf.php" (C'est juste pour tester que tout fonctionne bien, après je vais la modifier pour reçevoir le message par mail)
<?php
function CheckMail($mail)
{
if(preg_match('#^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,6}$#',$mail))
return true;
else
return false;
}
// if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['ip']) && isset($_POST['subject']) && isset($_POST['message']) && isset($_SESSION['captcha_text']) && isset($_POST['captcha_txt']) && $_POST['captcha_txt'] != "" && $_SESSION['captcha_text'] != "" && $_POST['name'] != "" && $_POST['email'] != "" && $_POST['ip'] != "" && $_POST['subject'] != "" && $_POST['message'] != "" && Checkmail($_POST['email']))
// {
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$ip = htmlspecialchars($_POST['ip']);
$subject = htmlspecialchars($_POST['subject']);
$message = htmlspecialchars($_POST['message']);
$captchaORIGINAL = htmlspecialchars($_SESSION['captcha_text']);
$captchaUSER = htmlspecialchars($_POST['captcha_text']);
// }
echo "NAME:".$name."<br>";
echo "EMAIL:".$email."<br>";
echo "IP:".$ip."<br>";
echo "SUBJECT:".$subject."<br>";
echo "MSG:".$message."<br>";
echo "Ocaptcha:".$captchaORIGINAL."<br>";
echo "Ucaptcha:".$captchaUSER."<br>";
?>
Merci
Afficher la suite