Advertisement
A1_Oxyde

index.php - login form

Jan 25th, 2019
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.41 KB | None | 0 0
  1. <?php
  2. session_start(); //starting the session to store information
  3. $log = ""; //output VAR for text
  4.  
  5.     $host = "ip:port"; //port might not be necessary
  6.     $user = "user";
  7.     $password = "password";
  8.     $db = "dbName";
  9.     $conn = mysqli_connect($host, $user, $password, $db); //creating a connection
  10.  
  11.         if (!$conn) {
  12.         die("Connection error: " . mysqli_connect_errno());
  13.             } else {
  14.             //do something else or delete the else statement if not needed
  15.                 }
  16.        
  17.     $log = "Insert Unsername and Password"; //default text on top of the login form
  18.  
  19.     if(isset($_POST['username'])){  //checking if the username is set and is not NULL.
  20.         $userUsername = $_POST['username']; //taking the user input for username from the loging form
  21.         $userPassword = $_POST['password']; //taking the user input for password from the loging form
  22.         $sql = "SELECT * FROM utenti WHERE username='".$userUsername."' AND password = '".$userPassword."'"; //VAR SQL query
  23.         $result = mysqli_query($conn, $sql); //result is equal to connection + the VAR query
  24.  
  25.         if(mysqli_num_rows($result)==1){ //checking the result of the query (in this case if there's 1 row of result)
  26.             $_SESSION['username'] = $userUsername; //setting the username input from the user inside the session to keep this info through the pages
  27.             header('location: main.php'); //redirect if result = 1 (redirct in PHP must be declaired BEFORE all the HTML code, otherwise it won't work)
  28.         } else {
  29.             $log = "Username or Password are wrong"; //changing the VAR log to display in the same page some text
  30.         }
  31.     } else {
  32.         //You can execute anything if the username is NULL, however if you set the username field as required in the HTML
  33.     }
  34. $conn->close() //close the connection
  35. ?>
  36. <html>
  37. <head>
  38. <link rel="stylesheet" type="text/css" href="indexstyle.css">
  39. </head>
  40. <body>
  41. <div id="container">
  42.     <div id="log">
  43.         <?php echo $log; // here you can print the log VAR inside a div that you can modify in CSS
  44.             ?>
  45.         </div>
  46.         <div class="form">
  47.             <form class="login-form" method="POST" action="#" >
  48.             <input type="text" id="user" name="username" placeholder="Username" autocomplete="off" required /> </br>
  49.             <input type="password" id="pass" name="password" placeholder="Password" autocomplete="off" required /> </br>
  50.             <input type="submit" class="button" name="button" value="LOGIN" />
  51.             </form>
  52.         </div>
  53.     </div>
  54. </body>
  55. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement