Here, let’s assume that register.php and database have been created and we just need to create login.php so that the user can login to access further features.
- Required files (already created)
- Register.php for register form
- Database for registering and logging in
- config.php to connect
- Index.php for the landing page, just HTML and CSS, don’t forget to add an href on the login button to login.php so you can connect.
- If it has not been created, it can be seen in article 05. Register with PHP.
- Create a login feature
- Please create login.php, fill it with the required code such as:
- Require_once(config.php) to connect with config.php and use functions in config.php
- After that check the login button has been created or not by using isset
- Perform a filter on the username and password so that the inputted data can enter the database.
- Next do the query:
SELECT * FROM users WHERE username=:username OR email=:email
- In the query we use logical OR so that the user can login with the email or username that has been created.
- Bind parameters and query execution
$params = array(
“:username” => $username,
“:email” => $username
);
$stmt->execute($params);
- After the query is executed, we retrieve the results using the fetch() function;
$user = $stmt->fetch(PDO::FETCH_ASSOC);
That is, we take the query result as an associative array
- The fetch() function will return NULL if there is no data, so we will create a condition with if
- Next we verify the password that has been created by the user with the password in the database by:
if(password_verify($password, $user[“password”]))
- The password_verify function will return true if the password entered is the same as the password in the database
- Next create a session and redirect it to the timeline.php page with header()
- Create a timeline page
- Before making the code on the timeline.php page, we first create the auth.php file to check whether the user is logged in or not
- Code file auth.php with
session_start();
if(!isset($_SESSION[“user”])) header(“Location: login.php”);
to use the $_SESSION variable, you must first call the session_start() function, then check whether you are logged in or not with the code below
-Create and fill in the timeline.php code with require_once(“auth.php”) so that timeline.php can only be opened by those who are logged in and fill in the display with HTML and CSS as desired
-Login has been successfully entered and access timeline.php which can only be accessed by those who have logged in.
-Logout with PHP
-Open it and create a logout.php file
-Fill the file with the code:
-Session_start(); so that the $_SESSION variable can be used
-Session_start(“user”); to destroy and delete the variable $_SESSION[“user”];
-Header(“Location: index.php”); to return to the main page
-Destroying is complete and the user will return to the main page
- Conclusion and Closing
By creating a login feature, certain users will access certain pages that cannot be accessed by normal users to be able to enter that page.
The need for digital IT is needed in daily activities, Bead IT Consultant is the right choice as your partner, visit our website by clicking this link: www.beadgroup.com