Sign Up and Login Using PHP and Mysqli
2- Login web page front-end and back-end using php.
3- Home page which you will see after login.
4- Implementation of new style sheet framework Bulma.
5- Oop structure Using php. How to use classes and function in php?
6- How to use sessions in php?
Sign up and Login in PHP Overview
First step in web development is to create your front-end design of web pages. Whenever you want to write back-end code using phpor any other language then first create front end design. In this tutorial I have designed three web pages. Sign up and login php code. Here you will also see sign up and login with sessions using php. First we look into the Sign up and login pages design using html and bulma CSS framework.
a) register.php
b) index.php
c) home.php
With these three web pages which contains html and css design,One more file is action-register.php which contains server side programming using php.
action-register.php
This file is for back-end coding using php language. In this file I have included a "config.php" file which contains base_url and connection to mysqli database variables initialization content. In the next line a class is started with the name of "add_new_user".Here you will see how to add form validations in php? We will add php form validations in sign up and login pages using PHP. We will also write code for email validation using php within sign up page.
Functions and classes in PHP
In this class create public functions for sign up and login using php. I have created four public functions, First function is "login_user" which is used for login. Login_user function containes two parameters one for email and other for password, these two parameters are used in mysqli database select query where clause. Second function is "register" and it containes five parameters($fname,$lname,$email,$phone,$password1).Third function "test_input" is used for email validation using PHP at the time of sign up button click. Fourth and last function "sanitize" is for protection of input data against sql injection in php. You will see in this function one parameter for input field passed to the mysqli_real_escape_string . Now whenever we initialize a variable with a input filed we use sanitize function instead of writing mysqli_real_escape_string. After you will create functions within the class close the class bracket.
How to display text in textboxes using php?
Next create an object of class add_new_user, below this line declare variables that we will use for our input fields
,to display error messages and for display success or failure message. Input field variables are used in value property
of each textbox to repopulate data after button click event, for example
Add this below code in action-register.php file.
First of all assign input fields to variables. Then check form validation if text boxes empty then display validation error message before saving data into mysqli database. We will also check here if two passwords text matched or not matched. If text of two passwords field not matched then display an error message. Next check if email text box is not empty then check the entered email is valid or not. if not valid then it will display an email validation error before saving data into database.
Now if count errors is equal to zero then call the function register from the class add_new_user and pass it variables. Here we will create a hash password of confirm password text box and this new hashed password will be passed into register function. If function executed then we will create a session variable and redirect to home.php otherwise display a message of failure.
Next step is to write code for login using the function login_user. Add this code below above code in action-register.php file
In above code of login button click event, assign two variables email and password. Then check both are empty are filled, if empty then display validation error message on button click of login before implementation of login_user function.
Now if count errors is equal to zero then convert password input filed text into hash code as we have saved hash password into database. After this call the login_user function from the above class and pass it variables as functions parameters.
Now if number of rows equal to 1 then fetch resualt of mysqli select query used in login_user function and initialize a variable "$name=$row['firstname']". Then pass this variable to a session variable and redirect to home.php
registration and login in php with mysqli database complete tutorial Download Source Code
No comments:
Post a Comment