PHP login script with encryption

In this php tutorial i want to explain how to make a secure php login script with encryption, how to inserted encrypted password when registration and accessing the same during login.
In this php login script i used md5 encryption method to secure the password from others. Also some others encryption technique are available in php like base64_ encode, sha1, password hash etc. Now a days mostly use hash techniques. But i will discuss in my upcoming post. This post is just for very beginner level and an introduction about encryption with md5. This php tutorial is more advance and secure than my previous login script PHP login script tutorial. Take a quick look at this live demo with username – demo, pass – demo to understand the flow of the PHP login script.

Creating the Database

CREATE TABLE user
(
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(255) UNIQUE,
password VARCHAR(255)
);

create dbconfig.php for connecting with the database

<?php
$host = 'localhost';
$username = 'username';
$password = 'password';
$database = 'database';
$dbconfig = mysqli_connect($host,$username,$password,$database);
?>

registration.php

<?php
include("dbconfig.php");
if($_SERVER["REQUEST_METHOD"] == "POST")
{
// username and password 
$username=mysqli_real_escape_string($dbconfig,$_POST['username']); 
$password=mysqli_real_escape_string($dbconfig,$_POST['password']); 
$password=md5($password); // Encrypted Password with md5
$sql="Insert into user(username,password) values('$username','$password')";
$result=mysqli_query($dbconfig,$sql);
$msg="Registration Successfully";
}
?>

After successful registration you can check that the password field in database table hold the encrypted value.

Now we have to create the login page.
login.php

<?php
include("dbconfig.php");
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST")
{
// username and password received from loginform
$username=mysqli_real_escape_string($dbconfig,$_POST['username']);
$password=mysqli_real_escape_string($dbconfig,$_POST['password']);
$password=md5($password);//convert into md5 encrypted password
$sql_query="SELECT id FROM user WHERE username='$username' and password='$password'";
$result=mysqli_query($dbconfig,$sql_query);
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
$count=mysqli_num_rows($result);// If result matched $username and $password, table row must be 1 row
if($count==1)
{
$_SESSION['login_user']=$username;
header("location: welcome-home.php");
}
else
{
$error="Username or Password is invalid";
}
}
?>

welcome-home.php
Session verification. If no session value page redirect to login.php and if session exist then show welcome screen.

if(!isset($_SESSION['login_user']))
{
header("Location: login.php");
}
else
{
$login_session=$_SESSION['login_user'];
?>
Welcome <?php echo $login_session;?>
<?php
}
?>

logout.php
Logged out with session destroy – destroy all the session when logout link click by user and redirect to login.php.

<?php
session_start();
if(session_destroy())
{
header("Location: login.php");
}
?>

Thanks for watching this tutorial. You can try yourself. I will share some useful script in PHP as well as some tips and tricks in PHP programming. Viewers can subscribe our newsletter to get all the updates about web programming.

28 responses to “PHP login script with encryption”

  1. Anonymous says:

    fine post,it is useful to me and others,please just keep it on….

  2. bonousa.com says:

    Hello! I just wish to offer you a big thumbs up for your excellent
    information you have right here on this post. I am coming back to your website for
    more soon.

  3. Anonymous says:

    I believe that is one of the so much vital info
    for me. And i am satisfied reading your article.

    But wanna observation on few basic things, The site style is ideal,
    the articles is really nice : D. Just right job, cheers

  4. This blog is quite interesting , and very seo friendly.

  5. Guest says:

    Helpful information. Fortunate me I found your site by accident, and I am stunned why this coincidence didn’t came about earlier! I bookmarked it.

  6. Looptr says:

    Great topic. I needs to spend some time learning much more or working out more. Thanks for fantastic information I was in search of this info for my mission.

  7. Guest says:

    I think the article is very helpful for people,it has solved my problem,thanks!

  8. Hamer says:

    This is something I actually have to try and do a lot of analysis into, thanks for the post.

  9. Guest says:

    Thanks for the good write up. However, how can we be in contact?

  10. murrayray says:

    First of all I would like to say great blog!
    I had a quick question in which I’d like to ask if you do not mind.
    I was interested to find out how you center yourself and clear your head before writing.
    I have had difficulty clearing my mind in getting
    my ideas out. I do enjoy writing but it just seems like the first 10 to 15 minutes are usually wasted just trying to figure out how to begin. Any ideas or tips?

    Appreciate it!

  11. darcimedford says:

    I’m gone to tell my little brother, that he should also visit this web
    site on regular basis to get updated from newest reports.

  12. quest bars says:

    It’s amazing to pay a quick visit this web page and reading the views of all mates about this article, while I am also zealous of getting knowledge.

  13. KirbyPMort says:

    Hi it’s me, I am also visiting this web site on a regular basis, this website is in fact pleasant and the users are truly sharing good thoughts.

  14. Petra marion says:

    Saved as a favorite, I love your web site!

  15. Guest says:

    After all, what a great site and informative posts, I will upload inbound link – bookmark this web site? Regards, Reader.

  16. MyrlSMcgavin says:

    Just desire to say your article is as surprising. The clearness in your post is simply excellent and i could assume you are an expert on this subject.
    Well with your permission let me to grab your RSS feed to keep updated
    with forthcoming post. Thanks a million and please carry on the enjoyable work.

  17. Venus says:

    Awsome info and right to the point. Thx 🙂

  18. Carpesa says:

    Fantastic put up, very informative. I wonder why the other specialists of this sector do not notice this. You should continue your writing. I am sure, you have a huge readers’ base already!

  19. Projektnatet says:

    I every time emailed this blog post page to all my friends, for the reason that i like to read it then my friends will too.

  20. Guest says:

    You are a very bright individual!

  21. Jospeh Carrousal says:

    There are certainly a lot of details like that to take into consideration. That is a great point to bring up. I offer the thoughts above as general inspiration but clearly there are questions like the one you bring up where the most important thing will be working in honest good faith. I don?t know if best practices have emerged around things like that, but I am sure that your job is clearly identified as a fair game. Both boys and girls feel the impact of just a moment’s pleasure, for the rest of their lives.

  22. Doyle says:

    Hello, I read your blog on a regular basis. Your humoristic style is awesome, keep
    it up!

  23. Malesin.us says:

    Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *