{"id":1335,"date":"2022-11-28T08:31:36","date_gmt":"2022-11-28T00:31:36","guid":{"rendered":"https:\/\/hostscripter.com\/?p=1335"},"modified":"2022-11-28T08:31:36","modified_gmt":"2022-11-28T00:31:36","slug":"part-4-create-your-own-login-system-using-php-and-mysql","status":"publish","type":"post","link":"https:\/\/hostscripter.com\/?p=1335","title":{"rendered":"Part 4: Create your own Login System using PHP and MySql"},"content":{"rendered":"<h2 id=\"authenticatinguserswithphp\">4. Authenticating Users with PHP<\/h2>\n<p>Now that we have our database setup, we can go ahead and start coding with PHP. We&#8217;re going to start with the authentication file, which will process and validate the form data that we&#8217;ll send from our\u00a0<i>index.html<\/i>\u00a0file.<\/p>\n<p>Edit the\u00a0<i>authenticate.php<\/i>\u00a0file and add the following:<\/p>\n<div class=\"code-header\"><\/div>\n<pre class=\" language-php\"><code class=\" language-php\"><span class=\"token php language-php\"><span class=\"token delimiter important\">&lt;?php<\/span>\r\n<span class=\"token function\">session_start<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token comment\">\/\/ Change this to your connection info.<\/span>\r\n<span class=\"token variable\">$DATABASE_HOST<\/span> <span class=\"token operator\">=<\/span> <span class=\"token string\">'localhost'<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token variable\">$DATABASE_USER<\/span> <span class=\"token operator\">=<\/span> <span class=\"token string\">'root'<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token variable\">$DATABASE_PASS<\/span> <span class=\"token operator\">=<\/span> <span class=\"token string\">''<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token variable\">$DATABASE_NAME<\/span> <span class=\"token operator\">=<\/span> <span class=\"token string\">'phplogin'<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token comment\">\/\/ Try and connect using the info above.<\/span>\r\n<span class=\"token variable\">$con<\/span> <span class=\"token operator\">=<\/span> <span class=\"token function\">mysqli_connect<\/span><span class=\"token punctuation\">(<\/span><span class=\"token variable\">$DATABASE_HOST<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token variable\">$DATABASE_USER<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token variable\">$DATABASE_PASS<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token variable\">$DATABASE_NAME<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span> <span class=\"token function\">mysqli_connect_errno<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>\r\n\t<span class=\"token comment\">\/\/ If there is an error with the connection, stop the script and display the error.<\/span>\r\n\t<span class=\"token function\">exit<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">'Failed to connect to MySQL: '<\/span> <span class=\"token punctuation\">.<\/span> <span class=\"token function\">mysqli_connect_error<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token punctuation\">}<\/span><\/span><\/code><\/pre>\n<p>Initially, the code will start the session as this enables us to preserve account details on the server and will be used later on to remember logged-in users.<\/p>\n<p>Connecting to the database is essential. Without it, how can we retrieve and store information related to our users? Therefore, we must make sure to update the variables to reflect our MySQL database credentials.<\/p>\n<p>Add below:<\/p>\n<div class=\"code-header\"><\/div>\n<pre class=\" language-php\"><code class=\" language-php\"><span class=\"token comment\">\/\/ Now we check if the data from the login form was submitted, isset() will check if the data exists.<\/span>\r\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span> <span class=\"token operator\">!<\/span><span class=\"token function\">isset<\/span><span class=\"token punctuation\">(<\/span><span class=\"token global\">$_POST<\/span><span class=\"token punctuation\">[<\/span><span class=\"token string\">'username'<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token global\">$_POST<\/span><span class=\"token punctuation\">[<\/span><span class=\"token string\">'password'<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>\r\n\t<span class=\"token comment\">\/\/ Could not get the data that should have been sent.<\/span>\r\n\t<span class=\"token function\">exit<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">'Please fill both the username and password fields!'<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token punctuation\">}<\/span><\/code><\/pre>\n<p>The above code will make sure the form data exists, whereas if the user tries to access the file without submitting the form, it will output a simple error.<\/p>\n<p>Add below:<\/p>\n<div class=\"code-header\"><\/div>\n<pre class=\" language-php\"><code class=\" language-php\"><span class=\"token comment\">\/\/ Prepare our SQL, preparing the SQL statement will prevent SQL injection.<\/span>\r\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token variable\">$stmt<\/span> <span class=\"token operator\">=<\/span> <span class=\"token variable\">$con<\/span><span class=\"token operator\">-<\/span><span class=\"token operator\">&gt;<\/span><span class=\"token function\">prepare<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">'SELECT id, password FROM accounts WHERE username = ?'<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>\r\n\t<span class=\"token comment\">\/\/ Bind parameters (s = string, i = int, b = blob, etc), in our case the username is a string so we use \"s\"<\/span>\r\n\t<span class=\"token variable\">$stmt<\/span><span class=\"token operator\">-<\/span><span class=\"token operator\">&gt;<\/span><span class=\"token function\">bind_param<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">'s'<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token global\">$_POST<\/span><span class=\"token punctuation\">[<\/span><span class=\"token string\">'username'<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n\t<span class=\"token variable\">$stmt<\/span><span class=\"token operator\">-<\/span><span class=\"token operator\">&gt;<\/span><span class=\"token function\">execute<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n\t<span class=\"token comment\">\/\/ Store the result so we can check if the account exists in the database.<\/span>\r\n\t<span class=\"token variable\">$stmt<\/span><span class=\"token operator\">-<\/span><span class=\"token operator\">&gt;<\/span><span class=\"token function\">store_result<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n\r\n\r\n\t<span class=\"token variable\">$stmt<\/span><span class=\"token operator\">-<\/span><span class=\"token operator\">&gt;<\/span><span class=\"token function\">close<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token punctuation\">}<\/span>\r\n<span class=\"token delimiter important\">?&gt;<\/span><\/code><\/pre>\n<p>The above code will prepare the SQL statement that will select the\u00a0<i class=\"hl\">id<\/i>\u00a0and\u00a0<i class=\"hl\">password<\/i>\u00a0columns from the accounts table. In addition, it will bind the\u00a0<i class=\"hl\">username<\/i>\u00a0to the SQL statement, execute, and then store the result.<\/p>\n<p>After the following line:<\/p>\n<pre class=\" language-php\"><code class=\" language-php\"><span class=\"token variable\">$stmt<\/span><span class=\"token operator\">-<\/span><span class=\"token operator\">&gt;<\/span><span class=\"token function\">store_result<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/code><\/pre>\n<p>Add:<\/p>\n<div class=\"code-header\"><\/div>\n<pre class=\" language-php\"><code class=\" language-php\"><span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token variable\">$stmt<\/span><span class=\"token operator\">-<\/span><span class=\"token operator\">&gt;<\/span><span class=\"token property\">num_rows<\/span> <span class=\"token operator\">&gt;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>\r\n\t<span class=\"token variable\">$stmt<\/span><span class=\"token operator\">-<\/span><span class=\"token operator\">&gt;<\/span><span class=\"token function\">bind_result<\/span><span class=\"token punctuation\">(<\/span><span class=\"token variable\">$id<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token variable\">$password<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n\t<span class=\"token variable\">$stmt<\/span><span class=\"token operator\">-<\/span><span class=\"token operator\">&gt;<\/span><span class=\"token function\">fetch<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n\t<span class=\"token comment\">\/\/ Account exists, now we verify the password.<\/span>\r\n\t<span class=\"token comment\">\/\/ Note: remember to use password_hash in your registration file to store the hashed passwords.<\/span>\r\n\t<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token function\">password_verify<\/span><span class=\"token punctuation\">(<\/span><span class=\"token global\">$_POST<\/span><span class=\"token punctuation\">[<\/span><span class=\"token string\">'password'<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token variable\">$password<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>\r\n\t\t<span class=\"token comment\">\/\/ Verification success! User has logged-in!<\/span>\r\n\t\t<span class=\"token comment\">\/\/ Create sessions, so we know the user is logged in, they basically act like cookies but remember the data on the server.<\/span>\r\n\t\t<span class=\"token function\">session_regenerate_id<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n\t\t<span class=\"token global\">$_SESSION<\/span><span class=\"token punctuation\">[<\/span><span class=\"token string\">'loggedin'<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">=<\/span> <span class=\"token constant\">TRUE<\/span><span class=\"token punctuation\">;<\/span>\r\n\t\t<span class=\"token global\">$_SESSION<\/span><span class=\"token punctuation\">[<\/span><span class=\"token string\">'name'<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">=<\/span> <span class=\"token global\">$_POST<\/span><span class=\"token punctuation\">[<\/span><span class=\"token string\">'username'<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span>\r\n\t\t<span class=\"token global\">$_SESSION<\/span><span class=\"token punctuation\">[<\/span><span class=\"token string\">'id'<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">=<\/span> <span class=\"token variable\">$id<\/span><span class=\"token punctuation\">;<\/span>\r\n\t\t<span class=\"token keyword\">echo<\/span> <span class=\"token string\">'Welcome '<\/span> <span class=\"token punctuation\">.<\/span> <span class=\"token global\">$_SESSION<\/span><span class=\"token punctuation\">[<\/span><span class=\"token string\">'name'<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token punctuation\">.<\/span> <span class=\"token string\">'!'<\/span><span class=\"token punctuation\">;<\/span>\r\n\t<span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token punctuation\">{<\/span>\r\n\t\t<span class=\"token comment\">\/\/ Incorrect password<\/span>\r\n\t\t<span class=\"token keyword\">echo<\/span> <span class=\"token string\">'Incorrect username and\/or password!'<\/span><span class=\"token punctuation\">;<\/span>\r\n\t<span class=\"token punctuation\">}<\/span>\r\n<span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token punctuation\">{<\/span>\r\n\t<span class=\"token comment\">\/\/ Incorrect username<\/span>\r\n\t<span class=\"token keyword\">echo<\/span> <span class=\"token string\">'Incorrect username and\/or password!'<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token punctuation\">}<\/span><\/code><\/pre>\n<p>First, we need to check if the query has returned any results. If the\u00a0<i class=\"hl\">username<\/i>\u00a0doesn&#8217;t exist in the database then there would be no results.<\/p>\n<p>If the username exists, we can bind the results to both the\u00a0<i class=\"hl\">$id<\/i>\u00a0and\u00a0<i class=\"hl\">$password<\/i>\u00a0variables.<\/p>\n<p>Subsequently, we proceed to verify the password with the\u00a0<i class=\"hl\"><a href=\"https:\/\/php.net\/manual\/en\/function.password-verify.php\" target=\"_blank\" rel=\"noopener noreferrer\">password_verify<\/a><\/i>\u00a0function. Only passwords that were created with the\u00a0<i class=\"hl\"><a href=\"https:\/\/php.net\/manual\/en\/function.password-hash.php\" target=\"_blank\" rel=\"noopener noreferrer\">password_hash<\/a><\/i>\u00a0function will work.<\/p>\n<p>If you don&#8217;t want to use any password encryption method, you can simply replace the following code:<\/p>\n<div class=\"code-header\"><\/div>\n<pre class=\" language-php\"><code class=\" language-php\"><span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token function\">password_verify<\/span><span class=\"token punctuation\">(<\/span><span class=\"token global\">$_POST<\/span><span class=\"token punctuation\">[<\/span><span class=\"token string\">'password'<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token variable\">$password<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><\/code><\/pre>\n<p>With:<\/p>\n<div class=\"code-header\"><\/div>\n<pre class=\" language-php\"><code class=\" language-php\"><span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token global\">$_POST<\/span><span class=\"token punctuation\">[<\/span><span class=\"token string\">'password'<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">===<\/span> <span class=\"token variable\">$password<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><\/code><\/pre>\n<p>However, I don&#8217;t recommend removing the hashing functions because if somehow your database becomes exposed, all the passwords stored in the accounts table will also be exposed. In addition, the user will have a sense of privacy knowing their password is encrypted.<\/p>\n<p>Upon successful authentication from the user, session variables will be initialized and preserved until they&#8217;re destroyed by either logging out or the session expiring. These session variables are stored on the server and are associated with a session ID stored in the user&#8217;s browser. We&#8217;ll use these variables to determine whether the user is logged in or not and to associate the session variables with our retrieved MySQL database results.<\/p>\n<div class=\"tip\"><i class=\"fas fa-lightbulb\" aria-hidden=\"true\"><\/i><strong>Did you know?<\/strong>The session_regenerate_id() function will help prevent session hijacking as it regenerates the user&#8217;s session ID that is stored on the server and as a cookie in the browser.<\/p>\n<\/div>\n<p>The user cannot change the session variables in their browser and therefore you don&#8217;t need to be concerned about such matter. The only variable they can change is the encrypted session ID, which is used to associate the user with the server sessions.<\/p>\n<p>Now we can test the login system and make sure the authentication works correctly. Navigate to\u00a0<i>http:\/\/localhost\/phplogin\/index.html<\/i>\u00a0in your browser.<\/p>\n<p>Type in a random username and password, and click the login button. It should output an error that should look like the following:<\/p>\n<div class=\"browser\">\n<div><span class=\"url\">http:\/\/localhost\/phplogin\/authenticate.php<\/span><\/div>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/i0.wp.com\/codeshack.io\/web\/img\/phplogin\/authentication-incorrect-username.png?resize=750%2C489&#038;ssl=1\" alt=\"Authentication Incorrect Username PHP\" width=\"750\" height=\"489\" data-recalc-dims=\"1\" \/><\/div>\n<p>Don&#8217;t worry, it&#8217;s not broken! If we navigate back to our login form and enter\u00a0<i>test<\/i>\u00a0for both the username and password fields, the authentication page will look like the following:<\/p>\n<div class=\"browser\">\n<div><span class=\"url\">http:\/\/localhost\/phplogin\/authenticate.php<\/span><\/div>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/i0.wp.com\/codeshack.io\/web\/img\/phplogin\/authentication-loggedin.png?resize=750%2C489&#038;ssl=1\" alt=\"Authentication Loggedin PHP\" width=\"750\" height=\"489\" data-recalc-dims=\"1\" \/><\/div>\n<p>If you receive an error, make sure to double-check your code to make sure you haven&#8217;t missed anything or check if the\u00a0<i>test<\/i>\u00a0account exists in your database.<\/p>\n<div data-counters='false' data-style='square' data-size='small' data-url='https:\/\/hostscripter.com\/?p=1335' data-title='Part 4: Create your own Login System using PHP and MySql' class='linksalpha_container linksalpha_app_3'><a href='\/\/www.linksalpha.com\/share?network='facebook' class='linksalpha_icon_facebook'><\/a><a href='\/\/www.linksalpha.com\/share?network='twitter' class='linksalpha_icon_twitter'><\/a><a href='\/\/www.linksalpha.com\/share?network='googleplus' class='linksalpha_icon_googleplus'><\/a><a href='\/\/www.linksalpha.com\/share?network='mail' class='linksalpha_icon_mail'><\/a><\/div><div data-position='' data-url='https:\/\/hostscripter.com\/?p=1335' data-title='Part 4: Create your own Login System using PHP and MySql' class='linksalpha_container linksalpha_app_7'><a href='\/\/www.linksalpha.com\/share?network='facebook' class='linksalpha_icon_facebook'><\/a><a href='\/\/www.linksalpha.com\/share?network='twitter' class='linksalpha_icon_twitter'><\/a><a href='\/\/www.linksalpha.com\/share?network='googleplus' class='linksalpha_icon_googleplus'><\/a><a href='\/\/www.linksalpha.com\/share?network='mail' class='linksalpha_icon_mail'><\/a><\/div>","protected":false},"excerpt":{"rendered":"<p>4. Authenticating Users with PHP Now that we have our database setup, we can go ahead and start coding with PHP. We&#8217;re going to start with the authentication file, which [&hellip;]<\/p>\n","protected":false},"author":301,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[4],"tags":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9KaPo-lx","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":1329,"url":"https:\/\/hostscripter.com\/?p=1329","url_meta":{"origin":1335,"position":0},"title":"Part 1: Create your own Login System using PHP and MySql","author":"h05t5cr1pt3r","date":"November 28, 2022","format":false,"excerpt":"In this tutorial, I'll be teaching you how you can create your very own secure PHP login system. A login form is what your website's visitors can use to log in to your website to access restricted content, such as a profile page. We will leverage MySQL to retrieve account\u2026","rel":"","context":"In &quot;Blog&quot;","block_context":{"text":"Blog","link":"https:\/\/hostscripter.com\/?cat=4"},"img":{"alt_text":"Secure Login System with PHP and MySQL","src":"https:\/\/i0.wp.com\/codeshack.io\/web\/img\/posts\/secure-login-system-php-mysql.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/codeshack.io\/web\/img\/posts\/secure-login-system-php-mysql.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/codeshack.io\/web\/img\/posts\/secure-login-system-php-mysql.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/codeshack.io\/web\/img\/posts\/secure-login-system-php-mysql.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":1343,"url":"https:\/\/hostscripter.com\/?p=1343","url_meta":{"origin":1335,"position":1},"title":"Part 1: Create your own Registration System using PHP and MySql","author":"h05t5cr1pt3r","date":"November 28, 2022","format":false,"excerpt":"This tutorial is a follow up to our previous tutorial\u00a0Secure Login System with PHP and MySQL. In this tutorial, we'll be creating a secure registration form and implementing basic validation. A registration form is what your website's visitors can use to register their details, which will subsequently be stored in\u2026","rel":"","context":"In &quot;Blog&quot;","block_context":{"text":"Blog","link":"https:\/\/hostscripter.com\/?cat=4"},"img":{"alt_text":"Secure Registration System with PHP and MySQL","src":"https:\/\/i0.wp.com\/codeshack.io\/web\/img\/posts\/secure-registration-system-php-mysql.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/codeshack.io\/web\/img\/posts\/secure-registration-system-php-mysql.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/codeshack.io\/web\/img\/posts\/secure-registration-system-php-mysql.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/codeshack.io\/web\/img\/posts\/secure-registration-system-php-mysql.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":1341,"url":"https:\/\/hostscripter.com\/?p=1341","url_meta":{"origin":1335,"position":2},"title":"Part 7: Create your own Login System using PHP and MySql","author":"h05t5cr1pt3r","date":"November 28, 2022","format":false,"excerpt":"7. Creating the Logout Script Creating the logout script is straightforward. All you need to do is destroy the sessions that were declared in the authenticate file. Edit the\u00a0logout.php\u00a0file and add the following code: <?php session_start(); session_destroy(); \/\/ Redirect to the login page: header('Location: index.html'); ?> Initialize sessions, destroy them,\u2026","rel":"","context":"In &quot;Blog&quot;","block_context":{"text":"Blog","link":"https:\/\/hostscripter.com\/?cat=4"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1349,"url":"https:\/\/hostscripter.com\/?p=1349","url_meta":{"origin":1335,"position":3},"title":"Part 4: Create your own Registration System using PHP and MySql","author":"h05t5cr1pt3r","date":"November 28, 2022","format":false,"excerpt":"4. Registering Users with PHP & MySQL Now we need to create the registration file that will process the form fields, check for basic validation, and insert the new account into our database. The registration page will require a connection to our database and therefore we must include the necessary\u2026","rel":"","context":"In &quot;Blog&quot;","block_context":{"text":"Blog","link":"https:\/\/hostscripter.com\/?cat=4"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1337,"url":"https:\/\/hostscripter.com\/?p=1337","url_meta":{"origin":1335,"position":4},"title":"Part 5: Create your own Login System using PHP and MySql","author":"h05t5cr1pt3r","date":"November 28, 2022","format":false,"excerpt":"5. Creating the Home Page The home page will be the first page our users see when they've logged-in. The only way they can access this page is if they're logged-in, whereas if they aren't, they will be redirected back to the login page. Edit the\u00a0home.php\u00a0file and add the following\u2026","rel":"","context":"In &quot;Blog&quot;","block_context":{"text":"Blog","link":"https:\/\/hostscripter.com\/?cat=4"},"img":{"alt_text":"PHP Loggedin Home Page","src":"https:\/\/i0.wp.com\/codeshack.io\/web\/img\/phplogin\/loggedin-home-page.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/codeshack.io\/web\/img\/phplogin\/loggedin-home-page.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/codeshack.io\/web\/img\/phplogin\/loggedin-home-page.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/codeshack.io\/web\/img\/phplogin\/loggedin-home-page.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":1339,"url":"https:\/\/hostscripter.com\/?p=1339","url_meta":{"origin":1335,"position":5},"title":"Part 6: Create your own Login System using PHP and MySql","author":"h05t5cr1pt3r","date":"November 28, 2022","format":false,"excerpt":"6. Creating the Profile Page The profile page will display the account information for the logged-in user. Edit the\u00a0profile.php\u00a0file and add the following code: <?php \/\/ We need to use sessions, so you should always start sessions using the below code. session_start(); \/\/ If the user is not logged in\u2026","rel":"","context":"In &quot;Blog&quot;","block_context":{"text":"Blog","link":"https:\/\/hostscripter.com\/?cat=4"},"img":{"alt_text":"PHP Loggedin Profile Page","src":"https:\/\/i0.wp.com\/codeshack.io\/web\/img\/phplogin\/loggedin-profile-page.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/codeshack.io\/web\/img\/phplogin\/loggedin-profile-page.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/codeshack.io\/web\/img\/phplogin\/loggedin-profile-page.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/codeshack.io\/web\/img\/phplogin\/loggedin-profile-page.png?resize=700%2C400&ssl=1 2x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/hostscripter.com\/index.php?rest_route=\/wp\/v2\/posts\/1335"}],"collection":[{"href":"https:\/\/hostscripter.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hostscripter.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hostscripter.com\/index.php?rest_route=\/wp\/v2\/users\/301"}],"replies":[{"embeddable":true,"href":"https:\/\/hostscripter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1335"}],"version-history":[{"count":1,"href":"https:\/\/hostscripter.com\/index.php?rest_route=\/wp\/v2\/posts\/1335\/revisions"}],"predecessor-version":[{"id":1336,"href":"https:\/\/hostscripter.com\/index.php?rest_route=\/wp\/v2\/posts\/1335\/revisions\/1336"}],"wp:attachment":[{"href":"https:\/\/hostscripter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hostscripter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hostscripter.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}