A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::open($save_path, $name) should either be compatible with SessionHandlerInterface::open(string $path, string $name): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 132

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::close() should either be compatible with SessionHandlerInterface::close(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 290

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::read($session_id) should either be compatible with SessionHandlerInterface::read(string $id): string|false, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 164

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::write($session_id, $session_data) should either be compatible with SessionHandlerInterface::write(string $id, string $data): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 233

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::destroy($session_id) should either be compatible with SessionHandlerInterface::destroy(string $id): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 313

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::gc($maxlifetime) should either be compatible with SessionHandlerInterface::gc(int $max_lifetime): int|false, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 354

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_cookie_params(): Session cookie parameters cannot be changed after headers have already been sent

Filename: Session/Session.php

Line Number: 296

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Session ini settings cannot be changed after headers have already been sent

Filename: drivers/Session_files_driver.php

Line Number: 108

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_save_handler(): Session save handler cannot be changed after headers have already been sent

Filename: Session/Session.php

Line Number: 110

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_start(): Session cannot be started after headers have already been sent

Filename: Session/Session.php

Line Number: 143

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

Complete User Registration System using PHP and MySQL database | Coderanks

Logo
Code Ranks ×

User Registration System In Php

27/03/2019  .   6 minutes, 45 seconds to read  .   Posted by Admin
#Php #MVCArchitecture #Model,View,Controller #htmlemail #codeigniteremail #mysql #phpmyadmin #best-practices #MSSQLServer #Update

User Registration System using PHP and MySQL database (Part-1)

In this Article, we'll walk through the complete process of creating a user registration system where users can create an account by providing a username, an email and password in md5, login page and log out page using PHP and MySQL. We will also learn how a user creates Database, Connection with Database, Insertion, Selection, Selection with Comparison and Forms with Post method and Action.

Requirements:

  1.  Xampp or Wamp Server Installed
  2. Code Editor( i.e VS Code/Sublime Text 3 etc.)

Detail:

Create a database called registration. In the database named as registration, users table added. The users table will take the following four fields as input from registration.

  1. id 
  2. username - varchar(60)
  3. email - varchar(60)
  4. password - varchar(60)

You can create it on the MySQL prompt using the following SQL script:

CREATE TABLE `users` (
      `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
      `username` varchar(100) NOT NULL,
      `email` varchar(100) NOT NULL,
      `password` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

 

You can create this using a MySQL client like PHPMyAdmin

cr_image

And that's it with the database. 

Now create a folder called registration in your localhost Server i.e create the folder inside htdocs (if you are using XAMPP server) or inside www (if you are using WAMPP server).

Create a folder name registration and add the following file in it: 

cr_image

Open these files up in a text editor of your choice. Try to use the VS Code.

Registering a user:

Open the register.php file and write the following code in it:

register.php

<?php include('db.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>Sign Up</h2>
  </div>
	
  <form method="post" action="register.php">
  <?php include('error.php'); ?>
  	<div class="input-group">
  	  <label>Username</label>
  	  <input type="text" name="username" placeholder="User Name" required="required">
  	</div>
  	<div class="input-group">
  	  <label>Email</label>
  	  <input type="email" name="email" placeholder="Email" required="required">
  	</div>
  	<div class="input-group">
  	  <label>Password</label>
  	  <input type="password" name="password" required="required" placeholder="Password">
  	</div>
  	<div class="input-group">
  	  <label>Confirm password</label>
  	  <input type="password" name="confirm_password" placeholder="Confirm Password" required="required">
  	</div>
  	<div class="input-group">
  	  <button type="submit" class="btn" name="registerNewUser">Register</button>
  	</div>
  	<div>
  		Already have an account? 
			<a class="right" href="login.php">Login</a>
  	</div>
  </form>
</body>
</html>

A few things to note here:

First is that our form's action attribute is set to register.php. This means that when the form submit button is clicked, all the data in the form will be submitted to the page (register.php). The part of the code that receives this form data is written in the db.php file and that's why we are including it at the very top of the register.php file.

In the head section, a style.css file is linking. Open up the style.css file and write/paste the following CSS in it:

style.css

* {
    margin: 0px;
    padding: 0px;
  }
  body {
    font-size: 120%;
    background: #F8F8FF;
  }
  
  .header {
    width: 35%;
    margin: 50px auto 0px;
    color: #060606;
    background: #c3cfd0;
    text-align: center;
    border: 1px solid #a4a8ad;
    border-bottom: none;
    border-radius: 10px 10px 0px 0px;
    padding: 20px;
  }
  form, .content {
    width: 35%;
    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: 100%;
    padding: 5px 10px;
    font-size: 16px;
    border-radius: 5px;
    border: 1px solid gray;
  }
  .btn {
    padding: 10px;
    font-size: 15px;
    color: #060606;
    background: #c3cfd0;
    border: none;
    text-align: center;
    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;
  }
  .right{
    text-align: right;
  }
  

Now the form looks beautiful and reasonable.

Registration page looks like:

Let's now write the code that will receive information submitted from the form in register.php and store/register the information in the database. As promised above, we'll do this in the db.php file.

Now enter data in the form like:

Open a db.php file and write this code carefully in it:

db.php

<?php
// connect to the database
$server="localhost";
$username="root";
$password="";
$database_name="registration";
$errors = array(); 
$con = mysqli_connect($server, $username, $password, $database_name);
if($con){
    if (isset($_POST['registerNewUser'])) {
        // receive all input values from the register.php form
        $username = mysqli_real_escape_string($con, $_POST['username']);
        $email = mysqli_real_escape_string($con, $_POST['email']);
        $password = mysqli_real_escape_string($con, $_POST['password']);
        $confirm_password = mysqli_real_escape_string($con, $_POST['confirm_password']);
        // by using array_push() corresponding errors in $errors() which is an array of errors.
        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 != $confirm_password) {
            array_push($errors, "Failed to Match");
        }  
            //fistly check in database that a user does not already exist with the same username and/or email.
        $get_all = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1";
        $result = mysqli_query($con, $get_all);
        $user = mysqli_fetch_assoc($result);
                
        if ($user) { // if user exists
        if ($user['username'] === $username) {
            array_push($errors, "Username is already existed");
        }
    
        if ($user['email'] === $email) {
            array_push($errors, "email is already existed");
        }
        }
    
        // Finally, register user if no error
        if (count($errors) == 0) {
            $pwd = md5($password);//encrypt the password before inserting in the database
    
            $register = "INSERT INTO users (username, email, password) 
                    VALUES('$username', '$email', '$pwd')";
            mysqli_query($con, $register);
            header('Location: login.php');
        }
    }
?>

Remember a few things here:-

The if statement determines if the reg_user button on the registration form is clicked. Remember, in our form, the submit button has a name attribute set to reg_user and that is what we are referencing in the if statement.

All the data in we inserted to the input in the form is received and carefully filtered or checked to make sure that the form correctly filled. The two fields of a password are also compared with each other to make sure that they are similar or match.

If there are no errors, the user is registered or signed it up in the users table in the database with a password(md5). The password is in md5 for security reasons. It ensures the user that even if someone like hacker manages to gain access to the database, it would not be easy to read the password.

But error messages are not displaying now because our errors.php file is still empty. To display the errors while executing the code, write this code in the error.php file carefully.

error.php

<?php  
if (count($errors) > 0) 
{ ?>
    <div class="error">
        <?php foreach ($errors as $error) 
        { ?>
        <p><?php echo $error ?></p>
        <?php }?>
    </div>
<?php
} ?>

After registration or adding the new user, Table will look like:

Important Points:

WAMPP or XAMPP is required for localhost to run the code.

First, run localhost server and for the register.php file, the URL is localhoast/registration/register.php 

Create a table in the database before insertion and use mysqli_real_escape_string() to avoid possible string errors.

Carefully write/handle PHP in HTML.

The error.php file is a guide for errors only.

Part-2