Webmaster Papers




Google
 
Web webhostingpapers.com




/pagead2.googlesyndication.com/pagead/show_ads.js">

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).

RELATED ARTICLES


9 Simple Steps to Create a Background Tiled Image Branded with Your Name
You have seen those web pages where they have the name of the Name Web repeated over over over in square tiles as the background. Now by following these 9 Simple steps you too can create a web page Branded with your Name, or Company Name.
Are You Overlooking the Benefits of Using Sub-Domains within Your Web-hosting account?
One of the most useful features offered by some web hosting companies, is the option of creating sub-domains in the same web hosting account. The problem is, most website owners are not aware of why and how to create sub-domains.
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.
The Importance of Website Stats to You
One of the best tools you have as a webmaster is your website statistics. The most popular site stats programs are Awstats, Webalizer, and Analog. Usually, they come free of charge and are accessible through your website's control panel provided by your web host.
Direct Sales and Your Corporate Website - A Creative Marketing Plan that Works!
Creatively marketing your corporate site takes time in the set up but you will learn that building your customers isn't about marketing your products but getting your name and reputation into the minds of internet customers.
How To Make Your Website More Successful? (Part I)
Building a website and getting it online is easy. Driving visitors to it is the more difficult part. Most people are not patient enough when it comes to build up traffic. They expect thousands of visitors a week after they go live with their website. But that is not how it works. We share some secrets of how to make your website more successful.
Linking Out is Good
Many websites I come across don't have a single link to another website. Ask the webmaster why not, and the answer you get is simple enough: "If I link to other websites people might leave my site." At this point I break the news that site visitors will leave your site. And there's nothing you can do about this.
Marketing Your Business Online
As a business you can't afford to ignore the Internet Age; in 2002 there was an estimated 605 million people online around the entire globe, today this is estimated at over a billion users worldwide.
4 Marketing Tips for Resourceful Webmasters!
The internet is a sea of knowledge. Getting your information to 'float' by the right audience can be like finding that one special grain of sand on the seashore. Paying big bucks for marketing can strain your already limited budget. What is a webmaster to do? Let's discuss four valuable and proven ways to market your site without breaking the bank.
7 Reasons Why Your Small Business Needs A Web Site
Many small businesses have the misconception that their business can not benefit from a website; that websites are too expensive or that because they don't use a computer neither do their potential clients. Here are 7 reasons why your small business NEEDS a website:
10 Simple Steps: Its Magic If You can E-Mail You can Update Your Web Page Dynamically
Dynamically Update Your Web Pages Via E-Mail
Five Question-Five Minute Web Makeover Quiz
If you haven't made the sales and built your clients to an income you want, then you may need a Web Site makeover.
3 Essential Tools Every Website Designer and Marketer Needs
Appealing to others is really important when you have a web page. This is because you will not have any personal contact with visitors and cannot persuade them to buy from you or come into your store, not personally that is. Your web page is your medium for persuasion so you need to put as much thought and effort into the design of your website and how it will appeal to potential customers as you would into a physical store. Unfortunately, a lot of web designers and marketers don't think of their web pages this way and make a lot of mistakes that cost visitors and sales. Don't let this happen to you, instead focus on the importance of appearance and have fake visitors visit your site and let you know what works and what does not, anonymously of course, before you launch so you will have some real web input. Beyond that, there are some other important tools you will need to utilize as well if you want to have ea successful website.
Selling Your Site: Outsource or Homemade?
To the starting internet entrepreneur, there is nothing more discouraging then the erroneous realization that your dream for a self sufficient business cannot be realized without a heavy investment. This may be why so many people shy away from the internet; seeing all the flashy sites, many of which are $50 templates, people are afraid to enter the world of internet sales and marketing.
Content Management
More and more businesses are recognizing the importance of content management when it comes to their websites. Website content is more important than ever before, and as the Internet matures and changes, it is likely be become even more important. Many smart companies are beginning to make their website content the centerpiece of their Internet presence.
What and How to choose the Right Keywords for Mega Traffic
Keywords in Search Engine Optimizing and Search Engine Marketing are the building blocks and foundation of your website on the search engines. If your foundation is weak or poorly put together your webpages won't have much to be based upon for the engines to rank with.
Website Strategy!
A website lets you put your products in front of a world-wide audience. It can help you generate new revenue, cut costs and build better relationships with both customers and suppliers.
Ebooks for Webmasters
Internet has opened a whole new world for web developers and web designers who are looking to get their websites developed that are finally launched on the huge world of World Wide Web. Keeping that factor webmasters are the people who have to face a lot of problems mainly due to the entrance of new software, programming languages and viruses that are a real threat for the webmasters.
Effectively Using Robots Meta Tags
The "robots" meta tag, when used properly, will tell the search engine spiders whether or not to index and follow a particular page. For the purposes of this article, we will be using the "( )" symbols to represent the "" in html coding.
Up The Sandbox!
Go to any internet marketing forum you want these days and one of the topics is sure to be whether or not there is a "sandbox" at Google where new sites are forced to come and play for 3-6 months before joining the ranks of ranked and searched results. On the surface it would appear that this is so. New sites are typically taking a long time to get indexed and even longer to show up in the search results. So hence, the sandbox theory. Let's examine some possible reasons and more importantly, our reaction to it.