Developing a Login System with PHP and MySQL - Webmaster Forum
Webmaster Forum

wmp banner

User Name
Password
Go Back   Webmaster Forum > WebmasterPost > Tutorials
Reply
01-09-2005, 10:14 PM   #1
daboss

WP V.I.P."The Boss"
 
Join Date: Jul 2004
Location: Here
Posts: 1,269

Forum$: 109.00 (Donate)
Bank: 0.00
Total Forum$: 109.00




Default Developing a Login System with PHP and MySQL

I reserve copyright to this article. Please include the following paragraph should you decide to use this article for your website. You are required to include a direct text link to http://www.designerbanners.com.

Used with the author's permission.
This article is written by daBoss. daBoss is the Webmaster of Designer Banners (http://www.designerbanners.com). daBoss can be contacted at sales (at) designerbanners (dot) com.



Developing a Login System with PHP and MySQL

Most interactive websites nowadays would require a user to log in into the website’s system in order to provide a customized experience for the user. Once the user has logged in, the website will be able to provide a presentation that is tailored to the user’s preferences.

A basic login system typically contains 3 components:
1. The component that allows a user to register his preferred login id and password
2. The component that allows the system to verify and authenticate the user when he subsequently logs in
3. The component that sends the user’s password to his registered email address if the user forgets his password

Such a system can be easily created using PHP and MySQL.

================================================

Component 1 – Registration

Component 1 is typically implemented using a simple HTML form that contains 3 fields and 2 buttons:
1. A preferred login id field
2. A preferred password field
3. A valid email address field
4. A Submit button
5. A Reset button

Assume that such a form is coded into a file named register.html. The following HTML code excerpt is a typical example. When the user has filled in all the fields, the register.php page is called when the user clicks on the Submit button.

[form name="register" method="post" action="register.php"]
[input name="login id" type="text" value="loginid" size="20"/][br]
[input name="password" type="text" value="password" size="20"/][br]
[input name="email" type="text" value="email" size="50"/][br]
[input type="submit" name="submit" value="submit"/]
[input type="reset" name="reset" value="reset"/]
[/form]

The following code excerpt can be used as part of register.php to process the registration. It connects to the MySQL database and inserts a line of data into the table used to store the registration information.

@mysql_connect("localhost", "mysql_login", "mysql_pwd") or die("Cannot connect to DB!");
@mysql_select_db("tbl_login") or die("Cannot select DB!");
$sql="INSERT INTO login_tbl (loginid, password and email) VALUES (".$loginid.”,”.$password.”,⠝.$email.”)”;
$r = mysql_query($sql);
if(!$r) {
$err=mysql_error();
print $err;
exit();
}

The code excerpt assumes that the MySQL table that is used to store the registration data is named tbl_login and contains 3 fields – the loginid, password and email fields. The values of the $loginid, $password and $email variables are passed in from the form in register.html using the post method.

================================================

Component 2 – Verification and Authentication

A registered user will want to log into the system to access the functionality provided by the website. The user will have to provide his login id and password for the system to verify and authenticate.

This is typically done through a simple HTML form. This HTML form typically contains 2 fields and 2 buttons:
1. A login id field
2. A password field
3. A Submit button
4. A Reset button

Assume that such a form is coded into a file named authenticate.html. The following HTML code excerpt is a typical example. When the user has filled in all the fields, the authenticate.php page is called when the user clicks on the Submit button.

[form name="authenticate" method="post" action="authenticate.php"]
[input name="login id" type="text" value="loginid" size="20"/][br]
[input name="password" type="text" value="password" size="20"/][br]
[input type="submit" name="submit" value="submit"/]
[input type="reset" name="reset" value="reset"/]
[/form]

The following code excerpt can be used as part of authenticate.php to process the login request. It connects to the MySQL database and queries the table used to store the registration information.

@mysql_connect("localhost", "mysql_login", "mysql_pwd") or die("Cannot connect to DB!");
@mysql_select_db("tbl_login") or die("Cannot select DB!");
$sql="SELECT loginid FROM login_tbl WHERE loginid=’".$loginid.”’ and password=’”.$password.”’ ”;
$r = mysql_query($sql);
if(!$r) {
$err=mysql_error();
print $err;
exit();
}
if(mysql_affected_rows()==0){
print "no such login in the system. please try again.";
exit();
}
else{
print "successfully logged into system.";
//proceed to perform website’s functionality – e.g. present information to the user
}

As in component 1, the code excerpt assumes that the MySQL table that is used to store the registration data is named tbl_login and contains 3 fields – the loginid, password and email fields. The values of the $loginid and $password variables are passed in from the form in authenticate.html using the post method.

================================================

Component 3 – Forgot Password

A registered user may forget his password to log into the website’s system. In this case, the user will need to supply his loginid for the system to retrieve his password and send the password to the user’s registered email address.

This is typically done through a simple HTML form. This HTML form typically contains 1 field and 2 buttons:
1. A login id field
2. A Submit button
3. A Reset button

