Location: PHPKode > projects > PHP MySQL Address Book > addressbook/index.php
<?php
//require("links.php");
// we must never forget to start the session
session_start();

$errorMessage = '';
if (isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) {
	include 'library/config.php';
	include 'library/opendb.php';
	
	$userId   = $_POST['txtUserId'];
	$password = $_POST['txtPassword'];
	
	// check if the user id and password combination exist in database
	$sql = "SELECT id,user,usr_level,isEnabled FROM cal_accounts WHERE user = '".$userId."' AND pass = '".md5($password.CAL_SQL_PASSWD_SALT)."'";
	//$sql = "SELECT id,user_id,usr_level,isEnabled FROM tbl_auth_user WHERE user_id = '".$userId."' AND user_password = '".$password."'";
	mysql_query("SET NAMES 'utf8'");
mysql_query('SET CHARACTER SET utf8'); 
	$result = mysql_query($sql) or die('Query failed. ' . mysql_error()); 
	if (mysql_num_rows($result) == 1)
	{
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	}
	
	if (mysql_num_rows($result) == 1 and $row['isEnabled']==1) {
		// the user id and password match, 
		// set the session
		$_SESSION['db_is_logged_in'] = true;
		//$row = mysql_fetch_array($result, MYSQL_ASSOC);
		$_SESSION['usr_level'] =$row['usr_level'];
		$_SESSION['usr_id'] =$row['id'];
		$_SESSION['usr_nam'] =$row['user'];
		$_SESSION['isEnabled'] =$row['isEnabled'];
		// after login we move to the main page
		header('Location: addresscard.php?type=sort&value=a');
		exit;
	} else {
		$errorMessage = 'Sorry, wrong user id / password';
		if (isset($_SESSION['db_is_logged_in'])) {
	unset($_SESSION['db_is_logged_in']);
}
	}
	
	include 'library/closedb.php';
}
?>
<html>
<head>
<title>
<?php
require("title.php");
?>
</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>

<body background="library/welcome-bg.gif">

<br>
<br>
<br>
<br><br><br><br><br><br><br><br>
<center>
<table width="33%" border="0" cellspacing="0" cellpadding="0" BACKGROUND="library/bg.gif">
  <tr>
    <td width="15">&nbsp;</td>
    <td width="370">&nbsp;</td>
    <td width="81">&nbsp;</td>
  </tr>
  <tr>
    <td colspan="3"><form action="" method="post" name="frmLogin" id="frmLogin">
 <table width="259" border="0" align="center" cellpadding="0" cellspacing="0" >
  <tr>
   <td width="100"><img src="library/user_suit.png">&nbsp;&nbsp;&nbsp;User Id</td>
   <td width="159"><input name="txtUserId" type="text" id="txtUserId"></td>
  </tr>
  <tr>
   <td width="100"><img src="library/application_key.png">&nbsp;&nbsp;&nbsp;Password</td>
   <td><input name="txtPassword" type="password" id="txtPassword"></td>
  </tr>
  <tr>
   <td width="100">&nbsp;</td>
   <td><input name="btnLogin" type="submit" id="btnLogin" value="Login"></td>
  </tr>
 </table>
</form></td>
    </tr>
  <tr>
    <td width="15">&nbsp;</td>
    <td width="370">&nbsp;</td>
    <td width="81">&nbsp;</td>
  </tr>
</table>
<?php
if ($errorMessage != '') {
?>
<p align="center"><strong><font color="#990000"><?php echo $errorMessage; ?></font></strong></p>
<?php
}
?>
</center>
</body>
</html>
Return current item: PHP MySQL Address Book