{"id":1329,"date":"2022-11-28T07:44:03","date_gmt":"2022-11-27T23:44:03","guid":{"rendered":"https:\/\/hostscripter.com\/?p=1329"},"modified":"2022-11-28T07:44:03","modified_gmt":"2022-11-27T23:44:03","slug":"part-1-create-your-own-login-system-using-php-and-mysql","status":"publish","type":"post","link":"https:\/\/hostscripter.com\/?p=1329","title":{"rendered":"Part 1: Create your own Login System using PHP and MySql"},"content":{"rendered":"<p><img decoding=\"async\" loading=\"lazy\" class=\"featuredimage\" src=\"https:\/\/i0.wp.com\/codeshack.io\/web\/img\/posts\/secure-login-system-php-mysql.png?resize=770%2C400&#038;ssl=1\" alt=\"Secure Login System with PHP and MySQL\" width=\"770\" height=\"400\" data-recalc-dims=\"1\" \/><\/p>\n<div class=\"content m100 mpadtb\">\n<p>In this tutorial, I&#8217;ll be teaching you how you can create your very own secure PHP login system. A login form is what your website&#8217;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 data from the database.<\/p>\n<h2 id=\"gettingstarted\">1. Getting Started<\/h2>\n<p>There are a few steps we need to take before we create our secure login system. We need to set up our web server environment and ensure we have the required extensions enabled.<\/p>\n<h3 id=\"requirements\">1.1. Requirements<\/h3>\n<ul>\n<li>If you haven&#8217;t got a local web server set-up, I recommend you download and install\u00a0<a class=\"l1\" href=\"https:\/\/www.apachefriends.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">XAMPP<\/a>.<\/li>\n<li>XAMPP is a cross-platform web server package that includes the essentials for back-end developers. It includes PHP, MySQL, Apache, phpMyAdmin, and more. It&#8217;s not necessary to install all the software separately with XAMPP.<\/li>\n<\/ul>\n<h3 id=\"whatyouwilllearn\">1.2. What You Will Learn in this Tutorial<\/h3>\n<ul>\n<li><b>Form Design<\/b>\u00a0\u2014 Design a login form with HTML5 and CSS3.<\/li>\n<li><b>Prepared SQL Queries<\/b>\u00a0\u2014 How to properly prepare SQL queries to prevent SQL injection and therefore preventing your database from being exposed.<\/li>\n<li><b>Basic Validation<\/b>\u00a0\u2014 Validating form data that is sent to the server using GET and POST requests (username, password, email, etc.).<\/li>\n<li><b>Session Management<\/b>\u00a0\u2014 Initialize sessions and store retrieved database results. Sessions are saved on the server and are associated with a unique ID that is saved in the browser.<\/li>\n<\/ul>\n<h3 id=\"filestructuresetup\">1.3. File Structure &amp; Setup<\/h3>\n<p>We can now start our web server and create the files and directories we&#8217;re going to use for our login system.<\/p>\n<ul>\n<li>Open\u00a0<i>XAMPP Control Panel<\/i><\/li>\n<li>Next to the Apache module click\u00a0<i>Start<\/i><\/li>\n<li>Next to the MySQL module click\u00a0<i>Start<\/i><\/li>\n<li>Navigate to XAMPP&#8217;s installation directory (<i>C:\\xampp<\/i>)<\/li>\n<li>Open the\u00a0<i>htdocs<\/i>\u00a0directory<\/li>\n<li>Create the following directories and files:<\/li>\n<\/ul>\n<div class=\"contents fs\">\n<h2>File Structure<\/h2>\n<p>\\&#8211; phplogin<br \/>\n|&#8211; index.html<br \/>\n|&#8211; style.css<br \/>\n|&#8211; authenticate.php<br \/>\n|&#8211; logout.php<br \/>\n|&#8211; home.php<br \/>\n|&#8211; profile.php<\/p>\n<\/div>\n<p>Each file will consist of the following:<\/p>\n<ul>\n<li><i>index.html<\/i>\u00a0\u2014 The login form created with HTML5 and CSS3. We don&#8217;t need to use PHP in this file. Therefore, we can save it as plain HTML.<\/li>\n<li><i>style.css<\/i>\u00a0\u2014 The stylesheet (CSS3) for our secure login system.<\/li>\n<li><i>authenticate.php<\/i>\u00a0\u2014 Authenticate users, connect to the database, validate form data, retrieve database results, and create new sessions.<\/li>\n<li><i>logout.php<\/i>\u00a0\u2014 Destroy the logged-in sessions and redirect the user to the login page.<\/li>\n<li><i>home.php<\/i>\u00a0\u2014 Basic home page for logged-in users.<\/li>\n<li><i>profile.php<\/i>\u00a0\u2014 Retrieve the user&#8217;s account details from our MySQL database and populate them with PHP and HTML.<\/li>\n<\/ul>\n<\/div>\n<div data-counters='false' data-style='square' data-size='small' data-url='https:\/\/hostscripter.com\/?p=1329' data-title='Part 1: 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=1329' data-title='Part 1: 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>In this tutorial, I&#8217;ll be teaching you how you can create your very own secure PHP login system. A login form is what your website&#8217;s visitors can use to log [&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-lr","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":1343,"url":"https:\/\/hostscripter.com\/?p=1343","url_meta":{"origin":1329,"position":0},"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":1329,"position":1},"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":1354,"url":"https:\/\/hostscripter.com\/?p=1354","url_meta":{"origin":1329,"position":2},"title":"Part 6: Create your own Registration System using PHP and MySql","author":"h05t5cr1pt3r","date":"November 28, 2022","format":false,"excerpt":"6. Implementing Account Activation The account activation system will send an email to the user with the activation link when the user has registered. The first thing we need to do is to go into\u00a0phpMyAdmin\u00a0and select our database, in our case this would be\u00a0phplogin, you can either add the column\u00a0activation_code\u00a0to\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":1347,"url":"https:\/\/hostscripter.com\/?p=1347","url_meta":{"origin":1329,"position":3},"title":"Part 3: Create your own Registration System using PHP and MySql","author":"h05t5cr1pt3r","date":"November 28, 2022","format":false,"excerpt":"3. Creating the Database and setting-up Tables You can skip this step if you followed the\u00a0Secure Login System Tutorial. For this part, you will need to access your MySQL database, either using\u00a0phpMyAdmin\u00a0or your preferred MySQL database management application. If you're using\u00a0phpMyAdmin\u00a0then follow these instructions: Navigate to:\u00a0http:\/\/localhost\/phpmyadmin\/ Click the\u00a0Databases\u00a0tab at the\u2026","rel":"","context":"In &quot;Blog&quot;","block_context":{"text":"Blog","link":"https:\/\/hostscripter.com\/?cat=4"},"img":{"alt_text":"phpMyAdmin Accounts Table","src":"https:\/\/i0.wp.com\/codeshack.io\/web\/img\/phplogin\/phpmyadmin-accounts-table.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/codeshack.io\/web\/img\/phplogin\/phpmyadmin-accounts-table.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/codeshack.io\/web\/img\/phplogin\/phpmyadmin-accounts-table.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/codeshack.io\/web\/img\/phplogin\/phpmyadmin-accounts-table.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":1333,"url":"https:\/\/hostscripter.com\/?p=1333","url_meta":{"origin":1329,"position":4},"title":"Part 3: Create your own Login System using PHP and MySql","author":"h05t5cr1pt3r","date":"November 28, 2022","format":false,"excerpt":"3. Creating the Database and setting-up Tables For this part, you will need to access your MySQL database, either using\u00a0phpMyAdmin\u00a0or your preferred MySQL database management application. Follow the below instructions if you're using\u00a0phpMyAdmin. Navigate to:\u00a0http:\/\/localhost\/phpmyadmin\/ Click the\u00a0Databases\u00a0tab at the top Under\u00a0Create database, enter\u00a0phplogin\u00a0in the text box Select\u00a0utf8_general_ci\u00a0as the collation Click\u00a0Create\u2026","rel":"","context":"In &quot;Blog&quot;","block_context":{"text":"Blog","link":"https:\/\/hostscripter.com\/?cat=4"},"img":{"alt_text":"phpMyAdmin Accounts Table","src":"https:\/\/i0.wp.com\/codeshack.io\/web\/img\/phplogin\/phpmyadmin-accounts-table.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/codeshack.io\/web\/img\/phplogin\/phpmyadmin-accounts-table.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/codeshack.io\/web\/img\/phplogin\/phpmyadmin-accounts-table.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/codeshack.io\/web\/img\/phplogin\/phpmyadmin-accounts-table.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":1335,"url":"https:\/\/hostscripter.com\/?p=1335","url_meta":{"origin":1329,"position":5},"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":[]}],"_links":{"self":[{"href":"https:\/\/hostscripter.com\/index.php?rest_route=\/wp\/v2\/posts\/1329"}],"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=1329"}],"version-history":[{"count":1,"href":"https:\/\/hostscripter.com\/index.php?rest_route=\/wp\/v2\/posts\/1329\/revisions"}],"predecessor-version":[{"id":1330,"href":"https:\/\/hostscripter.com\/index.php?rest_route=\/wp\/v2\/posts\/1329\/revisions\/1330"}],"wp:attachment":[{"href":"https:\/\/hostscripter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hostscripter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hostscripter.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}