Advertisement
Guest User

Account script PHP

a guest
Jul 26th, 2018
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.04 KB | None | 0 0
  1. <?php
  2. session_start();
  3. require_once('bdd_connexion.php');
  4.  
  5. if(empty($_SESSION['id'])){
  6.     if(isset($_POST['formconnexion'])){
  7.         $emailconnect = htmlspecialchars($_POST['emailconnect']);
  8.         $passwordconnect = $_POST['passwordconnect'];
  9.  
  10.         if(!empty($emailconnect) AND !empty($passwordconnect)){
  11.             $q = $db->prepare('SELECT * FROM users WHERE email = ?'); // Préparation de la requete SQL
  12.             $q->execute([$emailconnect]); // Execution de la requete
  13.             $user = $q->fetch(PDO::FETCH_OBJ); // Va chercher l'id de l'utilisateur
  14.  
  15.             if($user && password_verify($passwordconnect, $user->password)) {
  16.                 // Création des sessions
  17.                 $_SESSION['id'] = $user->id;
  18.                 $_SESSION['username'] = $user->username;
  19.                 $_SESSION['email'] = $user->email;
  20.  
  21.                 // redirection vers le compte utilisateur
  22.                 header("Location: index.php");
  23.             }else{
  24.                 $alert = "Mauvais mail ou mot de passe";
  25.             }
  26.  
  27.         }else{
  28.             $alert = "Vous devez compléter tous les champs";
  29.         }
  30.     }
  31. }else{
  32.     if(isset($_GET['logout'])){
  33.         session_destroy();
  34.         header("Location: index.php");
  35.     }
  36. }
  37.  
  38. ?>
  39. <!DOCTYPE html>
  40. <html lang="fr-FR">
  41.     <head>
  42.         <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  43.         <title>Mon compte</title>
  44.         <!--[if lt IE 9]>
  45.             <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
  46.         <![endif]-->
  47.     </head>
  48.     <body>
  49.         <center>
  50.             <b style="color: red;">
  51.                 <?php if(isset($alert)){echo($alert.'<br>');} ?>
  52.             </b>
  53.         </center>
  54.  
  55.         <?php if(empty($_SESSION['id'])): ?>
  56.             <form method="POST">
  57.                 <center>
  58.                     <label>Adresse email :</label>
  59.                     <input type="email" name="emailconnect"><br><br>
  60.                     <label>Mot de passe :</label>
  61.                     <input type="password" name="passwordconnect"><br><br>
  62.  
  63.                     <button type="submit" name="formconnexion">Valider</button>
  64.                 </center>
  65.             </form>
  66.         <?php else: ?>
  67.             <a href="index.php?logout">Se déconnecter</a><br><br>
  68.             <b><?= $_SESSION['id'] ?></b><br>
  69.             <b><?= $_SESSION['username'] ?></b><br>
  70.             <b><?= $_SESSION['email'] ?></b><br>
  71.         <?php endif; ?>
  72.     </body>
  73. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement