First, I'll provide you with the files then break them down into pieces
Database.php:
PHP Code:
<?php
$MySQL = array();
$MySQL2 = array();
$MySQLDB = array();
$MySQL['host'] = 'localhost';// Host name
$MySQL['user'] = 'user'; // Username
$MySQL['pass'] = 'pass'; // Password
$MySQL['name'] = 'database'; // Database Name
$MySQL['error_site'] = 'un-defined'; //Site to go to if there's an error
$conn = mysql_connect($MySQL['host'] , $MySQL['user'] , $MySQL['pass']) or die('[MYSQL] Error connecting to SQL server. Please report the bug <a href="' . $MySQL['error_site'] . '">here</a>."');
mysql_select_db($MySQL['name'])
?>
Login.php (To verify)
PHP Code:
<?php
function Redirect($URL) {
echo "<html><head><meta http-equiv=\"refresh\" content=\"0;url=$URL\"></head></html>";
}
$Login['user_cookie'] = 'tut_user';
$Login['user_cookie_pass'] = 'tut_pass';
$Login['site'] = 'your_site.com';
if (isset ( $_POST ['username'] ) && isset ( $_POST ['password'] )) {
include "database.php";
$user = $_POST ['username'];
$pass = $_POST ['password'];
$sql = mysql_query ( "SELECT `password` FROM `user` WHERE username = '" . $user . "'" );
$fetch_em = mysql_fetch_array ( $sql );
$numrows = mysql_num_rows ( $sql );
if ($pass == $fetch_em ["password"] && ! empty ( $fetch_em ["password"] )) {
$valid_user = 1;
} else {
$valid_user = 0;
}
if ($valid_user == 1) {
setcookie ( $Login['user_cookie'] , $user, time () + 60 * 60, "*.$Login['site']" );
setcookie ( $Login['user_cookie_pass'] , $pass, time () + 60 * 60, "*.$Login['site']" );
header ( "location: $Login['site']" );
} else {
Redirect ( "$Login['site']/login.php?error=invalid" );
die ( "" );
}
} else {
die ( mysql_error () );
}
?>
index.php (The page with the form)
PHP Code:
<?php if(isset($_GET['error'])){ if($_GET['error'] == "invalid"){echo "<font color=\"red\"><b>Invalid username or password. Please try again.</b></font>";} } ?>
<form action="login.php" method="post" autocomplete="off">
<table style="opacity: 1;">
<tbody><tr>
<td>Username:</td>
<td><input size="20" name="username" maxlength="12" type="text"></td>
</tr>
<tr>
<td>Password:</td>
<td><input size="20" name="password" maxlength="20" type="password"></td>
</tr>
<tr>
<td></td>
<td align="center"><input value="Secure Login" type="submit"></td>
</tr>
</tbody></table>
</form>
Table.sql
Code:
CREATE TABLE `user` (
`username` varchar(20) collate latin1_general_ci NOT NULL default '',
`password` varchar(20) collate latin1_general_ci default NULL,
`rights` int(1) NOT NULL default '0',
PRIMARY KEY (`username`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
Breaking down Database.php
Nothing really to put here, the functions are pretty obvious "mysql_select_db" selects the database after connecting to the host using mysql_connect.
Breaking down login.php
PHP Code:
$user = $_POST ['username'];
$pass = $_POST ['password'];
$sql = mysql_query ( "SELECT `password` FROM `user` WHERE username = '" . $user . "'" );
Gets the defined username and password and checks if they work or not
PHP Code:
$fetch_em = mysql_fetch_array ( $sql );
$numrows = mysql_num_rows ( $sql );
Fetches all the passwords and puts them into an array then numbers the rows
PHP Code:
if ($pass == $fetch_em ["password"] && ! empty ( $fetch_em ["password"] )) {
$valid_user = 1;
} else {
$valid_user = 0;
}
If the password is the same as the password for the user in the database, it sets the valid user variable to one, if it's anything else apart from that it's a fail.
PHP Code:
if ($valid_user == 1) {
setcookie ( $Login['user_cookie'] , $user, time () + 60 * 60, "*.$Login['site']" );
setcookie ( $Login['user_cookie_pass'] , $pass, time () + 60 * 60, "$Login['user_cookie']" );
Sets the cookie that's been defined in the variables if everything is correct.
Breaking down Index.php
PHP Code:
<?php if(isset($_GET['error'])){ if($_GET['error'] == "invalid"){echo "<font color=\"red\"><b>Invalid username or password. Please try again.</b></font>";} } ?>
If there's a get request in the URL (?error=invalid) it displays Invalid Username or password
And that's all I can explain basically.
Thanks for reading - Next a user registration system.