Skip to content

Instantly share code, notes, and snippets.

@olivierkto
Created March 2, 2017 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olivierkto/271b2e684fe714bacd3fa5d4bd7c5582 to your computer and use it in GitHub Desktop.
Save olivierkto/271b2e684fe714bacd3fa5d4bd7c5582 to your computer and use it in GitHub Desktop.
Inscription/Connexion PHP (MySQLi)
<?php
if(!empty($_POST["username"]) == true AND !empty($_POST["password"]) == true) {
$username = mb_strtolower(htmlentities($mysqli->real_escape_string($_POST["username"])), 'UTF-8');
$password = htmlentities($mysqli->real_escape_string($_POST["password"]));
$sql = "SELECT * FROM table WHERE username = '".$username."' AND password = SHA1('" . $password . "')";
$sql = $mysqli->query($sql);
$sql = $sql->num_rows;
if($sql == 1)
{
$alert_code = '<center><div class="alert alert-dismissable alert-success" style="width: 400px;"><strong>Vous êtes bien connecté au site.</strong> Accéder à l\'<b><a href="dashPath">espace membres</a></b>.</div></center>';
$dontview = false;
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
}
else {
$alert_code = '<center><div class="alert alert-dismissable alert-danger" style="width: 400px;"><strong>Mauvais identifiant/mot de passe.</strong></b></a></div></center>';
$dontview = true;
}
}
else {
$alert_code = '<center><div class="alert alert-dismissable alert-danger" style="width: 400px;"><strong>Mauvais identifiant/mot de passe.</strong></b></a></div></center>';
$dontview = true;
}
// Appel de la vue correspondante
$viewToInclude = 'loginView';
?>
<form action="loginController" method="POST">
<center>
<?php echo $alert_code; ?>
<div class="panel panel-primary" style="width:400px;text-align:left;">
<div class="panel-heading">
<h3 class="panel-title">Connexion</h3>
</div>
<div class="panel-body">
<?php if($dontview != false) { ?>
<div class="form-group">
<label class="control-label" for="username">Nom d'utilisateur</label>
<input class="form-control" id="username" name="username" type="text" <?php if(!empty($_POST["username"])) { echo 'value="' . $_POST["username"] . '"'; } ?>>
</div>
<div class="form-group">
<label class="control-label" for="username">Mot de passe</label>
<input class="form-control" id="username" name="password" type="password">
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary btn-sm btn-block"></input>
</div>
<?php } else { ?>
<label>Récapitulatif</label>
<table class="table table-striped table-hover ">
<thead>
<tr>
<th>#</th>
<th>Champ</th>
<th>Entrée</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Nom d'utilisateur</td>
<td><?php echo $username ?></td>
</tr>
<tr>
<td>2</td>
<td>Mot de passe</td>
<td>********</td>
</tr>
</table>
<?php } ?>
</div>
</div>
</form>
</center>
</div>
</div>
<div id="footer">
<?php
// Script de processing de l'inscription
if (!empty($_POST["username"]) == true AND !empty($_POST["email"]) == true AND !empty($_POST["password"]) == true AND !empty($_POST["confirmPassword"]) == true AND $_POST["password"] == $_POST["confirmPassword"]) {
$username = mb_strtolower(htmlentities($mysqli->real_escape_string($_POST["username"])), 'UTF-8');
$email = htmlentities($mysqli->real_escape_string($_POST["email"]));
$password = htmlentities($mysqli->real_escape_string($_POST["password"]));
$ipaddress = htmlentities($mysqli->real_escape_string($_SERVER['REMOTE_ADDR']));
$sql = "SELECT username FROM table WHERE username = '" . $username . "' ";
$sql = $mysqli->query($sql);
$rowsnumber = $sql->num_rows;
if ($rowsnumber == 0) {
$alert_code = '<center><div class="alert alert-dismissable alert-success" style="width: 400px;"><strong>Votre inscription a été validée.</strong></b> Vous pouvez maintenant vous <b><a href="loginPage">connecter</a></b>.</div></center>';
$dontview = false;
$sql = "INSERT INTO table (username, email, password, ip) VALUES ('" . $username . "','" . $email . "', SHA1('" . $password . "'),'" . $ipaddress . "')";
$sql = $mysqli->query($sql);
}
else {
$alert_code = '<center><div class="alert alert-dismissable alert-warning" style="width: 400px;"><strong>Vous êtes déjà inscrit sur ce site.</strong></b> Vous pouvez vous <b><a href="loginPage">connecter</a></b></a></div></center>';
$dontview = true;
}
}
else {
$alert_code = '<center><div class="alert alert-dismissable alert-danger" style="width: 400px;"><strong>Tous les champs requis n\'ont pas été remplis correctement.</strong></b></a></div></center>';
$dontview = true;
}
// Appel de la vue correspondante
$viewToInclude = 'viewPath';
?>
<form action="registerController" method="POST">
<center>
<div class="panel panel-primary" style="width:400px;text-align:left;">
<div class="panel-heading">
<h3 class="panel-title">
Inscription
</h3>
</div>
<div class="panel-body">
<?php if($dontview != false) { ?>
<div class="form-group">
<label class="control-label" for="username">
Pseudonyme
</label>
<input class="form-control" id="username" name="username" type="text">
</div>
<div class="form-group">
<label class="control-label" for="username">
Adresse email
</label>
<input class="form-control" id="username" name="email" type="email">
</div>
<div class="form-group">
<label class="control-label" for="username">
Mot de passe
</label>
<input class="form-control" id="username" name="password" type="password">
</div>
<div class="form-group">
<label class="control-label" for="username">
Répéter le mot de passe
</label>
<input class="form-control" id="username" name="confirmPassword" type="password">
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary btn-sm btn-block">
</input>
</div>
<?php } else { ?>
<label>
Récapitulatif
</label>
<table class="table table-striped table-hover ">
<thead>
<tr>
<th>
#
</th>
<th>
Champ
</th>
<th>
Entrée
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
1
</td>
<td>
Nom d'utilisateur
</td>
<td>
<?php echo $username ?>
</td>
</tr>
<tr>
<td>
2
</td>
<td>
Mot de passe
</td>
<td>
<?php echo $password ?>
</td>
</tr>
<tr>
<td>
3
</td>
<td>
Adresse email
</td>
<td>
<?php echo $email ?>
</td>
</tr>
</tbody>
</table>
<?php } ?>
</div>
</div>
</form>
</center>
</div>
</div>
<div id="footer">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment