Building a Simple Secure Login and Registration System with PHP and MySQL

In one of the preceding articles, we have seen how to write a simple static login system with PHP without using database.  In this article, we are going to get into how to build a simple dynamic login and registration system as simple as securely with PHP and MySQL. In this article, we will follow PDO approach to interact with database to keep the connection safe and secure thus preventing from attacks such as SQL injection.


In functions.php , we have organized all of our required common functionalities in one place to keep the code clean and organized thus enabling us to maintain the code easier and reusable. This way of organizing is a sort of refactoring our unmanageable code to avoid code smell. In our prospective articles, we will be showing how to make it even more reusable and maintainable code using object-oriented paradigm.

We will use functions.php in rest of our php scripts to avail our standard functionalities such as logging user in, logging user out, registering user, checking if user is logged in and redirecting urls.





We are using flags in our urls for login and register scripts to indicate the status of the actions. For instance, register.php?f=1 indicates success whereas register.php?f=2 indicates failure. We use this way of status indication to show flash messages to our users.


We use password securing mechanisms to hash the password as strong as possible with the default salt generated automatically and the cost of the algorithm is 12. We don’t recommend specifying the cost of the algorithm higher than 12 depending upon the hardware efficiency.

We also use MySQL prepared statements to avoid SQL Injection attacks using PHP Data Objects shortly as PDO.


We have reached the end of the article. Hope you find this article interesting and helpful for your projects. If you have any queries about the article or you want us to write an article of your choice, please drop them in the comments section. We are glad to hear from you and make efforts best way possible.

If you realize that the article needs corrections or improvements, please feel free to post them in the comments section.

Thanks! We will get back soon with our new articles.


We have attached our project files to the article. Click here to download it!