Browse Source

ajout pp3

Clément Krebs 6 years ago
parent
commit
094f93bfa3
8 changed files with 171 additions and 5 deletions
  1. 9 5
      Tp2/signin.php
  2. 44 0
      Tp3/adduser.php
  3. 37 0
      Tp3/authenticate.php
  4. 4 0
      Tp3/bdd.php
  5. 33 0
      Tp3/signin.php
  6. 4 0
      Tp3/signout.php
  7. 30 0
      Tp3/signup.php
  8. 10 0
      Tp3/welcome.php

+ 9 - 5
Tp2/signin.php

@@ -11,11 +11,15 @@
     <p>Salut</p>
     
     <form action="authenticate.php" method="post">
-     login :<br>
-    <input type="text" name="login"><br>
-     password :<br>
-    <input type="password" name="passwd"><br>
-    <input type="submit" value="Log in">
+        <div class="form-group">
+            login :<br>
+            <input type="text" class="form-control" name="login"><br>
+        </div>
+        <div class="form-group">
+            password :<br>
+            <input type="password" class="form-control" name="passwd"><br>
+        </div>
+        <input type="submit" class="btn btn-primary" value="Log in">
     </form>
     <?php 
         session_start();

+ 44 - 0
Tp3/adduser.php

@@ -0,0 +1,44 @@
+<?php
+    include_once('bdd.php');
+
+    if ($_SERVER['REQUEST_METHOD'] != 'POST') {
+        header('Location: signup.php');
+        exit();
+    }
+    try {
+        $pdo = new PDO(SQL_DSN, SQL_USERNAME, SQL_PASSWORD);
+
+    }
+    catch(PDOException $e) {
+        exit();
+    }
+    
+    if (!isset($_POST['login'], $_POST['passwd'], $_POST['passwdconf'])) {
+        header('Location: signup.php');
+        exit();
+    }
+    if (empty($_POST['login'] && empty($_POST['passwd'] && empty($_POST['passwdconf']) {
+        header('Location: signup.php');
+        exit();
+    }
+    
+    $login = htmlspecialchars($_POST['login']);
+    $passwd = htmlspecialchars($_POST['passwd']);
+    $passwdconf = htmlspecialchars($_POST['passwdconf']);
+    
+    if ($passwd != $passwdconf) {
+        header('Location: signup.php');
+        exit();
+    }
+    
+    $req = $pdo->prepare('INSERT INTO Users (login, passwd) VALUES (:login, :passwd)');
+    $req->bindValue(':login', $login, PDO::PARAM_STR);
+    $req->bindValue(':passwd', $passwd, PDO::PARAM_STR);
+    if ($req->execute()) {
+        header('Location: signin.php');
+    } else {
+        header('Location: signup.php');
+    }
+    
+    
+        

+ 37 - 0
Tp3/authenticate.php

@@ -0,0 +1,37 @@
+<?php
+    session_start();
+    include_once('bdd.php');
+
+    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
+        $login = htmlspecialchars($_POST['login']);
+        $passwd = htmlspecialchars($_POST['passwd']);
+        try {
+            $pdo = new PDO(SQL_DSN, SQL_USERNAME, SQL_PASSWORD);
+
+        }
+        catch(PDOException $e) {
+            $_SESSION['message'] = $e->getMessage();
+            header('Location: signin.php');
+            exit();
+        }
+        
+        $req = $pdo->prepare('SELECT passwd from Users WHERE login = :login');
+        $req->bindValue(':login', $login, PDO::PARAM_STR);
+        $req->execute();
+        $count = $req->rowCount();
+        if ($count == 0) {
+            header('Location: signin.php');
+            exit();
+        }
+        foreach ($req as $row) {
+            if ($row['passwd'] != $login) {
+                header('Location: signin.php');
+                $_SESSION['message'] = 'Bad password';
+                exit();
+            }
+        }
+        $_SESSION['login'] = $login;
+        header('Location: welcome.php');
+    } else {
+        header('Location: signin.php');
+    }

+ 4 - 0
Tp3/bdd.php

@@ -0,0 +1,4 @@
+<?php
+const SQL_DSN = 'mysql:host=mysql.iutrs.unistra.fr;dbname=w31clem';
+const SQL_USERNAME = 'clement.krebs';
+const SQL_PASSWORD = '3pfc1i7n';

+ 33 - 0
Tp3/signin.php

@@ -0,0 +1,33 @@
+<!doctype html>
+<html lang="fr">
+<head>
+  <meta charset="utf-8">
+  <title>Tp 1</title>
+  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
+  <link rel="stylesheet" type="text/css" href="style.css">
+</head>
+<body>
+
+    <p>Salut</p>
+    
+    <form action="authenticate.php" method="post">
+        <div class="form-group">
+            login :<br>
+            <input type="text" class="form-control" name="login"><br>
+        </div>
+        <div class="form-group">
+            password :<br>
+            <input type="password" class="form-control" name="passwd"><br>
+        </div>
+        <input type="submit" class="btn btn-primary" value="Log in">
+    </form>
+    <?php 
+        session_start();
+        if (isset($_SESSION['message'])) {
+            echo('<section> error : ' . $_SESSION['message'] . '</section>');
+            unset($_SESSION['message']);
+        }
+    ?>
+
+</body>
+</html>

+ 4 - 0
Tp3/signout.php

@@ -0,0 +1,4 @@
+<?php
+    session_start();
+    session_destroy();
+    header('Location: signin.php');

+ 30 - 0
Tp3/signup.php

@@ -0,0 +1,30 @@
+<!doctype html>
+<html lang="fr">
+<head>
+  <meta charset="utf-8">
+  <title>Tp 1</title>
+  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
+  <link rel="stylesheet" type="text/css" href="style.css">
+</head>
+<body>
+
+    <p>Salut inscription</p>
+    
+    <form action="adduser.php" method="post">
+        <div class="form-group">
+            login :<br>
+            <input type="text" class="form-control" name="login"><br>
+        </div>
+        <div class="form-group">
+            password :<br>
+            <input type="password" class="form-control" name="passwd"><br>
+        </div>
+        <div class="form-group">
+            password confirmation :<br>
+            <input type="password" class="form-control" name="passwdconf"><br>
+        </div>
+        <input type="submit" class="btn btn-primary" value="Log in">
+    </form>
+
+</body>
+</html>

+ 10 - 0
Tp3/welcome.php

@@ -0,0 +1,10 @@
+<?php
+    session_start();
+    if (isset($_SESSION['login'])) {
+        echo('Salut ' . $_SESSION['login']);
+        echo('<form action="signout.php" method="post">');
+        echo('<input type="submit" value="Sign out">');
+        echo('</form>');
+    } else {
+        header('Location: signin.php');
+    }