Creating a Login System with PHP and MySQL

The following guide is written by daBoss. daBoss is the Webmaster of Designer Banners. daBoss can be contacted at sales (at) designerbanners (dot) com.

Creating a Login System with PHP and MySQL

Most interactive websites nowadays would require a user to log in into the web site's platform so as to present a personalized experience for the consumer. When the user has logged in, the web site will have the ability to present a presentation that's tailored to the consumer's tastes.

A basic login system typically contains 3 elements:

The component Which Allows a user to register his preferred login id and password

The component Which Allows the system to verify and authenticate the user when he attempts in

The component that sends the user's password to his registered email address if the user forgets his password

This type of system is easily created using PHP and MySQL.

Part 1 — Registration

Part 1 is typically implemented using a simple HTML form that contains 3 fields and 2 buttons:

A preferred login id field

A preferred password field

A valid email address field

A Submit button

A Reset button

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

This code excerpt can be utilized as part of register.php to process the enrollment. It connects to the MySQL database and inserts a line of data into the table used to keep the registration details.

@mysql_connect("localhost", "mysql_login", "mysql_pwd") or die("can't 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 keep the registration data is called tbl_login and comprises 3 fields — both the loginid, password and email fields. The worth of the $loginid, $password and $email variables are passed from the form in register.html using the post system.

Part 2 — Evidence and Authentication

A registered user may want to log in the system to get the functionality offered by the site. The consumer might need to provide his login id and password to the system to confirm and authenticate.

This is generally achieved through a simple HTML form. This HTML form typically contains 2 fields and 2 buttons:

A login id field

A password field

A Submit button

A Reset button

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

This code excerpt can be applied as part of authenticate.php to process the login request. It connects to the MySQL database and queries the table used to keep the registration details.

@mysql_connect("localhost", "mysql_login", "mysql_pwd") or die("can't 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(); Find out more about php scripts for social networking by checking out alkanyx.com/.

if(mysql_affected_rows()==0)

Publish "no such login in the system. please try again. ";

exit();

else

Publish "successfully logged into system. ";

//proceed to perform website's performance — e.g. current information to the consumer

As in component 1, the code excerpt assumes that the MySQL table that's used to keep the registration data is called tbl_login and comprises 3 fields — both the loginid, password and email fields. The worth of the $loginid and $password variables are passed from the form in authenticate.html using the post method.

Part 3 — Forgot Password

A registered user may forget his password to log into the web site's system. In cases like this, the consumer will have to supply his loginid for the system to recover his password and send the password to your consumer's registered email address.

This is done via a simple HTML form. This HTML form typically contains 1 field and 2 buttons:

Facebooktwittergoogle_plusredditpinterestlinkedinmail