What is session?
The session is used to access the various pages of an entire website. A session creates a temporary directory on the server. With the help of session variables và their value is stored. In the previous tutorial, we discussed the session in PHP. A session is used to lớn store the information on the vps rather than the client computer.
Create a MYSQL database table using Query
Whenever we create a login system we need an MYSQL database.We can logIn by fetching user data from the MYSQL database table. In the previous tutorial, we created a simple login khung using PHP & MYSQL databse. Let"s create a database table using the MYSQL query.
CREATE TABLE `users` ( `id` INT( 50 ) NOT NULL , `uname` VARCHAR( 40 ) NOT NULL , `upassword` VARCHAR( 40 ) NOT NULL) ENGINE = INNODB default CHARSET = latin1;
Insert data into MYSQL database table for Login with sessionkhổng lồ insert the data into the table, you can use the registration form or insert query. When you create a login form, you have to lớn fetch the data. Unless you have data in the database, you will not be able lớn create the login system. You can use the registration khung to insert data or you can perform insert query to lớn insert data into the database table.Let"s insert the data into database table using MYSQL query.
Bạn đang xem: Php mysql login system
INSERT INTO `users` (`id`, `uname`, `upassword`) VALUES(1, "admin", "admin
Create a connection tệp tin to connect MYSQL & PHPThe config file hanles the connection between PHP and MYSQL database. Now, create a file and save with name & extension "config.php".
<?php$databaseHost = "127.0.0.1";//or localhost$databaseName = "project"; // your db_name$databaseUsername = "root"; // root by mặc định for localhost $databasePassword = ""; // by defualt empty for localhost$mysqli = mysqli_connect($databaseHost, $databaseUsername, $databasePassword, $databaseName); ?> In the above config file, we are used mysqli_connect() function for a new connection. We are passed this information as a parameter & stored in a variable $mysqli. You can change database host, database name, database username, và database Password according to your needs.
Login with session pages structure in PHP
In this small project, we use the session lớn access session data on pages of the website. We create pages to lớn access session data. After the destroyed session, unknown persons cannot access the pages. This is the main advantage of PHP session . Let"s create some pages . 1.Index.html - After session created the user redirects to main page(index.html).2.Login.php -
The login page handles the user đầu vào information. This is the starting page of login system. Without login success, user cannot access all pages.3.Loginprocess.php -
This is the kích hoạt file of the login form. That includes all the PHP script. We create a session in this file.4.Page1.php -
Access session on page 1 after login.5.Page2.php -
Access session on page 2 after login.
Access session on page 3 after login.7.Logout.php -
We use session destroy to the destroyed complete session. This is used to logout. After logout, the user cannot access another pages.
The session is recommended to start the session at the beginning of every page. Index.html is a trang chủ page.When a user wants to visit index.html without login the session returns lớn the login page. The user can not directly access the index.html page without login. If any user wants to access any page session return to the login page. First of all, the user should log in then access all pages direct till the session destroyed? Let"s implement a complete Login system using PHP and MYSQL database.
Make a login size for user login using PHP sessionNow, we create an HTML form. The HTML khung is used to input information from the user. A user enters a username and password using HTML form. This is the beginning of the login system in PHP. The user files a username & password and clicks the login button. If the username và password are matched, the user logs in. If the password & username are incorrect, an error message is displayed on the same login form. Let"s create an HTML form.
In the above form, we defined the action "loginprocess.php". The login process tệp tin handles the backend login process.
Create PHP code for login using session
We create khung handling scripts in PHP. This tệp tin handles the MYSQL và HTML khung functional. 1.First of all ,start session at the beginning of the page2. We include the config file.We can user include() function & require() function to lớn include config.php file. 3. Use if condition to set form fields data on the button. 4. Here, we use the request variable. The request variable contains the PHP POST method values. 5. Use the MYSQL select query. 6. Store session variable data. 7. Use header() function . The header() function is used lớn redirect another page after login. If the error occurred ,redirects to the same login form. If user id và password are matched , redirects lớn index.html page else error shows result "please enter valid id and password".
Create the main page after login successNow, create the main page index.html page. It is the index page of the login system. First of all, we start the session by session_start() function at the beginning of the page. After that, we kiểm tra if the session is not created than return khổng lồ the login page (login.php ).
Now, We create more pages (page 1, page2, and page 3) as example khổng lồ access session. All pages include session_start() function at the beginning of the page. Start session is the must for entire pages. Users can access all pages by the login. If the session is not created or destroyed then redirect to the login.php page.A trang web contains a lot of pages. These pages help you understand how to access sessions on the pages of the website. If the session is destroyed, the user can not access any page of the website. An authenticated user can access the entire pages of the website. That"s why the session is used for security purposes. Secure your trang web data from unknown users. Let"s create another page.
Make another page 1 to access login with session
Create another page. Start the session on every single page of the website. Use if condition, If the session is mix then stay on the page và if the session is destroyed then redirect khổng lồ login.php After this PHP code, you can create và design your page according lớn your need but vì not remove PHP session code at the đứng top of pages.Follow these instructions for other website pages.