Developing a Login System with PHP and MySQL - Web Hosting Forum
Web Hosting Forum

wmp banner

User Name
Password
Go Back   Web Hosting Forum > WebmasterPost > Tutorials
Closed Thread
01-09-2005, 11:14 PM   #1
daboss

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




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
Sponsored Links
01-10-2005, 04:58 AM   #2
Python

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



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
01-10-2005, 04:58 AM   #3
CannonBallGuy

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



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
01-10-2005, 07:24 AM   #4
daboss

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




Default

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

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



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
01-11-2005, 07:09 PM   #6
Forge

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



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
01-12-2005, 06:34 AM   #7
martian2k4

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



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
01-12-2005, 11:40 AM   #8
Python

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



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
01-27-2005, 08:57 AM   #9
Tillbster

Beginner
 
Join Date: Jan 2005
Posts: 1




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
10-28-2006, 07:15 PM   #10
WatchOut

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



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
11-30-2006, 06:45 PM   #11
Kilk

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



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
11-30-2006, 09:29 PM   #12
DeluxeNames

Admin
 
Join Date: Jul 2006
Posts: 1,162




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
12-03-2006, 11:49 PM   #13
Kilk

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



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
11-08-2012, 11:29 PM   #14
VotoAntinly

Beginner
 
Join Date: Nov 2012
Location: Denmark
Posts: 1



Send a message via ICQ to VotoAntinly

Default cialis 10 ou 20 mg cialis en suisse

Meaning both you and your accomplice is often spontaneous, responding to every other sexually in the event the second is correct. Needless to say your success may well range so communicate for your medical professional now to find out if TADALAFIL meets your needs. In medical trials, TADALAFIL was revealed to enhance the power of fellas with ED to possess an individual prosperous intercourse try providing 36 hrs following dosing. TADALAFIL hasn't been examined for various sexual makes an attempt for each dose.

The best way to Require?

TADALAFIL may possibly be taken as much as after each day by most people. And since sexual stimulation is necessary for TADALAFIL to operate, you reply to your companion only if the second is proper.



Backache and muscle mass ache ended up also noted, from time to time with delayed onset. Even though a exceptional event, fellas who working experience an erection for additional than four several hours (priapism) must seek out instant health care consideration. Explore your clinical disorders and prescription drugs along with your physician to make sure Tadalafil meets your needs which you happen to be nutritious ample for sexual exercise.



four mg after each day), approved for prostate difficulties or hypertension, usually do not consider Tadalafil. This kind of mixtures could result in a unexpected, unsafe decline in blood pressure level. You should not consume booze in excessive (to your stage of intoxication) with Tadalafil. This mix may well boost your odds of having dizzy or decreasing your blood pressure level. Tadalafil doesn't safeguard a male or his associate from sexually transmitted illnesses, which include HIV
VotoAntinly is offline
11-21-2012, 10:22 PM   #15
XRumerTest

Beginner
 
Join Date: Sep 2012
Posts: 5




Default Test, just a test

Hello. And Bye.
XRumerTest is offline
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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

Forum$ Per Thread View:
Forum$ Per Thread:
Forum$ Per Reply:
Forum Jump

All times are GMT -5. The time now is 08:58 PM.
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
Copyright 2012 - HostBoards.com, All Rights Reserved