Login/Register script using PHP and MySQL
The following example expalins the creation of simple login and registration page using PHP and MySQL.
Sample code for registering in MySQL database using php.
?php
include("dbinfo.inc.php");
session_start();
$connect=mysql_connect($server, $db_username, $db_password);
if (!$connect) {
die('Could not connect: ' . mysql_error());
}
else {
mysql_select_db($db_name, $connect) or
die (mysql_errno().": ".mysql_error()."");
$m_strUsername = $_POST['username'];
$m_strFullname = $_POST['fullname'];
$m_strEmail = $_POST['email'];
$m_strPassword = md5($_POST['password']);
$m_nPhoneNumber = $_POST['phonenumber'];
// Query if the user name is already existing
$isUsernameExist = "SELECT username FROM $tb_name WHERE username='$m_strUsername'";
//echo $isUsernameExist;
$row = mysql_num_rows(mysql_query($isUsernameExist));
if ($row == 1) {
echo "Username is already registered. Please choose different username.";
header("Location: register.htm");
}
else {
$insert = "INSERT INTO $tb_name VALUES ('',
'$m_strUsername',
'$m_strFullname',
'$m_strEmail',
'$m_strPassword',
'$m_nPhoneNumber')";
if (!mysql_query($insert)) {
die('Error: ' . mysql_error());
header("Location: register.htm");
}
else {
echo "Registered Successfully";
header("Location: login.htm");
}
}
mysql_close($connect);
}
?
Sample code for login in MySQL database using php.
?php
include("dbinfo.inc.php");
session_start();
$connect=mysql_connect($server, $db_username, $db_password);
if (!$connect) {
die('Could not connect: ' . mysql_error());
}
else {
$isDbSelected = mysql_select_db($db_name, $connect) or
die (mysql_errno().": ".mysql_error()."");
//echo $isDbSelected;
$m_strUsername = $_POST['username'];
$m_strPassword = md5($_POST['password']);
// To protect MySQL injection (more detail about MySQL injection)
$m_strUsername = stripslashes($m_strUsername);
$m_strPassword = stripslashes($m_strPassword);
$m_strUsername = mysql_real_escape_string($m_strUsername);
$m_strPassword = mysql_real_escape_string($m_strPassword);
// Query if the user name is already existing
$isUsernameExist = "SELECT * FROM $tb_name WHERE username='$m_strUsername' AND password='$m_strPassword'";
//echo $isUsernameExist;
$count = mysql_num_rows(mysql_query($isUsernameExist));
//echo $count;
if ($count == 1) {
// http://php.net/manual/en/function.session-register.php
$_SESSION['authorised'] = true;
$_SESSION['username'] = $m_strUsername;
header("Location: logout.php");
}
else {
echo "Please check your Username or Password";
header("Location: login.htm");
}
}
// Close the conenction
mysql_close($connect);
exit;
?
Sample code for logout in MySQL database using php.
?php
session_start();
$_SESSION['authorised'] = false;
unset($_SESSION['authorised']);
unset($_SESSION['username']);
session_destroy();
header("Location: login.htm");
?>
Sample code for db information.
$server = "localhost";
$db_username="UserName";
$db_password="******";
$db_name="YourDatabaseName";
$tb_name="members";
$create_table = "CREATE TABLE IF NOT EXISTS `members` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`username` varchar(65) COLLATE utf8_bin NOT NULL,
`fullname` varchar(65) COLLATE utf8_bin NOT NULL,
`email` varchar(65) COLLATE utf8_bin NOT NULL,
`password` varchar(65) COLLATE utf8_bin NOT NULL,
`phonenumber` int(14) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=3";
?