Question

In: Computer Science

Using PHP Provide and the ability for the user to determine their userid and to change...

Using PHP

Provide and the ability for the user to determine their userid and to change their password if they forget either.

Also, provide the ability for the user to change the password whenever they chose. If they are changing their password (when they know the password), they must be required to enter the old password, and then the new password (twice).

Anytime the password is changed it must verify the correct format of the password as previously mentioned in other assignments.

Make sure that your program works properly.

Solutions

Expert Solution

1.register.php

<?php include('functions.php') ?>
<!DOCTYPE html>
<html>
<head>
        <title>Registration system PHP and MySQL</title>
        <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
        <div class="header">
                <h2>Register</h2>
        </div>
        
        <form method="post" action="register.php">

                <?php echo display_error(); ?>

                <div class="input-group">
                        <label>Username</label>
                        <input type="text" name="username" value="<?php echo $username; ?>">
                </div>
                <div class="input-group">
                        <label>Email</label>
                        <input type="email" name="email" value="<?php echo $email; ?>">
                </div>
                <div class="input-group">
                        <label>Password</label>
                        <input type="password" name="password_1">
                </div>
                <div class="input-group">
                        <label>Confirm password</label>
                        <input type="password" name="password_2">
                </div>
                <div class="input-group">
                        <button type="submit" class="btn" name="register_btn">Register</button>
                </div>
                <p>
                        Already a member? <a href="login.php">Sign in</a>
                </p>
        </form>
</body>
</html>

2.login.php

<?php include('functions.php') ?>
<!DOCTYPE html>
<html>
<head>
        <title>Registration system PHP and MySQL</title>
        <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

        <div class="header">
                <h2>Login</h2>
        </div>
        
        <form method="post" action="login.php">

                <?php echo display_error(); ?>

                <div class="input-group">
                        <label>Username</label>
                        <input type="text" name="username" >
                </div>
                <div class="input-group">
                        <label>Password</label>
                        <input type="password" name="password">
                </div>
                <div class="input-group">
                        <button type="submit" class="btn" name="login_btn">Login</button>
                </div>
                <p>
                        Not yet a member? <a href="register.php">Sign up</a>
                </p>
        </form>


</body>
</html>

3.style.css

* {
        margin: 0px;
        padding: 0px;
}
body {
        font-size: 120%;
        background: #F8F8FF;
}

.header {
        width: 40%;
        margin: 50px auto 0px;
        color: white;
        background: #5F9EA0;
        text-align: center;
        border: 1px solid #B0C4DE;
        border-bottom: none;
        border-radius: 10px 10px 0px 0px;
        padding: 20px;
}
form, .content {
        width: 40%;
        margin: 0px auto;
        padding: 20px;
        border: 1px solid #B0C4DE;
        background: white;
        border-radius: 0px 0px 10px 10px;
}
.input-group {
        margin: 10px 0px 10px 0px;
}

.input-group label {
        display: block;
        text-align: left;
        margin: 3px;
}
.input-group input {
        height: 30px;
        width: 93%;
        padding: 5px 10px;
        font-size: 16px;
        border-radius: 5px;
        border: 1px solid gray;
}
#user_type {
        height: 40px;
        width: 98%;
        padding: 5px 10px;
        background: white;
        font-size: 16px;
        border-radius: 5px;
        border: 1px solid gray;
}
.btn {
        padding: 10px;
        font-size: 15px;
        color: white;
        background: #5F9EA0;
        border: none;
        border-radius: 5px;
}
.error {
        width: 92%; 
        margin: 0px auto; 
        padding: 10px; 
        border: 1px solid #a94442; 
        color: #a94442; 
        background: #f2dede; 
        border-radius: 5px; 
        text-align: left;
}
.success {
        color: #3c763d; 
        background: #dff0d8; 
        border: 1px solid #3c763d;
        margin-bottom: 20px;
}

.profile_info img {
        display: inline-block; 
        width: 50px; 
        height: 50px; 
        margin: 5px;
        float: left;
}

.profile_info div {
        display: inline-block; 
        margin: 5px;
}

.profile_info:after {
        content: "";
        display: block;
        clear: both;
}

4.home.php

<?php 
        include('../functions.php');

        if (!isAdmin()) {
                $_SESSION['msg'] = "You must log in first";
                header('location: ../login.php');
        }

