PHP Tutorials

XMLStyleSoldierFlowerMonkey

Signup and Login

Learn how to have a user database where people can sign up and log in.

Spreadshirt
So you're ready to make your site a community and want to have a username and password entry for users? Well, this tutorial will show you how to easily make a script for users to create a username and password. The second part of the tutorial will show you how to have them log in the next time they come. You can see all these files for what I created and tested at: signup.txt, make.txt, login.txt, getin.txt. Let's get started.

PART I - CREATING THE REGISTRATION
First and foremost, for this tutorial, you're going to need a *nix server running PHP, mySQL, and phpMyAdmin. First, we need to create a mySQL table which will store the user's name and password when they create it. So, log on to phpMyAdmin, and select your database. My database is called "spoono_db". Inside the database, we're going to have to create a table, so click the link of "spoono_db" on the left frame of phpMyAdmin and go to the bottom of the right frame where it displays "Create Table". We'll call it "users" and have 4 columns for it.
The three columns for the "users" table inside the "spoono_db" are:
  1. id - which will be an int (auto increment, primary)
  2. username - which will be text
  3. password - which will be text
  4. email - which will be text
Alright, so the table is created. Now, the next thing we have to do is create the HTML form where people can create their username and password. Create a blank file, copy the following code, and save it as "signup.html". Now here's the code for the form:
<form action="make.php" method="post">
Username Desired: <input type="text" name="username" size="10">
Password Desired: <input type="password" name="password" size="10">
E-mail: <input type="text" name="email" size="10">
<input type="submit" value="submit" name="submit">
</form>

Ok, that's a simple form which asks for the user to create a name and password. As you can see by the first line, the action of the form takes them to "make.php". So let's create a blank file called "make.php" and put this code:
<?
//replace username and password with your mysql name and password
$conn = mysql_connect("localhost","USERNAME","PASSWORD");

//select the database
$db = mysql_select_db("spoono_db");

$username = $_POST["username"];
$password = $_POST["password"];
$email = $_POST["email"];

//insert the values
$result= MYSQL_QUERY("INSERT INTO users (id, username, password, email)".
   "VALUES ('NULL', '$username', '$password', '$email')");
   
echo "Your name and password have been submitted into our database :)";
?> 

At first this code may look scary, but it's pretty simple. The first two lines connect to your mySQL database. For USERNAME and PASSWORD, put in your phpMyAdmin/mySQL username and password. The second line chooses the database where our table is stores, so I chose "spoono_db". The third line runs the mySQL query and inserts into the "users" table we made earlier the values of the name/password the person entered. Finally, the last line spits out a message saying their name was submitted.

PART II - LOGGING IN WITH NAME AND PASSWORD
So now, your visitor has successfully created a name and password. Now, how does he log in? Well, first he has to go to a form and put his name and password. This will be almost identical to the signup.html. So create a file called "login.html" and place this code:
<form action="getin.php" method="post">
Username: <input type="text" name="username" size="10">
Password: <input type="password" name="password" size="10">
<input type="submit" value="submit" name="submit">
</form>

Again, it's identical to the signup.html except the action is now to "getin.php". So, the final step is to make the "getin.php". So create a new file and save it as "getin.php" and place this code:
<?
$conn = mysql_connect("localhost","USERNAME","PASSWORD");
$db = mysql_select_db("spoono_db");

$username = $_POST["username"];
$password = $_POST["password"];

$result = MYSQL_QUERY("SELECT * from users WHERE username='$username'and password='$password'")
   or die ("Name and password not found or not matched");

$worked = mysql_fetch_array($result);

$username = $worked[username];
$password = $worked[password];
$email = $worked[email];

if($worked)
   echo "Welcome $user! Your e-mail address is $email"; 
?>

Check out that hunk of code :) Alright, you should be familiar with the first two lines which just logs in and selects the database. The $result variable tries to select the name and password which match the ones they submitted. If it does, it goes on to the next line; if it doesn't, the code dies there and says the name wasn't found. Now, if it is found, the database returns the array with the name and password. You can see all these files for what I created and tested at: signup.txt, make.txt, login.txt, getin.txt. Pretty cool, eh? Hope this tutorial helps in making your community.

Discuss this tutorial »
Written by: Akash Goel
Back to PHP TutorialsTop


Copyright © 2000-2008 Spoono, LLC. All rights reserved.
Network: Reseller Web Hosting by Spoono Host | Spoonloads | Absolute Cross
Terms of Service | Privacy Policy.

kdfj