Assume that such a form is coded into a file named forgot.html. The following HTML code excerpt is a typical example. When the user has filled in all the fields, the forgot.php page is called when the user clicks on the Submit button.

[form name="forgot" method="post" action="forgot.php"]
[input name="login id" type="text" value="loginid" size="20"/][br]
[input type="submit" name="submit" value="submit"/]
[input type="reset" name="reset" value="reset"/]
[/form]

The following code excerpt can be used as part of forgot.php to process the login request. It connects to the MySQL database and queries the table used to store the registration information.

@mysql_connect("localhost", "mysql_login", "mysql_pwd") or die("Cannot connect to DB!");
@mysql_select_db("tbl_login") or die("Cannot select DB!");
$sql="SELECT password, email FROM login_tbl WHERE loginid=’".$loginid.”’”;
$r = mysql_query($sql);
if(!$r) {
$err=mysql_error();
print $err;
exit();
}
if(mysql_affected_rows()==0){
print "no such login in the system. please try again.";
exit();
}
else {
$row=mysql_fetch_array($r);
$password=$row["password"];
$email=$row["email"];

$subject="your password";
$header="from:you@yourdomain.com";
$content="your password is ".$password;
mail($email, $subject, $row, $header);

print "An email containing the password has been sent to you";
}

As in component 1, the code excerpt assumes that the MySQL table that is used to store the registration data is named tbl_login and contains 3 fields – the loginid, password and email fields. The value of the $loginid variable is passed from the form in forgot.html using the post method.

================================================

Conclusion

The above example is to illustrate how a very basic login system can be implemented. The example can be enhanced to include password encryption and additional functionality – e.g. to allow users to edit their login information.
daboss is offline Reply With Quote
Sponsored Links
01-10-2005, 03:58 AM   #2
Python

Forum Leader
 
Python's Avatar
 
Join Date: Apr 2004
Location: UK, Wales
Posts: 4,651

Forum$: 12.00 (Donate)
Bank: 0.00
Total Forum$: 12.00



Send a message via AIM to Python Send a message via MSN to Python Send a message via Yahoo to Python

Default

nice
__________________
Web Development Tutorials
- Photoshop, CSS, PHP & more...
Python is offline Reply With Quote
01-10-2005, 03:58 AM   #3
CannonBallGuy

Moderator
 
Join Date: Apr 2004
Location: Planet Earth.
Posts: 2,168

Forum$: 7.00 (Donate)
Bank: 50.00
Total Forum$: 57.00



Send a message via AIM to CannonBallGuy Send a message via MSN to CannonBallGuy

Default

Woah. I only skim read parts of that but its one heck of a tutorial.
Noce going!
__________________
IndexScript Forum
CannonBallGuy is offline Reply With Quote
01-10-2005, 06:24 AM   #4
daboss

WP V.I.P."The Boss"
 
Join Date: Jul 2004
Location: Here
Posts: 1,269

Forum$: 109.00 (Donate)
Bank: 0.00
Total Forum$: 109.00




Default

thanks! more to come... when i find the time, that is
daboss is offline Reply With Quote
01-10-2005, 06:36 AM   #5
martian2k4

Llama Hunter
 
Join Date: Oct 2004
Location: ??????
Posts: 1,018

Forum$: 24.00 (Donate)
Bank: 0.00
Total Forum$: 24.00



Send a message via MSN to martian2k4 Send a message via Yahoo to martian2k4

Default

whoaa kew makes my tutorials look crap
__________________
Free MySpace Layouts- Coming soon
Photoshop Tutorials- Coming soon
Premium PHP Scripts- Coming soon

Haha i should really do some work so i can remove all the coming soon's
martian2k4 is offline Reply With Quote
01-11-2005, 06:09 PM   #6
Forge

Senior Member
 
Join Date: Aug 2004
Location: Under Your Bed
Posts: 234

Forum$: 0.00 (Donate)
Bank: 0.00
Total Forum$: 0



Send a message via AIM to Forge Send a message via MSN to Forge Send a message via Yahoo to Forge

Default

thats a nice tutorial, but it lacks of small things, what if the username field is empty, password? Email? And what if the user is already taken? That can cause some serious problems, your intentions are good but somethings in my opionion should be fixed.
__________________
Can fat people Go skinny Dipping?
Forge is offline Reply With Quote
01-12-2005, 05:34 AM   #7
martian2k4

Llama Hunter
 
Join Date: Oct 2004
Location: ??????
Posts: 1,018

Forum$: 24.00 (Donate)
Bank: 0.00
Total Forum$: 24.00



Send a message via MSN to martian2k4 Send a message via Yahoo to martian2k4

Default

In that tutorial who ever is doing it is developing a basic login and register system and thats good if you re learning. You don't wanna be learning and have a huge list of if else statements they would just confewz u or checking the database to see if the username is taken
__________________
Free MySpace Layouts- Coming soon
Photoshop Tutorials- Coming soon
Premium PHP Scripts- Coming soon

Haha i should really do some work so i can remove all the coming soon's
martian2k4 is offline Reply With Quote
01-12-2005, 10:40 AM   #8
Python

