Hi,
I was trying to develop a login script using OO php.But I found it is not directing to the menu page even with the correct login details ,instead it is redirect to the login page.Even it is not giving any error messages.Can anybody tell me whats wrong there..Im here posting the 2 files.Thank you...
database.php
<?php
class database{
function __construct() {
$db_name = 'Lakkam';
$db_host = 'localhost';
$db_user = 'root';
$db_password = '';
$con = mysql_connect ($db_host,$db_user,$db_password)
or die("Could not connect to MySQL server. Please try again.");
mysql_select_db($db_name,$con)
or die("Could not connect to the database. Please try again");
}
function login($uname,$pass)
{ // after creating the database object you have to pass username & password
// username and password sent from form
$myusername=$_POST['username'];
$mypassword=md5($_POST['password']);
// To protect MySQL injection
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql=sprintf("SELECT user_id,user_name,user_password FROM 'tbl_admin' WHERE user_name = '$myusername' AND user_password ='mypassword'");
$query = mysql_query($sql);
if(mysql_num_rows($query) == 1) // checks if a user name with that password exsists
{
$row = mysql_fetch_assoc($query); // Put it to an associative array
session_start();
$_SESSION['user_id'] = $row['user_id'];
$_SESSION['logged_in'] = "ok";
header("Location: menu.php");
return true;
}
else
{
header("Location: login.php");
return false;
}
}
}
?>
login.php
<?php
include_once ('database.php'); // This is the location of your database class in your server
$con= new database(); // create the database object
if(isset($_POST['Submit'])){ // Check if the form is actually submitted
if($_POST['username']!='' && $_POST['password']!='')
{
if($con->login($_POST['username'],$_POST['password'])){ //check the username & password
}else{
$error = 'Incorrect Login Details ...Try again later!<br>';
}
}
else {
$error = 'Username Or Password Field is Empty..Please Check again!<br>';
}
}
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Lakkam Trade Center</title>
<meta name="keywords" content="" />
<meta name="description" content="" />
</head>
<body>
<div id="Layer1"><img src="images/login.jpg" alt="" width="149" height="132" /></div>
<p> </p>
<div align="center" class="style1" id="Layer2">Administrator Login </div>
<div id="Layer3">
<form id="form1" name="form1" method="post" >
<p>Username
<input type="text" name="username" />
</p>
<p> </p>
<p>Password
<input type="password" name="password" />
</p>
<p> </p>
<p> </p>
<p>
<input type="submit" name="Submit" value="Login" />
<input type="reset" name="Reset" value="Reset" />
<?php if(isset($error)){ echo "<span class=\"style6\">$error</span>";} //display errors?>
</p>
</div>
<div id="main">
<div id="header"></div>
</body>
</html>