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.
Used with the author's permission.
This article is written by John L.
John L is the Webmaster of Designer Banners (http://www.designerbanners.com).
|
|
|
Created & Maintained by Empower! CMS Web Sites
Host2Sell Web Hosting | Emarketing Workshops | Site SEO Review | FREE NewsletterYour Website Reflects Your Business
Some left shoes are in isle 5, while the right shoes are in isle 3. Shoe hills are in random places. You can barely walk through the store without stumbling over a shoe.No employees are in sight. As you are desperate for help, you finally make it past the heaping hill of shoes to the back of the store and find a button that says "Page for Help". You press the button and out comes a card that says "please leave your mailing address, we'll send you a letter". Frustrated, you leave the store, and finally realize there is a Footlocker across the street.We all know how first impressions can make or break a potent...(related: Web Development)
Maximizing Roi Via Web Site Traffic Analysis
We are clearly well past the innocent "golden age" of the Internet ? Darwinian economics have become the order of the day today. Any company worth their salt must maintain a web site as an information resource and/or to generate incremental ecommerce revenue. Qualified traffic is the name of the game, as it's the oxygen that keeps a good ecommerce web site thriving. Consequently, it's very important to have an understanding of your web site traffic or visitor activity; i.e. baseline information that includes the number of visitors during specific time periods, originating location (domain), where the visitors went on a site and how long they stayed on specific sections or pages of a web site. The de facto industry standard traffic analysis tool application is Web Trends, manufactured/supported by netiQ, Inc., which we will foc...(related: Web Development)
Six Basic Reasons Why Visitors Stay On Your Web Site
1. The first page appears quickly.It's a good way to have your brief tagline in the starting page of your web site and your company logo. Highlight the benefits and advantages of your products on the starting page.But don't dump all the information on it so that it will appear quickly. Limit your graphic to a file size of 20 KB or less.2. It's purpose is immediately and clearly identified.Say what you want to say and say it briefly and straight to th...(related: Web Development)
Live Support From Your Website? Can You Do It For Free?
I'm a freelance programmer and somehow I managed to get clients, speak with them, and generate leads for years without a live help system. To be honest, I had put in bids for a "live" he...(related: Web Development)
Frequent Business Question: How Do You Start A Web Business?
Today it seems everyone is jumping on the entrepreneurbandwagon, and who could blame them? Setting your ownschedule, working from the comfort of your own home, andnever having to deal with a horrible commute or officepolitics again, this option is quite popular among all ages.With the advent of the technology age, it seems moreindividuals are looking to the internet to...(related: Web Development)
Trust Us! Sending Credible Messages
A company must earn and keep trust or sales don't happen. Imagine that last Sunday, while you were reading the paper, you saw an ad for a great deal on a digital camera. You'd been considering buying one for a while, and this ad sealed the deal. You went to buy the camera, and the sales person told you they were sold out. They didn't offer you a rain check and instead substituted a different model for a "similar" price.In this classic case of "bait and switch," and you felt like you'd been had.After that, do you trust these people? Will you return to buy from them? Were you aggravated at the paper for running a less than honest ad?In a similar fashion, maintai...(related: Web Development)
Get Your Business On The Web
I can't think of any business alive today that couldn't benefit from at least a minor presence on the web. Now that I've made that statement, I'm sure someone will point out one. My point is, however, that having a web presence today is about as cheap as anything going and if you ever get one client or sale from the web you are way ahead.How many times have you come across someone who would be ideal as one of your customers. They have about a minute to talk to you and you don't happen to have a brochure in your back pocket. How easy it would be to simply tell them to "gotom...(related: Web Development)
Beautiful Web Sites Seldom Make More Sales
Designing an E-commerce Web site is not as simple as having a "pretty" site that is a pleasure to visit. While it is important to have an attractive site, as much thought must be given to functionality as to appearance.Many Web designers seem to focus more on appearance than functionality, and while an attractive site is helpful, visitors that become frustrated by the inability of a site to function well, will leave almost immediately, never to return!Just as important in designing an e-commerce-style Web site, is a somewhat basic knowledge of Web site design, with some simple design skills included. While a thorough knowledge of design and functionality is not important, designing an e-commerce site does have some requirements, or else you'll always find yourself g...(related: Web Development)
How To Get Your Website In Front Of Thousands Of Customers At No Cost To You!
Dan Kennedy once said that if you can't make money without money? then? you won't make money with money either.Those words swirled in my brain over and over. Then, one fine day, I decided to test them out. Here's what I did:I was running my own promotions and noticed that the Internet was beginning to unfurl. A little voice in the back of my head told me I had better jump into cyberspace.But I did not have a websit...(related: Web Development)
Having A Domain Name And Web Site Gives Your Business The Key To The Door
Just like all things in life, the ways to run a business are rapidly changing and evolving. The potential involved with having a domain name/web site is staggering to say the least. A web site gives the consumer a 'shop front' that is not only open 24 hours a day, seven days a week, but is also situated right on their desktop. Instead of being locked into only the immediate area, the whole world's marketplace becomes your sphere of interaction.Customers can access information on all your products, no more salesperson with limited knowledge trying to explain something they no little about. No more waiting in lines for help, all people hav...(related: Web Development)
Are You Losing Sales Because Of These 4 Simple Website Mistakes?
Losing website sales and traffic is sometimes simply a case of web usablity - your website just isn't clear to your visitors. By fixing these simple mistakes, you'll easily increase your conversion rate and make more online sales.Mistake #1: No Clear Web Navigation SystemIs t...(related: Web Development)
site-map - Copyright © 2006 Empower! Web Design | All Rights Reserved. | Web Development
