How to encrypt user info with php

June 16th, 2008 at 02:22pm Under PHP

If you run a serious webpage where you save login information for your members to a database it is generally a very good idea to perform some kind of encryption on these password to prevent the information to be shared in case your datebase would be hacked.

Encryption is very easy to do with PHP in fact all you need to perform a “one way encryption” is the function crypt(). As an example say that we have the variables $user and $password and we want to encrypt the $password variable before we store it in the database. To do this we use the following function:


$crypted_pass = crypt(md5($password),md5($user));

What this does is that it generates an encrypted string from the md5 encoded $password with the $user string as security salt and voila we have an encrypted string ready to be saved to the database. This string can not be decrypted so if we want to use it to verify if someone typed in a correct password for a specific user we need to encode the input in the same way and compare it to the encrypted password.


$try_password = crypt(md5($password),md5($user));
  if($crypted_pass == $try_password)
   echo "success";
else
   echo "wrong password";

Now with the passwords encrypted we will buy enough time to be able to change everyones user info in case of the database being hacked and the information leaked.

By Stefan 7 comments

Building your own Myspace.com with PHP part VI: Finishing touches

July 31st, 2007 at 09:40pm Under PHP+ Tutorials

We have learned by now how to register a user and login, how to display and edit a presentation, how to add friends and search for new friends. In this last part of the tutorial we are going to finish it up with a guestbook where the visitor to a presentation can leave a message for the owner.

What is left to do now is tie it all together with a nice layout I have made a simple interface for all the code in this tutorial that you are free to use for inspiration, you can download it here. I also have a demo available online.

There are many improvements and feautures that could be added to this project but I will leave that up to you :). Here are some ideas of possible extensions.

    Private messaging
    Who is online
    Making friend request go both ways

If you have any suggestions on improvements for this tutorial or have any comments in general either post them as comments or send me an e-mail at stefan [at] upgradetheweb.com

By Stefan 13 comments

Building your own Myspace.com with PHP part V: Friends and guestbook

July 31st, 2007 at 09:00pm Under MySQL+ PHP+ Tutorials

What is a community without friends? Not much really… So thats why this tutorial will cover how to add friends to the users presentation as bookmarks. We will also go over how to search the database for new friends to add to your list.
(more…)

By Stefan 2 comments

Building your own Myspace.com with PHP part IV: Presentation

July 31st, 2007 at 08:59pm Under MySQL+ PHP+ Tutorials

In the previous tutorial we learned how to register a new user and how to create a login system with php sessions. With this done we can now move on to the users personal presentation page. So in this tutorial we will first go over how to display a presentation and then how to let the user edit his own page.
(more…)

By Stefan 1 comment

Building your own Myspace.com with PHP Part III: Register and log in with sessions

July 31st, 2007 at 08:58pm Under PHP+ Tutorials

In this third part of the tutorial we are going to create an important part of the application. We will learn how to register a new user and save this user to the database, when a user is created it should of course be possible for him to log in. We will accomplish the login part by using session variables.

Well lets start with how to register a user. The first thing we will need is some text boxes with html to put in the desired username and password.
(more…)

By Stefan 4 comments

Building your own myspace.com with PHP Part II: Designing the database

July 31st, 2007 at 08:57pm Under MySQL+ PHP+ Tutorials

In this part of the tutorial we are going to create the back end database that will hold all the information that we need to save for our script. We will create the table structure to prepare the database for the information we want it to hold. We will also create a wrapper class for MySQL that we will use to make our database calls as painless and clean as possible.
(more…)

By Stefan 7 comments

Building your own Myspace.com with PHP Part I: Introduction

July 31st, 2007 at 08:56pm Under PHP+ Tutorials

In this series of tutorials we are going to go through the development of a complete (yet very basic) community system. The final community will allow visitors to sign up and get a personal presentation page, guestbook and a friends list. I have tried to keep the programming as simple as possible and will explain what im trying to do as throughly as possible. It should be fairly easy to follow even if you are very new to programming web applications.

The script will be developed with PHP and we will use MySQL as the database of choice. A complete example of what you will learn to create by following this series of tutorial is available at the following link. You can login with test/test to try it out!. If you want to download the source files they are available here.

Table of contents:

Next part: Designing the database

By Stefan 12 comments


Recent Blog Posts

Categories

Posts by Month

Blogroll

RSS Feeds