Creating Simple Login System in PHP

We will cover about creating simple login mechanism using PHP and MySQL. Login usually used to seperate the public access page and secured page. Sample of public page is for example like public home page, public profile page, about page, contact page etc. Secured page for example like user profile settings page, user accounts page etc. The point is everything that only specific user can view is considered secured page.The page that displayed after the login screen is secured page.

First we have to prepare the database that store the user credentials. See below table structure.

1
2
3
4
5
6
CREATE TABLE IF NOT EXISTS `tbl_users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) DEFAULT NULL,
  `password` varchar(1024) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1;

Now we have table tbl_users on MySQL that will store the login details. It’s very simple table that contains username and password field. Let’s populate the table with one credentials data.

INSERT INTO tbl_users (username, password) VALUES ('sonyak', sha1('welcome'));

We have created new data with username ‘sonyak’ and password ‘welcome’. The password is hashed using sha1() function on MySQL. The data will be like this.

mysql> select * from tbl_users;
+----+----------+------------------------------------------+
| id | username | password                                 |
+----+----------+------------------------------------------+
|  1 | sonyak   | c0b137fe2d792459f26ff763cce44574a5b5ab03 |
+----+----------+------------------------------------------+
1 row in set (0.00 sec)

Now we need the login form. Here is the HTML code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<p>Simple Login Form</p>
<form method="post" action="p_login.php">
  <label>
    Username:
    <input type="text" name="username" id="username" />
  </label>
  <br />
  <br />
  Password:
  <label>
    <input type="password" name="password" id="password" />
  </label>
  <br />
  <br />
  <input name="submit" type="submit" id="submit" value="Login" />
</form>
<p>&nbsp;</p>
</body>
</html>

Save it as index.php.

Login form example

Login form example

The form login above has action that associated with p_login.php script.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
  session_start();
 
  // prepare to connect to mysql
  $dbConn = mysql_connect("localhost", "your_db_username", "your_db_password") or die("Connection to database failed, perhaps the service is down !!");
  mysql_select_db("earthstone") or die("Database name not available !!");
 
  $username = $_POST['username'];
  $password = $_POST['password'];
 
  $sqlString = "SELECT * FROM tbl_users WHERE username = '" . $username . "' AND password = '" . sha1($password) . "'";
  $resultSet = mysql_query($sqlString, $dbConn);
 
  if (mysql_num_rows($resultSet) > 0) {
    // login success
    $_SESSION['username'] = $_POST['username'];
    header("Location: home.php");
  } else {
    // login failed
    header("Location: index.php");
  }
?>

Save the script above as p_login.php. The script starts with creating a session and then connect to the database. The script then will do the query to table tbl_users for specified username and password. If the user is found then we assign $_SESSION['username'] with $username variable and then we throw user to secured page called home.php. If login failed then user will be thrown to the login screen again.

How about the secured page? How it looks like? Here is the sample of secured page home.php.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
  session_start();
 
  if (!isset($_SESSION['username'])) {
    header("Location: index.php");
  }
?>
<html>
<head>
<title>Home - Secured Page Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
Sample of secured page - home.php<br/>
<br/>
Click here to <a href="p_logout.php">logout</a>.
</body>
</html>

Save it as home.php.

Sample of secured page

Sample of secured page

Inside the home.php there is a link to do a logout mechanism. There is a link to p_logout.php that will do the logout. Here is the code for p_logout.php.

1
2
3
4
5
6
7
8
9
<?php
  session_start();
 
  unset($_SESSION['username']);
 
  session_destroy();
 
  header("Location: index.php");
?>

Save it as p_logout.php. What is logout actually doing? It will clear or reset the session data and then destroy the session using session_destroy() function. Finally you will be thrown to index.php that contains the login form. That’s it, simple isn’t?

If you have any difficulties you can send comment below or send email to info@sony-ak.com.

Bookmark and Share

Related posts:

  1. Create a Simple ‘Forget Password’ Feature using PHP
  2. Simulate Login to Facebook with PHP and curl
  3. Sample Code of Login to Friendster using Curl on PHP
  4. Backup and Restore MySQL Database using mysqldump
  5. Create Simple Data Paging using PHP and MySQL

3 Comments

  1. [...] the original: Sony AK Knowledge Center » Blog Archive » Creating Simple Login … [...]

  2. Hello extremely good blogs!! Man .. I will take the feeds

  3. wiyono says:

    Hallo,
    di mana aku simpan table untuk mysql??

    Tolong donggg..

    Tengs
    wiyono

Leave a Reply