Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <title>Test Login Page</title>
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <link rel="stylesheet" href="css/register.css" />
- <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
- <link rel="stylesheet" href="css/index.css" />
- <link rel="stylesheet" href="css/styles.css" />
- <link href='https://fonts.googleapis.com/css?family=Raleway:400,500,300,600' rel='stylesheet' type='text/css'>
- </head>
- <?php
- //These lines create all the required variables and sets their default values
- $servername = "127.0.0.1";
- $username = "root";
- $password = "";
- $dbname = "accounts";
- $error = 0;
- $emailErr = $passErr = $success = $email = $pass = $user = "";
- $row = "";
- //This line creates the variable that will be used to connect to the database
- $conn = new mysqli($servername, $username, $password, $dbname);
- //Checks the connection to the database
- if ($conn->connect_error) {
- //If the connection fails, an error message is produced describing the error
- die("Connection failed: " . $conn->connect_error . "/nPlease try again. If it still doesn't work, copy the error message and send it to cjbrennan2701@gmail.com");
- }
- include "navBar.php";
- //When the user clicks submit on the form below, the following indented code runs
- if ($_SERVER["REQUEST_METHOD"] === "POST") {
- //If the user hasn't entered anything in the email box, an error message is produced
- if (empty($_POST["email"])){
- $emailErr = "Please enter an email address";
- $error = 1;
- } else {
- //Otherwise, their input is stored as $email
- $email = $_POST["email"];
- }
- //If the user hasn't entered anything in the password box, an error message is produced
- if (empty($_POST["password"])){
- $passErr = "Please enter a password";
- $error = 1;
- } else {
- //Otherwise, their input is stored as $pass
- $pass = $_POST["password"];
- }
- //If no initial errors are found, the user's input is then used to search the database
- if($error != 1){
- //The database is then searched for matching email and password combinations
- $sql = "SELECT * FROM testTable";
- //The number of results from the query is stored as $result
- $result = mysqli_query($conn, $sql);
- //If the query was successful, there will only be one result
- if (mysqli_num_rows($result) > 0) {
- //The values associated with the record found is stored in the array $row
- while($row = mysqli_fetch_assoc($result)) {
- //A session is started in the navigation bar using session_start() to allow the use of $_SESSION
- //The global variable $_SESSION["id"] has the user's id stored
- $_SESSION["id"] = $row["id"];
- //The user's first name is stored in the global variable $_SESSION["firstN"]
- $_SESSION["firstN"] = $row["firstN"];
- $_SESSION["surn"] = $row["surn"];
- $_SESSION["email"] = $row["email"];
- if (is_null($row["phone"])){
- $_SESSION["phone"] = "Not entered";
- } else {
- $_SESSION["phone"] = $row["phone"];
- }
- }
- //The user is presented with a greeting message using the assigned $_SESSION["firstN"]
- $success = "Welcome, " . $_SESSION["firstN"];
- //If a match isn't found, an error message is displayed to the user
- } else {
- $success = "Email and password combination not found, please try again";
- }
- }
- }
- ?>
- <body>
- <div class="container-fluid">
- <div class="row">
- <div class="col-md-3"></div>
- <div class="col-xs-12 col-md-6" style="text-align:center">
- <!-- This line creates a form which runs the PHP code above when the user clicks submit -->
- <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'])?>" method="post" name="login">
- <p>Please enter your username and password</p>
- <!-- These lines create a box allowing the user to input their email address and space for any errors being displayed -->
- E-mail:
- <input type="text" name="email" id="email">
- <span class="error">* <?php echo $emailErr;?></span> <br>
- <br />
- <!-- These lines create a box for the user to input their password as well as space to display any required errors -->
- Password:
- <input type="text" name="password" id="password">
- <span class="error">* <?php echo $passErr;?></span> <br>
- <br />
- <!-- This line creates a button which, when clicked runs the PHP code above -->
- <button type="submit" name="submit">Log in</button>
- </form>
- <br>
- <!-- This line produces a message depending on whether the login attempt has succeeded or failed -->
- <p><?php echo $success;?></p>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement