Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This is for registration.php
- <?php include('server.php'); ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Registration</title>
- <link rel="stylesheet" type="text/css" href="./css/styles.css">
- </head>
- <body>
- <center>
- <div id="signup_form_center" style="margin: 70px 0 50px;">
- <img src="./logo/wecon_logo_single.png" height="50px" viewBox="-3 -3 82 82" width="50">
- <h1 style="text-align: center; font-weight: bold; ">Sign Up</h1>
- <form action="registration.php" method="post">
- <!-- display validation errors here -->
- <?php include('errors.php') ?>
- <input type="text" name="username" value="<?php echo $username; ?>"placeholder="Username">
- <br>
- <input type="email" name="email" value="<?php echo $email; ?>"placeholder="Email Address">
- <br>
- <input type="password" name="password_1" required="required"placeholder="Password">
- <br>
- <input type="password" name="password_2" required="required"placeholder="Confirm Password">
- <br>
- <center>
- <input type="submit" name="reg_user" class="button">
- </center>
- </form>
- <i>Already a user <a href="login.php" class="signup_a">Login</a></i>
- </div>
- </center>
- </body>
- </html>
- //registration.php end
- //This is for login.php
- <?php include('server.php') ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Log in</title>
- <link rel="stylesheet" type="text/css" href="./css/styles.css">
- <link rel="stylesheet" type="text/css" href="./css/w3.css">
- </head>
- <body>
- <center>
- <div id="login_form_center">
- <img src="./logo/wecon_logo_single.png" height="50px" viewBox="-3 -3 82 82" width="50">
- <h1 style="text-align: center; font-weight: bold; ">The Right Place For Business</h1>
- <div style="margin: 0 auto; width: 268px;">
- <form action="login.php" method="post">
- <!-- display validation errors here -->
- <?php include('errors.php'); ?>
- <br>
- <input type="text" name="username" placeholder="Username" required="required" style="height: 40px;width: auto";>
- <br>
- <input type="password" name="password" placeholder="Password" required="required" style="height: 40px;width: auto">
- <br>
- <button type="submit" name="login" class="button"> Login </button>
- </form>
- </div>
- <i>Please if you don't have an account <a onclick="document.getElementById('id01').style.display='block'" class="signup_a">Sign Up</a>
- </i>
- </div>
- </center>
- </body>
- </html>
- //login.php end
- //This is for server.php
- <?php
- session_start();
- $username = "";
- $email = "";
- $error = array();
- //connect to db
- $db = mysqli_connect('localhost', 'root', '', 'practice');
- // if the register button is clicked
- if (isset($_POST['reg_user'])) {
- $username = mysqli_real_escape_string($db, $_POST['username']);
- $email = mysqli_real_escape_string($db, $_POST['email']);
- $password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
- $password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
- // ensure that form fields are filled properly
- if (empty($username)) {
- array_push($errors, "Username is required");
- }
- if (empty($email)) {
- array_push($errors, "Email is required");
- }
- if (empty($password)) {
- array_push($errors, "Password is required");
- }
- if ($password_1 != $password_2) {
- array_push($errors, "Passwords do not match");
- }
- //if there are no errors, save user to database
- if (count($errors) == 0) {
- $password = md5($password_1); //This will encrypt the password
- $sql = "INSERT INTO user (username, email, password)
- VALUES ('$username', '$email', '$password')";
- mysqli_query($db, $sql);
- $_SESSION['username'] = $username;
- $_SESSION['success'] = "You are now logged in";
- header('location: index.php'); //redirect to home page
- }
- }
- // log user in from login page
- if(isset($_POST['login'])) {
- $username = mysqli_real_escape_string($db, $_POST['username']);
- $password = mysqli_real_escape_string($db, $_POST['password']);
- // ensure that form fields are filled properly
- if (empty($username)) {
- array_push($error, "Username is required");
- }
- if (empty($password)) {
- array_push($error, "Password is required");
- }
- if (count($error) == 0 ) {
- $passord = md5($password); //encrypt password before comparing with that from database
- $query = "SELECT * FROM user WHERE username='$username' AND password='$password'";
- $result = mysqli_query($db, $query);
- if (mysqli_num_rows($result) == 1) {
- // log user in
- $_SESSION['username'] = $username;
- $_SESSION['success'] = "You are now logged in";
- header("location: index.php"); //redirect to homepage
- }else{
- array_push($error, "Wrong username/password combination. Please try again.");
- }
- }
- }
- // logout
- if (isset($_GET['logout'])) {
- session_destroy();
- unset($_SESSION['username']);
- header('location: login.php');
- }
- //This is the end of server.php
- //This is for index.php
- <?php include('server.php');
- // if user is not logged in, they cannot access this page
- if (empty($_SESSION['username'])) {
- header('location: login.php');
- }
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Home Page</title>
- <link rel="stylesheet" type="text/css" href="/css/style.css">
- </head>
- <body>
- <h1>This is the homepage</h1>
- <?php if(isset($_SESSION['success'])): ?>
- <div>
- <h3>
- <?php
- echo $_SESSION['success'];
- unset($_SESSION['success']);
- ?>
- </h3>
- </div>
- <?php endif ?>
- <?php
- // if the user logs in print information about him
- if (isset($_SESSION["username"])) : ?>
- <h3>Welcome <strong><?php echo $_SESSION['username']; ?> ?></strong></h3>
- <p><a href="index.php?logout='1'" style="color: red;">Logout</a></p>
- <?php endif ?>
- </body>
- </html>
- //end of index.php
- //This is for errors.php
- <?php if (count($error) > 0 ): ?>
- <div>
- <?php foreach ($error as $error): ?>
- <p><?php echo $error; ?></p>
- <?php endforeach ?>
- </div>
- <?php endif ?>
- //End of errors.php
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement