{"id":1341,"date":"2022-11-28T08:46:45","date_gmt":"2022-11-28T00:46:45","guid":{"rendered":"https:\/\/hostscripter.com\/?p=1341"},"modified":"2022-11-28T08:46:45","modified_gmt":"2022-11-28T00:46:45","slug":"part-7-create-your-own-login-system-using-php-and-mysql","status":"publish","type":"post","link":"https:\/\/hostscripter.com\/?p=1341","title":{"rendered":"Part 7: Create your own Login System using PHP and MySql"},"content":{"rendered":"<h2 id=\"creatingthelogoutscript\">7. Creating the Logout Script<\/h2>\n<p>Creating the logout script is straightforward. All you need to do is destroy the sessions that were declared in the authenticate file.<\/p>\n<p>Edit the\u00a0<i>logout.php<\/i>\u00a0file and add the following code:<\/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 function\">session_destroy<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token comment\">\/\/ Redirect to the login page:<\/span>\r\n<span class=\"token function\">header<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">'Location: index.html'<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token delimiter important\">?&gt;<\/span><\/span><\/code><\/pre>\n<p>Initialize sessions, destroy them, and redirect the user to the login page. We use sessions to determine whether the user is logged in or not, so by removing them, the user will not be logged in.<\/p>\n<h2>Conclusion<\/h2>\n<p>You should now have a basic understanding of how a login system works with PHP and MySQL. You&#8217;re free to use the source code and incorporate it into your own projects.<\/p>\n<p>The next step is to create a registration system that will enable visitors to register.<\/p>\n<p>Don&#8217;t forget to follow us and share the article as it will help us create future tutorials and update existing content with new features.<\/p>\n<p>Next tutorial in this series:\u00a0<strong>Secure Registration System with PHP and MySQL<\/strong><\/p>\n<div data-counters='false' data-style='square' data-size='small' data-url='https:\/\/hostscripter.com\/?p=1341' data-title='Part 7: 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=1341' data-title='Part 7: 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>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 [&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-lD","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":1339,"url":"https:\/\/hostscripter.com\/?p=1339","url_meta":{"origin":1341,"position":0},"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":[]},{"id":1337,"url":"https:\/\/hostscripter.com\/?p=1337","url_meta":{"origin":1341,"position":1},"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":1343,"url":"https:\/\/hostscripter.com\/?p=1343","url_meta":{"origin":1341,"position":2},"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":1335,"url":"https:\/\/hostscripter.com\/?p=1335","url_meta":{"origin":1341,"position":3},"title":"Part 4: Create your own Login System using PHP and MySql","author":"h05t5cr1pt3r","date":"November 28, 2022","format":false,"excerpt":"4. Authenticating Users with PHP Now that we have our database setup, we can go ahead and start coding with PHP. We're going to start with the authentication file, which will process and validate the form data that we'll send from our\u00a0index.html\u00a0file. Edit the\u00a0authenticate.php\u00a0file and add the following: <?php session_start();\u2026","rel":"","context":"In &quot;Blog&quot;","block_context":{"text":"Blog","link":"https:\/\/hostscripter.com\/?cat=4"},"img":{"alt_text":"Authentication Incorrect Username PHP","src":"https:\/\/i0.wp.com\/codeshack.io\/web\/img\/phplogin\/authentication-incorrect-username.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/codeshack.io\/web\/img\/phplogin\/authentication-incorrect-username.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/codeshack.io\/web\/img\/phplogin\/authentication-incorrect-username.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/codeshack.io\/web\/img\/phplogin\/authentication-incorrect-username.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":1329,"url":"https:\/\/hostscripter.com\/?p=1329","url_meta":{"origin":1341,"position":4},"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":1331,"url":"https:\/\/hostscripter.com\/?p=1331","url_meta":{"origin":1341,"position":5},"title":"Part 2: Create your own Login System using PHP and MySql","author":"h05t5cr1pt3r","date":"November 28, 2022","format":false,"excerpt":"2. Creating the Login Form Design We will now create a form that our users can use to enter their details and submit them for processing. We will be using HTML and CSS for this part of the tutorial as PHP will not be necessary on this page. Edit the\u00a0index.html\u00a0file\u2026","rel":"","context":"In &quot;Blog&quot;","block_context":{"text":"Blog","link":"https:\/\/hostscripter.com\/?cat=4"},"img":{"alt_text":"Basic HTML Login Form Layout","src":"https:\/\/i0.wp.com\/codeshack.io\/web\/img\/phplogin\/basic-html-login-form-layout.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/codeshack.io\/web\/img\/phplogin\/basic-html-login-form-layout.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/codeshack.io\/web\/img\/phplogin\/basic-html-login-form-layout.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/codeshack.io\/web\/img\/phplogin\/basic-html-login-form-layout.png?resize=700%2C400&ssl=1 2x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/hostscripter.com\/index.php?rest_route=\/wp\/v2\/posts\/1341"}],"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=1341"}],"version-history":[{"count":1,"href":"https:\/\/hostscripter.com\/index.php?rest_route=\/wp\/v2\/posts\/1341\/revisions"}],"predecessor-version":[{"id":1342,"href":"https:\/\/hostscripter.com\/index.php?rest_route=\/wp\/v2\/posts\/1341\/revisions\/1342"}],"wp:attachment":[{"href":"https:\/\/hostscripter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hostscripter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1341"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hostscripter.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}