Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // This is mandatory to use sessions !
- session_start();
- // Here we check if user is already authenticated, if so we redirect to index.php
- if (isset($_SESSION["username"]) and isset($_SESSION["auth_ok"])){
- // this function print message in the logs of the web server
- error_log("user authenticated");
- // This is the way we do a redirect with PHP, by setting a HTTP header and exiting
- header("Location: template.php");
- die();
- }
- /** The name of the database */
- define('DB_NAME', 'cv');
- /** MySQL database username */
- define('DB_USER', 'root');
- /** MySQL database password */
- define('DB_PASSWORD', '');
- /** MySQL hostname */
- define('DB_HOST', 'localhost');
- $conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
- if ($conn->connect_error) {
- error_log("Connection failed: " . $conn->connect_error);
- }
- $conn->set_charset('utf8');
- // Perform SQL query
- if (isset($_POST['u'])) {
- $query = $conn->query('SELECT * FROM `user` WHERE user.username= "' . $_POST['u'] . '"');
- $tata = $query->fetch_array();
- // Check if there was an error
- if ($tata['password'] == sha1($_POST['p'])) {
- var_dump($tata);
- $_SESSION['username'] = $_POST['u'];
- $_SESSION['auth_ok'] = 1;
- }
- }
- ?>
- <?php
- // We check if the form was submitted
- if (isset($_POST["u"]) && isset($_POST["p"])) {
- error_log("try to authenticate");
- // We extract the form data and store them in variables
- $email = $_POST["u"];
- $pass = $_POST["p"];
- // Hum, something is wrong here right ?
- if (True) {
- error_log("auth OK");
- $_SESSION["u"] = $_POST["u"];
- $_SESSION["auth_ok"] = 1;
- header("Location: login.php");
- die();
- } else {
- error_log("auth not OK");
- $err = "erreur d'authentification";
- $_SESSION["u"] = '';
- $_SESSION["auth_ok"] = 0;
- }
- }
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <link href="css/login.css" rel="stylesheet">
- <head>
- <meta charset="UTF-8">
- <title>Login</title>
- <!DOCTYPE html>
- <html>
- <div class="login">
- <h1>Se connecter</h1>
- <form method="post">
- <input type="text" name="u" placeholder="Username" required="required" />
- <input type="password" name="p" placeholder="Password" required="required" />
- <button type="submit" class="btn btn-primary btn-block btn-large">Laissez moi entrer.</button>
- </form>
- </div>
- </body>
- </html>
- </head>
- <body>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement