verification donnees formulaire php
Bonjour,
POur mon formulaire d'authentification, j'aimerai effectuer plusieurs contrôles avant la validation comme la vérification de l'existence ou non de l'email dans la base, la vérification de le concordance des deux mots de passe tapés, et puis le contrôle dans les champs, l'email, la longueur du nom, la longueur du prénom... Mais le problème est que je ne sais pas comment organiser tout ça, je n'ai qu'une partie des contrôles qui se font.
Est-ce que vous pouvez m'aider ou m'orienter ?
Merci d'avance
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
<?php
// on teste si le visiteur a soumis le formulaire
if (isset($_POST['inscription']) && $_POST['inscription'] == 'I create my account') {
// on teste l'existence de nos variables. On teste également si elles ne sont pas vides
if ((isset($_POST['email']) && !empty($_POST['email']))
&& (isset($_POST['password']) && !empty($_POST['password']))
&& (isset($_POST['titre']) && !empty($_POST['titre']))
&& (isset($_POST['prenom']) && !empty($_POST['prenom']) && strlen($_POST['prenom'])>4)
&& (isset($_POST['nom']) && !empty($_POST['nom']) && strlen($_POST['prenom'])>4)
&& (isset($_POST['pass_confirm']) && !empty($_POST['pass_confirm']))) {
// on teste les deux mots de passe
if(!empty($_POST) && strlen($_POST['prenom'])<4){
$error_prenom = ' Votre prenom doit comporter au minimun 4 caracteres !';
}
if(!empty($_POST) && strlen($_POST['nom'])<4){
$error_nom = ' Votre nom doit comporter au minimun 4 caracteres !';
}
if(!empty($_POST) && !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
$error_email = ' Votre Email n\'est pas valide !';
}
if(!empty($_POST) && strlen($_POST['password'])<6){
$error_password = ' Votre password doit comporter au minimun 6 caracteres !';
}
if ($_POST['password'] != $_POST['pass_confirm']) {
$erreur = 'Les 2 mots de passe sont différents.';
}
else {
$base = mysql_connect ('localhost', 'root', '');
mysql_select_db ('membres', $base);
// on recherche si ce login est déjà utilisé par un autre membre
$sql = 'SELECT count(*) FROM membre WHERE email="'.mysql_escape_string($_POST['email']).'"';
$req = mysql_query($sql) or die('Erreur SQL !<br />'.$sql.'<br />'.mysql_error());
$data = mysql_fetch_array($req);
// si cet email n'est pas déjà présent dans la table, on peut l'enregistrer $data = 0
if ($data[0] == 0) {
$sql = 'INSERT INTO membre VALUES("",
"'.mysql_escape_string($_POST['email']).'",
"'.mysql_escape_string(md5($_POST['password'])).'",
"'.mysql_escape_string($_POST['titre']).'",
"'.mysql_escape_string($_POST['prenom']).'",
"'.mysql_escape_string($_POST['nom']).'",
"")';
mysql_query($sql) or die('Erreur SQL !'.$sql.'<br />'.mysql_error());
// et on commence la session qui s'appelle email
session_start();
$_SESSION['email'] = $_POST['email'];
header('Location: inscription2.php');
exit();
}
else {
$erreur = 'Un membre possède déjà ce login.';
}
}
}
else {
$erreur = 'Au moins un des champs est vide.';
}
}
?>
<html>
<head>
<title>Inscription</title>
</head>
<body>
<div id="contour_formulaire">
<form action="inscription.php" method="post">
<fieldset style="border-color:#73aad2;border-size:2px;" width="625">
<legend class="titre_labelaut">Open your personal Interchim web account & get benefit of many advantages :</legend>
<br/>
<table width="628" border="0" cellpadding="0" cellspacing="0" >
<tr>
<td colspan="6" class="error"></td>
</tr>
<tr>
<td width="132"> <label for="titre" class="TexteLogin">Title : </label></td>
<td width="59" class="TexteCheck"><label><input name="titre" type="radio" value="Mr" checked="checked" />
Mr </label></td>
<td width="80" class="TexteCheck"><label><input name="titre" type="radio" value="Mme" />Mme </label></td>
<td colspan="2" class="TexteCheck"><label><input name="titre" type="radio" value="Mlle" />
Mlle</label></td>
<td width="280" class="error2"></td>
</tr>
<tr>
<td width="132"> <label for="prenom" class="TexteLogin">First Name : </label></td>
<td colspan="3">
<input type="text" name="prenom" class="loginRemplissage" value="<?php if (isset($_POST['prenom'])) echo htmlentities(trim($_POST['prenom'])); ?>">
<span class="error">*</span></td>
<td colspan="2" class="error2"><?php if(isset($error_prenom)){ echo $error_prenom; } ?>
</td>
</tr>
<tr>
<td colspan="6" class="error"></td>
</tr>
<tr>
<td width="132"> <label for="nom" class="TexteLogin">Family Name : </label></td>
<td colspan="3">
<input type="text" name="nom" class="loginRemplissage" value="<?php if (isset($_POST['nom'])) echo htmlentities(trim($_POST['nom'])); ?>">
<span class="error">*</span></td>
<td colspan="2" class="error2"><?php if(isset($error_nom)){ echo $error_nom; } ?>
</td>
</tr>
<tr>
<td colspan="6" class="error2"></td>
</tr>
<tr>
<td width="132"> <label for="email" class="TexteLogin">E-mail : </label></td>
<td colspan="3">
<input type="text" name="email" class="loginRemplissage" value="<?php if (isset($_POST['email'])) echo htmlentities(trim($_POST['email'])); ?>">
<span class="error">*</span></td>
<td colspan="2" class="error2"><?php if(isset($error_email)){ echo $error_email; } ?>
</td>
</tr>
<tr>
<td colspan="6" class="error"></td>
</tr>
<tr>
<td width="132"> <label for="password" class="TexteLogin">My password : </label></td>
<td colspan="3">
<input type="password" name="password" class="loginRemplissage" value="<?php if (isset($_POST['password'])) echo htmlentities(trim($_POST['password'])); ?>">
<span class="error">*</span></td>
<td colspan="2" class="error2"><?php if(isset($error_password)){ echo $error_password; } ?>
</td>
</tr>
<tr>
<td width="132"> <label for="password" class="TexteLogin">Confirm password : </label></td>
<td colspan="3">
<input type="password" name="pass_confirm" class="loginRemplissage" value="<?php if (isset($_POST['pass_confirm'])) echo htmlentities(trim($_POST['pass_confirm'])); ?>">
<span class="error">*</span></td>
<td colspan="2" class="error2"><?php if(isset($error_password)){ echo $error_password; } ?>
</td>
</tr>
<tr>
<td> </td>
<td colspan="5"><?php if (isset($erreur)) echo '<br />',$erreur; ?></td>
</tr>
<tr>
<td height="43"></td>
<td colspan="5"> <input type="submit" name="inscription" class="AlignementCreateAccount" value="I create my account"/></td>
</tr>
<tr>
<td height="19"> </td>
<td colspan="5"><span class="error">*</span> <span class="error">Must be mandatory filled </span></td>
</tr>
</table>
</fieldset>
</form><br/>
</div>
</body>
</html> |
correction de l'exemple (oups)
de rien, et si ton code fonctionne maintenant, tant mieux, je ne suis pas là pour gagner un concours de toute façon...
pour le fun (et accessoirement pour ta connaissance) voici ton code modifié :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
| <?php
session_start(); // toujours en premier dans la page !!!
// on initialise le tableau des erreurs
$erreurs= array(
"titre" => array("msg" => ""),
"nom" => array("msg" => ""),
"prenom" => array("msg" => ""),
"email" => array("msg" => ""),
"password" => array("msg" => ""),
"pass_confirm" => array("msg" => "")
);
$error_exist= false;
$erreur= "";
$titre= (isset($_POST['titre'])) ?($_POST['titre']) :("");
$nom= (isset($_POST['nom'])) ?($_POST['nom']) :("");
$prenom= (isset($_POST['prenom'])) ?($_POST['prenom']) :("");
$email= (isset($_POST['email'])) ?($_POST['email']) :("");
$password= (isset($_POST['password'])) ?($_POST['password']) :("");
$pass_confirm= (isset($_POST['pass_confirm'])) ?($_POST['pass_confirm']) :("");
// on teste si le visiteur a soumis le formulaire
if(isset($_POST['inscription'])) {
if(empty($titre) || empty($nom) || empty($prenom) || empty($email) || empty($password) || empty($pass_confirm)) {
$erreur = "Tous les champs sont requis !";
$error_exist= true;
}
// test sur nom
$nom_ko= (strlen($nom) < 4);
if($nom_ko) {
$erreurs["nom"]["msg"].="Votre nom doit comporter au minimun 4 caracteres !<br/>";
$error_exist= true;
}
// test sur prenom
$prenom_ko= (strlen($prenom) < 4);
if($prenom_ko) {
$erreurs["prenom"]["msg"].="Votre prénom doit comporter au minimun 4 caracteres !<br/>";
$error_exist= true;
}
// test sur mot de passe
$pass_ko=(strlen($password) < 6);
if($pass_ko) {
$erreurs["password"]["msg"].="Votre password doit comporter au minimun 6 caracteres !<br/>";
$error_exist= true;
}
if($password != $pass_confirm) {
$erreurs["password"]["msg"].= "Les 2 mots de passe sont différents !<br/>";
$error_exist= true;
}
// test sur email
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$erreurs["email"]["msg"].= "Votre Email n\'est pas valide !<br/>";
}
if(!$error_exist) {
$base = mysql_connect ('localhost', 'root', '');
mysql_select_db ('membres', $base);
// on recherche si ce login est déjà utilisé par un autre membre
$sql = 'SELECT count(*) FROM membre WHERE email="'.mysql_escape_string($email).'"';
$req = mysql_query($sql) or die('Erreur SQL !<br />'.$sql.'<br />'.mysql_error());
$data = mysql_fetch_array($req);
// si cet email n'est pas déjà présent dans la table, on peut l'enregistrer $data = 0
if ($data[0] == 0) {
$sql = "INSERT INTO membre VALUES('',
'".mysql_escape_string($email)."',
'".mysql_escape_string(md5($password))."',
'".mysql_escape_string($titre)."',
'".mysql_escape_string($prenom)."',
'".mysql_escape_string($nom)."',
'')";
mysql_query($sql) or die('Erreur SQL !'.$sql.'<br />'.mysql_error());
// et on commence la session qui s'appelle email
$_SESSION['email'] = $email;
header('Location: inscription2.php');
exit();
}
else {
$erreur = 'Un membre possède déjà ce login.';
}
}
}
?>
<html>
<head>
<title>Inscription</title>
</head>
<body>
<div id="contour_formulaire">
<form action="inscription.php" method="post">
<fieldset style="border-color:#73aad2;border-size:2px;" width="625">
<legend class="titre_labelaut">Open your personal Interchim web account & get benefit of many advantages :</legend>
<br/>
<table width="628" border="0" cellpadding="0" cellspacing="0" >
<tr>
<td colspan="6" class="error"></td>
</tr>
<tr>
<td width="132"> <label for="titre" class="TexteLogin">Title : </label></td>
<td width="59" class="TexteCheck"><label><input name="titre" type="radio" value="Mr" checked="checked" />
Mr </label></td>
<td width="80" class="TexteCheck"><label><input name="titre" type="radio" value="Mme" />Mme </label></td>
<td colspan="2" class="TexteCheck"><label><input name="titre" type="radio" value="Mlle" />
Mlle</label></td>
<td width="280" class="error2"></td>
</tr>
<tr>
<td width="132"> <label for="prenom" class="TexteLogin">First Name : </label></td>
<td colspan="3">
<input type="text" name="prenom" class="loginRemplissage" value="<?php echo $prenom; ?>">
<span class="error">*</span></td>
<td colspan="2" class="error2"> <?php echo $erreurs["prenom"]["msg"]; ?>
</td>
</tr>
<tr>
<td colspan="6" class="error"></td>
</tr>
<tr>
<td width="132"> <label for="nom" class="TexteLogin">Family Name : </label></td>
<td colspan="3">
<input type="text" name="nom" class="loginRemplissage" value="<?php echo $nom; ?>">
<span class="error">*</span></td>
<td colspan="2" class="error2"> <?php echo $erreurs["nom"]["msg"]; ?>
</td>
</tr>
<tr>
<td colspan="6" class="error2"></td>
</tr>
<tr>
<td width="132"> <label for="email" class="TexteLogin">E-mail : </label></td>
<td colspan="3">
<input type="text" name="email" class="loginRemplissage" value="<?php echo $email; ?>">
<span class="error">*</span></td>
<td colspan="2" class="error2"> <?php echo $erreurs["email"]["msg"]; ?>
</td>
</tr>
<tr>
<td colspan="6" class="error"></td>
</tr>
<tr>
<td width="132"> <label for="password" class="TexteLogin">My password : </label></td>
<td colspan="3">
<input type="password" name="password" class="loginRemplissage" value="">
<span class="error">*</span></td>
<td colspan="2" class="error2"><?php if(isset($error_password)){ echo $error_password; } ?>
</td>
</tr>
<tr>
<td width="132"> <label for="password" class="TexteLogin">Confirm password : </label></td>
<td colspan="3">
<input type="password" name="pass_confirm" class="loginRemplissage" value="">
<span class="error">*</span></td>
<td colspan="2" class="error2"><?php if(isset($error_password)){ echo $error_password; } ?>
</td>
</tr>
<tr>
<td> </td>
<td colspan="5"><?php if (isset($erreur)) echo '<br />',$erreur; ?></td>
</tr>
<tr>
<td height="43"></td>
<td colspan="5"> <input type="submit" name="inscription" class="AlignementCreateAccount" value="I create my account"/></td>
</tr>
<tr>
<td height="19"> </td>
<td colspan="5"><span class="error">*</span> <span class="error">Must be mandatory filled </span></td>
</tr>
</table>
</fieldset>
</form><br/>
</div>
</body>
</html> |
voila, bonne analyse...
;)