?>
<!DOCTYPE html>
<html>
<head>
        <title>Home</title>
        <link rel="stylesheet" type="text/css" href="../style.css">
        <style>
        .header {
                background: #003366;
        }
        button[name=register_btn] {
                background: #003366;
        }
        </style>
</head>
<body>
        <div class="header">
                <h2>Admin - Home Page</h2>
        </div>
        <div class="content">
                <!-- notification message -->
                <?php if (isset($_SESSION['success'])) : ?>
                        <div class="error success" >
                                <h3>
                                        <?php 
                                                echo $_SESSION['success']; 
                                                unset($_SESSION['success']);
                                        ?>
                                </h3>
                        </div>
                <?php endif ?>

                <!-- logged in user information -->
                <div class="profile_info">
                        <img src="../images/admin_profile.png"  >

                        <div>
                                <?php  if (isset($_SESSION['user'])) : ?>
                                        <strong><?php echo $_SESSION['user']['username']; ?></strong>

                                        <small>
                                                <i  style="color: #888;">(<?php echo ucfirst($_SESSION['user']['user_type']); ?>)</i> 
                                                <br>
                                                <a href="home.php?logout='1'" style="color: red;">logout</a>
                                                &nbsp; <a href="create_user.php"> + add user</a>
                                        </small>

                                <?php endif ?>
                        </div>
                </div>



        </div>
                
</body>
</html>

5.functions.php

<?php 
        session_start();

        // connect to database
        $db = mysqli_connect('localhost', 'root', '', 'multi_login');

        // variable declaration
        $username = "";
        $email    = "";
        $errors   = array(); 

        // call the register() function if register_btn is clicked
        if (isset($_POST['register_btn'])) {
                register();
        }

        // call the login() function if register_btn is clicked
        if (isset($_POST['login_btn'])) {
                login();
        }

        if (isset($_GET['logout'])) {
                session_destroy();
                unset($_SESSION['user']);
                header("location: ../login.php");
        }

        // REGISTER USER
        function register(){
                global $db, $errors;

                // receive all input values from the form
                $username    =  e($_POST['username']);
                $email       =  e($_POST['email']);
                $password_1  =  e($_POST['password_1']);
                $password_2  =  e($_POST['password_2']);

                // form validation: ensure that the form is correctly filled
                if (empty($username)) { 
                        array_push($errors, "Username is required"); 
                }
                if (empty($email)) { 
                        array_push($errors, "Email is required"); 
                }
                if (empty($password_1)) { 
                        array_push($errors, "Password is required"); 
                }
                if ($password_1 != $password_2) {
                        array_push($errors, "The two passwords do not match");
                }

                // register user if there are no errors in the form
                if (count($errors) == 0) {
                        $password = md5($password_1);//encrypt the password before saving in the database

                        if (isset($_POST['user_type'])) {
                                $user_type = e($_POST['user_type']);
                                $query = "INSERT INTO users (username, email, user_type, password) 
                                                  VALUES('$username', '$email', '$user_type', '$password')";
                                mysqli_query($db, $query);
                                $_SESSION['success']  = "New user successfully created!!";
                                header('location: home.php');
                        }else{
                                $query = "INSERT INTO users (username, email, user_type, password) 
                                                  VALUES('$username', '$email', 'user', '$password')";
                                mysqli_query($db, $query);

                                // get id of the created user
                                $logged_in_user_id = mysqli_insert_id($db);

                                $_SESSION['user'] = getUserById($logged_in_user_id); // put logged in user in session
                                $_SESSION['success']  = "You are now logged in";
                                header('location: index.php');                          
                        }

                }

        }

        // return user array from their id
        function getUserById($id){
                global $db;
                $query = "SELECT * FROM users WHERE id=" . $id;
                $result = mysqli_query($db, $query);

                $user = mysqli_fetch_assoc($result);
                return $user;
        }

        // LOGIN USER
        function login(){
                global $db, $username, $errors;

                // grap form values
                $username = e($_POST['username']);
                $password = e($_POST['password']);

                // make sure form is filled properly
                if (empty($username)) {
                        array_push($errors, "Username is required");
                }
                if (empty($password)) {
                        array_push($errors, "Password is required");
                }

                // attempt login if no errors on form
                if (count($errors) == 0) {
                        $password = md5($password);

                        $query = "SELECT * FROM users WHERE username='$username' AND password='$password' LIMIT 1";
                        $results = mysqli_query($db, $query);

                        if (mysqli_num_rows($results) == 1) { // user found
                                // check if user is admin or user
                                $logged_in_user = mysqli_fetch_assoc($results);
                                if ($logged_in_user['user_type'] == 'admin') {

                                        $_SESSION['user'] = $logged_in_user;
                                        $_SESSION['success']  = "You are now logged in";
                                        header('location: admin/home.php');               
                                }else{
                                        $_SESSION['user'] = $logged_in_user;
                                        $_SESSION['success']  = "You are now logged in";

                                        header('location: index.php');
                                }
                        }else {
                                array_push($errors, "Wrong username/password combination");
                        }
                }
        }

        function isLoggedIn()
        {
                if (isset($_SESSION['user'])) {
                        return true;
                }else{
                        return false;
                }
        }

        function isAdmin()
        {
                if (isset($_SESSION['user']) && $_SESSION['user']['user_type'] == 'admin' ) {
                        return true;
                }else{
                        return false;
                }
        }

        // escape string
        function e($val){
                global $db;
                return mysqli_real_escape_string($db, trim($val));
        }

        function display_error() {
                global $errors;

                if (count($errors) > 0){
                        echo '<div class="error">';
                                foreach ($errors as $error){
                                        echo $error .'<br>';
                                }
                        echo '</div>';
                }
        }

?>

6.index.php

<?php 
        include('functions.php');

        if (!isLoggedIn()) {
                $_SESSION['msg'] = "You must log in first";
                header('location: login.php');
        }
?>
<!DOCTYPE html>
<html>
<head>
        <title>Home</title>
        <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
        <div class="header">
                <h2>Home Page</h2>
        </div>
        <div class="content">
                <!-- notification message -->
                <?php if (isset($_SESSION['success'])) : ?>
                        <div class="error success" >
                                <h3>
                                        <?php 
                                                echo $_SESSION['success']; 
                                                unset($_SESSION['success']);
                                        ?>
                                </h3>
                        </div>
                <?php endif ?>
                <!-- logged in user information -->
                <div class="profile_info">
                        <img src="images/user_profile.png"  >

                        <div>
                                <?php  if (isset($_SESSION['user'])) : ?>
                                        <strong><?php echo $_SESSION['user']['username']; ?></strong>

                                        <small>
                                                <i  style="color: #888;">(<?php echo ucfirst($_SESSION['user']['user_type']); ?>)</i> 
                                                <br>
                                                <a href="index.php?logout='1'" style="color: red;">logout</a>
                                        </small>

                                <?php endif ?>
                        </div>
                </div>
        </div>
</body>
</html>

7.create_user.php

<?php include('../functions.php') ?>
<!DOCTYPE html>
<html>
<head>
        <title>Registration system PHP and MySQL - Create user</title>
        <link rel="stylesheet" type="text/css" href="../style.css">
        <style>
                .header {
                        background: #003366;
                }
                button[name=register_btn] {
                        background: #003366;
                }
        </style>
</head>
<body>
        <div class="header">
                <h2>Admin - create user</h2>
        </div>
        
        <form method="post" action="create_user.php">

                <?php echo display_error(); ?>

                <div class="input-group">
                        <label>Username</label>
                        <input type="text" name="username" value="<?php echo $username; ?>">
                </div>
                <div class="input-group">
                        <label>Email</label>
                        <input type="email" name="email" value="<?php echo $email; ?>">
                </div>
                <div class="input-group">
                        <label>User type</label>
                        <select name="user_type" id="user_type" >
                                <option value=""></option>
                                <option value="admin">Admin</option>
                                <option value="user">User</option>
                        </select>
                </div>
                <div class="input-group">
                        <label>Password</label>
                        <input type="password" name="password_1">
                </div>
                <div class="input-group">
                        <label>Confirm password</label>
                        <input type="password" name="password_2">
                </div>
                <div class="input-group">
                        <button type="submit" class="btn" name="register_btn"> + Create user</button>
                </div>
        </form>
</body>
</html>

Summary:

The following code contains a whole structure like Register(SignUp), Login Page, functions(like login, logout, admin, etc). There is also a database function where it stores the user info.

Note: Put all the codes in the same folder in order to run with errors.

Note: If you find any error or if i had missed something then please let me know through comments.


Related Solutions

Using PHP, Make a form that allows the user to enter the weight of the item...
Using PHP, Make a form that allows the user to enter the weight of the item being shipped. This will be used to calculate the shipping cost.Create a form that uses the method POST The form should capture a customer's package weight with the one field for the package weight in pounds. All fields should have the REQUIRED attribute. The form should have a submit button and a reset button. The form should look nice. All the labels should line...
Develop a program in C++, using functions, to validate a userID. Valid userID specifications: • 5...
Develop a program in C++, using functions, to validate a userID. Valid userID specifications: • 5 - 10 characters long. • must begin with a letter. • must contain at least one upper case letter. • must contain at least one lower case letter. • must contain at least one decimal digit. • must contain at least one of the following special characters: #_$ • must not contain any other characters than those specified above. The main program should loop,...
Develop a program in C++, using functions, to validate a userID. Valid userID specifications: • 5...
Develop a program in C++, using functions, to validate a userID. Valid userID specifications: • 5 - 10 characters long. • must begin with a letter. • must contain at least one upper case letter. • must contain at least one lower case letter. • must contain at least one decimal digit. • must contain at least one of the following special characters: #_$ • must not contain any other characters than those specified above. The main program should loop,...
PHP You will be using the object oriented features of PHP to design a music album...
PHP You will be using the object oriented features of PHP to design a music album processing page. First you will build a form page called addAlbum.php. This form will contain text fields for album title, artist, publisher (Sony, BMI, etc.) and genre. Add two more fields of your choice. You will post this form to the file process.php. If all the fields have values, we will create a new Album object and print the details of the object. You...
organizational change essay using the article: changing the change rules at Google, and determine why they...
organizational change essay using the article: changing the change rules at Google, and determine why they were successful in changing their employees behavior in approximately 750 word essay, address the following: evaluate what happens when change is not managed with an organization and what impact can it have on employees identify ways change fatigue can b avoided in a organization where innovation and change is constant. discuss who should be involved in change management strategies to ensure a greater success
How would I structure the following PHP (PDO) code into a table format using only PHP?...
How would I structure the following PHP (PDO) code into a table format using only PHP? //Our SQL statement, which will select a list of tables from the current MySQL database. $sql = "SELECT * FROM jobs"; //Prepare our SQL statement, $statement = $pdo->prepare($sql); //Execute the statement. $statement->execute(); //Fetch the rows from our statement. $tables = $statement->fetchAll(PDO::FETCH_NUM); //Loop through our table names. foreach($tables as $table){ //Print the table name out onto the page. echo $table[0], ' '; echo $table[1], '...
I need assistance in making a simple html and php script that lets a user explor...
I need assistance in making a simple html and php script that lets a user explor the RGB color specturum. It should be able to use 6 buttons (R+,G+,B+,R-,G-,B-) that when pressed, add or subtract 2 points (ranging from 0-255) of that color from the RGB ratio. The new RGB color is then displayed in a small window/box at the bottom of the page for the user to see. This should allow the user to explore all the different colors...
: PHP Please read carefully - display the USER info for IP address, web browser and...
: PHP Please read carefully - display the USER info for IP address, web browser and operating system - do not hard code these parts    1. Create a new PHP document (nit Objective 1)    2. Write a comment similar to the following: "This is my first PHP document, which displays some data in the web browser"(Unit Objective 1)    3. Assign your name as a string value into the variable labeled myName (Unit Objective 2)    4. Assign...
Create a Database in POSTGRESQL using the following table names and attributes: users: userid (int, primary...
Create a Database in POSTGRESQL using the following table names and attributes: users: userid (int, primary key), name (text) movies: movieid (integer, primary key), title (text) taginfo: tagid (int, primary key), content (text) genres: genreid (integer, primary key), name (text) ratings: userid (int, foreign key), movieid (int, foreign key), rating (numeric), timestamp (bigint, seconds since midnight Coordinated Universal Time (UTC) of January 1, 1970) tags: userid (int, foreign key), movieid (int, foreign key), tagid (int, foreign key), timestamp (bigint, seconds...
PHP Please read carefully - display the USER info for IP address, web browser and operating...
PHP Please read carefully - display the USER info for IP address, web browser and operating system - do not hard code these parts    1. Create a new PHP document (nit Objective 1)    2. Write a comment similar to the following: "This is my first PHP document, which displays some data in the web browser"(Unit Objective 1)    3. Assign your name as a string value into the variable labeled myName (Unit Objective 2)    4. Assign 53870...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT