PHP LOGIN COMPLETE SCRIPT.



In this post going to build a new php and MySql login system for your site and web application with database connectivity.

To begin, you will need to create a new table in your database named 'users'. In this database create three new fields 'id' (primary key), 'username', 'password'. You can add more fields as you need them later. For now we're just going to worry about checking for an existing username and password combination.

This script is great for basic logins, but may not be what you need for something more complex.

How to connect and Select a database?

Create new file with name mysql_connect.php.Set the database host, username, password and database name.

<? php
define('DB_USER', 'USERNAME');
define('DB_PASSWORD', 'PASSWORD');
define('DB_HOST', 'LOCALHOST');
define('DB_NAME', 'MYDATABASE');

Connect to MySQL Script:
$dbc = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD);

How to select the Database?

mysql_select_db (DB_NAME);



Next, create your html page with the login form. Below is a quick sample of a form you can build.

if (isset($_POST['submit']))
 { // Start the submit form.
require_once('../mysql_connect.php'); // connect to the database

$message  = NULL;

//Check the username field empty or not.

if(empty($_POST('username'))) {
$user = FALSE;
$message .= '<p> You did not enter any username <p>';
}
else
{
$username = $_POST('username');
}

//Check the password field empty or not.

if(empty($_POST('password')))
{
$pass = FALSE;
$message .= '<p> You did not enter any password <p>';
}
else
{
$pass = $_POST('password');
}

if ($user && $password)// if username and password Perfect
{

// query select username from Post username and password .
$finduser = “Select userid, firstname from USERS WHERE username = ‘$user’
AND password = ‘$pass’)”;
$result = @mysql_query($finduser,$dbc);
$row=Mysql_fetch_array ($result,MYSQL_NUM); // find a record.
If ($row) // when record was exists.
{
// set the cookies and redirect.
Setcookie (‘firstname’, $row[1]);
Setcookie (‘userid’, $row[0];
Header (“Location: http://” .$_SERVER[‘HTTP_HOST’] . dirname($_SERVER[‘PHP_SELF’]) .
“/loggedin.php”);
Exit(); Quit the script.
}
Else // no record match with query.
{
$message = ‘<p> Wrong username and password. </p>’;
}
Mysql_close(); close the connection.
}
else
{
$message .= ‘<p> Try again. </p>’;
}
} // End of submit form
// start the Login form
$page_title = ‘Login Form’;
If(isset($message))
{
echo ‘<font> color=”red”>’, $message, ‘<font>’;
}
?>
<form action = “<? Php echo $_server[‘PHP_SELF’]; ?>” method = “post”>
<fieldset> <legend> User Login Form </legend>
<p> <b> User Name: </b> <input type = “text” name=”username” size =”10” maxlength= “20” value=”<? Php if (isset($_POST[‘username’])) echo $_POST[‘username’]; ?>” /></p>
<p> <b> Password:</b> <input type=”password” name=”[password” size=”20” maxlength =”20”/></p>
<div align=”center”>< input type=”submit” name=”submit” value=”Login” /> </div>
</form>

Post a Comment

0 Comments