This is Part 1 of my PHP Login System tutorial. By the end of this series, you’ll have your own custom social networking site. Users will be able to register, login, edit their profile and add pictures. We’re aiming for a very stripped down version of Facebook and Myspace.
For this tutorial you’ll need a webserver running PHP and mySQL. Click here for instructions on how to set one up on your local machine with XAMPP.
Lets start by making our database. Our users table will have 7 fields.
id, first, last, username, password, email and about.
Heres the SQL code to make this happen. You can paste it into your phpmyadmin SQL tab. Make sure you change the `db` in the first line to match the name of your database.
CREATE TABLE `db`.`users` ( `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY , `first` VARCHAR( 32 ) NOT NULL , `last` VARCHAR( 32 ) NOT NULL , `username` VARCHAR(32) NOT NULL, `password` VARCHAR(255) NOT NULL, `email` VARCHAR(255) NOT NULL, `about` TEXT NOT NULL ) ENGINE = MYISAM;
We make the id auto_increment so that every user that is added to the db has their own id. Since this field will be unique, we’ll make it our primary key. It will be used to reference each user later.
Our database is setup. We just need to connect to it using php. We’ll create our first php file, “db_connect.php”.
PHP has built in functions to interact with mysql databases, mysql_connect and mysql_select_db.
The mysql_connect function takes 3 arguments, the host, username, and password. We’ll store the result of this function into a variable. If the connect fails you tell it to die and show the error output on the screen using mysql_error().
If your connection is successful, you just have to select the database you want to
interact with. The mysql_select_db(“name_of_your_db”,”the_connection_resource”) function will select your database from the successful connection we established previously.
Heres the php code
<?php $con = mysql_connect(localhost,"username","password") or die(mysql_error()); $db = mysql_select_db("db",$con); ?>
We’ve connected to the mysql server and selected our db.
Im going to add a function into this page that we’ll be using later
function protect($string) { $string = mysql_real_escape_string($string); return $string; }
This function will clean what the users submit to the database so that we won’t need to worry about SQL Injection. I made it a function because typing “mysql_real_escape_string” every time you need to use it is a pain.
The entire db_connect.php code
<?php //db_connect.php $con = mysql_connect(localhost,"username","password") or die(mysql_error()); $db = mysql_select_db("db",$con); function protect($string) { $string = mysql_real_escape_string($string); return $string; } ?>
Next will be Part 2 where we create the Login and Registration Page.


#1 by Stefanos on March 24, 2009 - 3:32 am
Five stars is not enough for this tutorial ! Great work from Bhavik ! He knows the subject and more knows to teach that . Thank you !
#2 by Bhavik on March 24, 2009 - 5:57 am
Haha, I don’t think I know it that well
Thank you for the kind words!
#3 by Sasori on April 8, 2009 - 8:45 am
I have searched and work my head as I am a novie at this to find a tutorial that is has an easy walkthrough without the thinking that the reader already knows it. I almost gave until i stumbble on yours. thanks a lot. Am still going through it with the files that you provided before I start to implement mine.
thanks again
#4 by Bhavik on April 8, 2009 - 6:32 pm
Your welcome Sasori, Let me know if you need help with anything in particular. Thanks for reading!
#5 by Rashid Haddadin on April 21, 2009 - 12:37 pm
Well What Could I Say
I Can’t Say good neither amazing I Need new word to write down AMAZING GREATE TUTORIAL I Have Just Checked Both Part 1 and Part 2 Still checkin
#6 by Bhavik on April 21, 2009 - 10:18 pm
I appreciate your kind comments Rashid. Thanks for reading!
#7 by Abraham Cuenca on April 22, 2009 - 2:21 pm
You sir, are a genius, a gentleman and a scholar. By far the best PHP Login System tutorial I have ever found.
Thank you a million times over. (do you have a paypal where I can donate at least a few bucks?)
#8 by Bhavik on April 22, 2009 - 6:16 pm
Abraham,
Thank you so much for your extremely kind words. I’m glad the tutorial worked well for you. You can donate at http://bhaviksblog.com/donate. Your contribution will help buy books for next term! Thank you so much again Abraham. Take care and let me know if you need anything else.
#9 by Justin on May 20, 2009 - 3:34 am
This is such a gift from above
Am definitely going to take my time and go through it. When am done I’ll come and post my website here so you can see
#10 by James on May 18, 2010 - 7:28 am
Very good website tutorial
#11 by VS on June 10, 2010 - 3:03 pm
You rock, man!
Keep posting!
#12 by Kate on July 26, 2010 - 12:49 pm
Hi I have found this information really helpful; however, I have one question if any could answer it, it would be of great help
Do I need a webserver or could I use this code using basic dreaweaver?
#13 by Karl on July 28, 2010 - 3:11 pm
Kate, you would need a webserver with PHP.
#14 by addisinge on July 30, 2010 - 1:47 pm
Does anyone have knowledge of a obedient place to find baby gifts? I desire to allow a pygmy belittle doll with my nephews name on it for his christening. I have set up some really well-mannered sites, representing model iul.ie – baby girl gifts , but If anyone can recomend safer I would apreciate it. Thanks!
#15 by RRF on August 13, 2010 - 1:26 pm
Pygmy belittle dolls ? I use those, but only for the president, congress, and bankers. These are too harsh for just everybody.
Nice tutorial Bhavik. I’m also curious what software or package you are using to power your blog. Is this some php package ? Thanks.
#16 by gostkeyleyla on August 17, 2010 - 5:55 am
установка замка акпп выполняется на специальном оборудовании с применением современных процессов с обеспечением скидки
#17 by Frank on August 18, 2010 - 11:53 am
I’ve downloaded the source code, but am struggling a little bit. I can get the index.php running, but when try to register it fails.