Login example for PHP

Following image shows the simple form that I am going to use as login window.

Login Form

Some important code phases relevant to above GUI is as follows.

form id="form1" name="form1" method="post" action="login.php"
input type="text" name="textfieldUsernameName" id="textfieldUsernameId"
input type="password" name="textfieldPassword" id="textfieldPassword"
input type="submit" name="buttonLogin" id="buttonLogin" value="Log In"


First you have to fill Username and Password fields. Then when you press the Log In button it will call the login.php page. In this scenario it has done using the form action attribute. If not we can do the same thing with onClick attribute in the button.



$var_username = $_POST[@textfieldUsernameName];
$var_password = $_POST[@textfieldPassword];

$selectQuery=mysql_query("SELECT * FROM hec_login WHERE username='$var_username' AND password='$var_password'");
$numberOrRows = mysql_num_rows($selectQuery);

	header("Location: login_success.php");
	echo 'Login fail. Redirecting You Back....';
	echo '';


The code that should come with in ‘ ‘ marks in the second echo is “meta http-equiv=”refresh” content=”2;url=index.php”” without the first and last quotation marks and instead of those use angel brackets.

Here I have used a different file to connect to the database. That file I have included via include option in PHP. Content of that database.php file is;



$connection = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
$db_select = mysql_select_db(DB_SCHEMA, $connection);


That file includes another file named as constants.php. The content of that file is;


define ('DB_HOST', 'localhost');
define ('DB_USER', 'root');
define ('DB_PASSWORD', '');
define ('DB_SCHEMA', 'hec');


If all things correct you will be able to see the following page else you will be redirect to the index page with a login failed message.
Login Success

I am hoping to continue this further. So keep in touch.

Thank you🙂

About AnujAroshA

An undergraduate in the stream of ICT (Information & Communication Technology). A simple person :)
This entry was posted in PHP examples. Bookmark the permalink.

2 Responses to Login example for PHP

  1. Pingback: PHP “session” and a logout example | Anu's crazy world

  2. Pingback: Retrieve data from MySQL table and generate XML in PHP | Anu's crazy world

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s