Forum Leader
 
Python's Avatar
 
Join Date: Apr 2004
Location: UK, Wales
Posts: 4,651

Forum$: 12.00 (Donate)
Bank: 0.00
Total Forum$: 12.00



Send a message via AIM to Python Send a message via MSN to Python Send a message via Yahoo to Python

Default

what you havent included (or what i dont think you included) is a bit of code which could be placed onto a page to make it accesible to logged in users only...in the end thats the whole point of a login script...
__________________
Web Development Tutorials
- Photoshop, CSS, PHP & more...
Python is offline Reply With Quote
01-27-2005, 07:57 AM   #9
Tillbster

Beginner
 
Join Date: Jan 2005
Posts: 1

Forum$: 0.00 (Donate)
Bank: 0.00
Total Forum$: 0




Default Trying to do it with MS Access

I am trying to do a log in page but have connected to an MS Access database. The problem i'm having is on the line:

if(mysql_affected_rows()==0){
print "no such login in the system. please try again.";
exit();

on mine i've got:

$rs=odbc_exec($conn,$sql);
if (!$rs)
{
$_SESSION["message"] = "Could not connect to Airedale Sports Centre as $appUsername";
header("Location: login.php");
}
else
{
$_SESSION["authenticatedUser"] = $appUsername;
// Relocate to the logged-in page
header("Location: loggedon.php");
}

All it does is let anyone log in nomatter what user name or password. If i change it to:

if (!$rs->$appPassword=false)

No one can log in. I think its only this line thats wring with it. I tried putting in if(mysql_affected_rows()==0 but that came up with an error.

Any help would be greatly appreciated,

Cheers.
Tillbster is offline Reply With Quote
10-28-2006, 06:15 PM   #10
WatchOut

Advanced User
 
WatchOut's Avatar
 
Join Date: Oct 2006
Location: Sweden
Posts: 362

Forum$: 6.60 (Donate)
Bank: 0.00
Total Forum$: 6.60



Send a message via MSN to WatchOut Send a message via Yahoo to WatchOut

Default Re: Developing a Login System with PHP and MySQL

wow great article! thanks a lot
__________________
Computer Forumz

www.computerforumz.com
WatchOut is offline Reply With Quote
11-30-2006, 05:45 PM   #11
Kilk

Junior Member
 
Join Date: Nov 2006
Location: Idaho
Posts: 58

Forum$: 65.00 (Donate)
Bank: 0.00
Total Forum$: 65.00



Send a message via MSN to Kilk

Default Re: Developing a Login System with PHP and MySQL

This is a pretty good article, Thanks daboss!
Kilk is offline Reply With Quote
11-30-2006, 08:29 PM   #12
DeluxeNames

Admin
 
Join Date: Jul 2006
Posts: 1,162

Forum$: 20,981.13 (Donate)
Bank: 0.00
Total Forum$: 20,981.13




Default Re: Developing a Login System with PHP and MySQL

This thread is the most viewed one in the whole forum by far
DeluxeNames is offline Reply With Quote
12-03-2006, 10:49 PM   #13
Kilk

Junior Member
 
Join Date: Nov 2006
Location: Idaho
Posts: 58

Forum$: 65.00 (Donate)
Bank: 0.00
Total Forum$: 65.00



Send a message via MSN to Kilk

Default Re: Developing a Login System with PHP and MySQL

Quote:
Originally Posted by DeluxeNames View Post
This thread is the most viewed one in the whole forum by far
Well it was posted almost 1 year ago...
Kilk is offline Reply With Quote
04-25-2010, 05:34 PM   #14
soepxozk

Junior Member
 
Join Date: Apr 2010
Posts: 59

Forum$: 98.00 (Donate)
Bank: 0.00
Total Forum$: 98.00




Default Buy Backlinks jcbbgdwgfy

You should really go see LowCostLinks.com, they sell the very highest quality backlinks on the internet.
soepxozk is offline Reply With Quote
04-26-2010, 01:46 PM   #15
soepxozk

Junior Member
 
Join Date: Apr 2010
Posts: 59

Forum$: 98.00 (Donate)
Bank: 0.00
Total Forum$: 98.00




Default Buy Backlinks vfojfxxvwl

You should definitely go see LowCostLinks.com, they sell the best quality backlinks online!
soepxozk is offline Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum$ Per Thread View: 0
Forum$ Per Thread: 2.00
Forum$ Per Reply: 1.00
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using line breaks in mysql danielneri PHP 7 12-22-2005 04:51 PM
My new book: Beginning PHP 5 & MySQL Python PHP 0 09-21-2005 04:15 PM
Help in connecting to MySQL using PHP chococat PHP 2 09-19-2005 11:08 AM
Listing MySQL Tables In PHP Python PHP 2 08-14-2005 04:27 PM
MySQL Database Handling in PHP daboss Tutorials 1 11-08-2004 11:17 AM

All times are GMT -5. The time now is 07:43 PM.
Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 2006 - Webmaster Post