simnorwebdesign
Feb 7th 2008, 6:53 am
Hi, I am currently working on a site at the moment where the user has to log in, I am working with code from here: http://www.php-mysql-tutorial.com/user-authentication/database.php
<?php
session_start();
$errorMessage = '';
if (isset($_POST['username']) && isset($_POST['password'])) {
include 'mysql_connect.php';
$userId = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT username FROM profile WHERE username = '$userId' AND password = PASSWORD('$password')";
$result = mysql_query($sql)
or die('Query failed. ' . mysql_error());
if (mysql_num_rows($result) == 1) {
$_SESSION['db_is_logged_in'] = true;
header('Location: memberhome.php');
exit;
} else {
$errorMessage = 'Sorry, wrong user id / password';
}
mysql_close($conn);
}
?>
<html>
<head>
<title>Basic Login</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php include 'header.php'; ?>
<div id="contentarea">
<div id="advert"><a href="#"><img src="images/ad.png" alt="advertisement" /></a></div>
<div id="contentarea_padding">
<div class="content">
<?php if ($errorMessage != '') { ?>
<?php echo $errorMessage; ?>
<?php } ?>
<div class="bannertitles">
<font color="#b7fd3d">log</font><font color="#fded36">in</font>
</div><br/><br/>
<form method="post" name="frmLogin" id="frmLogin">
<div class="inputcontainer">
<div class="label">Username:</div><input type="text" name="username" size="60" maxlength="60" /> </div>
<div class="inputcontainer">
<div class="label">Password:</div><input type="password" name="password" size="60" maxlength="60" /> </div>
<br/><br/>
<div class="inputcontainer"><div align="right">
<input type="submit" name="btnLogin" value="Login"></div></div>
</form>
</div>
</div></div>
<?php include 'sidebar.php'; ?>
<?php include 'footer.php'; ?>
</body>
</html>
and the memberhome.php:
<?php
session_start();
if (!isset($_SESSION['db_is_logged_in'])
|| $_SESSION['db_is_logged_in'] !== true) {
header('Location: login.php');
exit;
}
?>
<?php include 'header.php'; ?>
<div id="contentarea">
<div id="advert"><a href="#"><img src="images/ad.png" alt="advertisement" /></a></div>
<div id="contentarea_padding">
<div class="content">
success
</div>
</div></div>
<?php include 'sidebar.php'; ?>
<?php include 'footer.php'; ?>
The code all looks fine to me, only it doesn't go to memberhome.php when I put in the correct log in details. Is there something I need to change? Any help will be greatly appreciated.
Thanks
Simon
<?php
session_start();
$errorMessage = '';
if (isset($_POST['username']) && isset($_POST['password'])) {
include 'mysql_connect.php';
$userId = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT username FROM profile WHERE username = '$userId' AND password = PASSWORD('$password')";
$result = mysql_query($sql)
or die('Query failed. ' . mysql_error());
if (mysql_num_rows($result) == 1) {
$_SESSION['db_is_logged_in'] = true;
header('Location: memberhome.php');
exit;
} else {
$errorMessage = 'Sorry, wrong user id / password';
}
mysql_close($conn);
}
?>
<html>
<head>
<title>Basic Login</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php include 'header.php'; ?>
<div id="contentarea">
<div id="advert"><a href="#"><img src="images/ad.png" alt="advertisement" /></a></div>
<div id="contentarea_padding">
<div class="content">
<?php if ($errorMessage != '') { ?>
<?php echo $errorMessage; ?>
<?php } ?>
<div class="bannertitles">
<font color="#b7fd3d">log</font><font color="#fded36">in</font>
</div><br/><br/>
<form method="post" name="frmLogin" id="frmLogin">
<div class="inputcontainer">
<div class="label">Username:</div><input type="text" name="username" size="60" maxlength="60" /> </div>
<div class="inputcontainer">
<div class="label">Password:</div><input type="password" name="password" size="60" maxlength="60" /> </div>
<br/><br/>
<div class="inputcontainer"><div align="right">
<input type="submit" name="btnLogin" value="Login"></div></div>
</form>
</div>
</div></div>
<?php include 'sidebar.php'; ?>
<?php include 'footer.php'; ?>
</body>
</html>
and the memberhome.php:
<?php
session_start();
if (!isset($_SESSION['db_is_logged_in'])
|| $_SESSION['db_is_logged_in'] !== true) {
header('Location: login.php');
exit;
}
?>
<?php include 'header.php'; ?>
<div id="contentarea">
<div id="advert"><a href="#"><img src="images/ad.png" alt="advertisement" /></a></div>
<div id="contentarea_padding">
<div class="content">
success
</div>
</div></div>
<?php include 'sidebar.php'; ?>
<?php include 'footer.php'; ?>
The code all looks fine to me, only it doesn't go to memberhome.php when I put in the correct log in details. Is there something I need to change? Any help will be greatly appreciated.
Thanks
Simon