PHP Login Script

Posted By: KarthiKeyan

PHP Tutorial for people who is going to develop some real-time applications in PHP-MySQL . I am not going to say what is a variable and what is “for” loop syntax. If you want to know very basic about php then go to Zend PHP Tutorials . My tutorial is fully application based.

I assume that you know basic programming knowledge in php. Like for loop, while loop, session & mysql database connectivity.

Ok we are just going to develop one login system now.

What are the steps?

  1. Get username & Password from user
  2. Validate them
  3. If valid then let user to use the system
  4. else show an error message

These are basic considerations.

We can achieve this by writing a single php file.

1. PHP login form design

Design notes: Use tables to show form fields in aligned manner.

Code for simple table with 6 cells

<table width=”300″ border=”0″>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>

I hope you know for printing a blank space. Enclose the table tags with a form tag.

<form id=”form1″ name=”form1″ method=”post” action=”login.php”>
<table width=”300″ border=”0″>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</form>

Note: action = “login.php” login.php is current file name. you can change its name.

<form id=”form1″ name=”form1″ method=”post” action=”login.php”>
<table width=”300″ border=”0″>
<tr>
<td>Username</td>
<td><input type=”text” name=”username_field” /></td>
</tr>
<tr>
<td>Password</td>
<td><input type=”text” name=”password_field” /></td>
</tr>
<tr>
<td> </td>
<td><input type=”submit” name=”login” value=”Login” /></td>
</tr>
</table>
</form>

Note: input type=”password” because Password field will carry password characters.

You know these are just <html> tags.

<?php
$username = $_POST[‘username_field’];
$password = $_POST[‘password_field’];
$user = “user1”;
$pass = “123456”;
If(($username == $user) && ($password == $pass))
Echo(“Welcome $username<br>”);
Else
Echo(“Incorrect username/password<br>”);
?>

Note: $_POST[‘username_field’]; and $_POST[‘password_field’]; are POST method values. $user & $pass are variables which holds the username/password.
Continue to page 2
This is not a full login system. This is for showing how it will be.




Google