login.php (some html has been removed but was only there for styling etc)
<?php
session_start();
?>
<html>
<body>
<?php
if( isset($_SESSION['ERRMSG_ARR']) && is_array($_SESSION['ERRMSG_ARR']) && count($_SESSION['ERRMSG_ARR']) >0 ) {
foreach($_SESSION['ERRMSG_ARR'] as $msg) {
echo $msg;
}
unset($_SESSION['ERRMSG_ARR']);
}
?>
<form action="test.php" method="post" id="contactform">
<fieldset>
<label>Username</label>
<input type="text" name="uname" class="textfield" id="uname" value="" />
<div class="contact-column-right">
<label>Password</label>
<input type="password" name="pass" class="textfield" id="pass" value="" />
<label> </label>
<input type="submit" name="submit" value="Login" class="button" />
</div>
</fieldset>
</form>
</body>
</html>
This form posts to the following php script (test.php)
<?php
//Start session
session_start();
//Include database connection details
require_once('config.inc');
//Array to store validation errors
$errmsg_arr = array();
//Validation error flag
$errflag = false;
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
//Sanitize the POST values
$login = clean($_POST['uname']);
$password = clean($_POST['pass']);
$pwd = hash('sha256', $password);
//Input Validations
if($login == '') {
$errmsg_arr[] = 'Login ID missing';
$errflag = true;
}
if($password == '') {
$errmsg_arr[] = 'Password missing';
$errflag = true;
}
//If there are input validations, redirect back to the login form
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: login.php");
exit();
}
//Create query
$qry="SELECT * FROM tbl_user WHERE u_name='$login' AND password='$pwd'";
$result=mysql_query($qry);
//Check whether the query was successful or not
if($result) {
if(mysql_num_rows($result) == 1) {
//Login Successful
session_regenerate_id();
$member = mysql_fetch_assoc($result);
$_SESSION['SESS_MEMBER_ID'] = $member['id_user'];
$_SESSION['SESS_FIRST_NAME'] = $member['f_name'];
$_SESSION['SESS_LAST_NAME'] = $member['s_name'];
session_write_close();
header("location: member-index.php");
exit();
}else {
//Login failed
header("location: login.php");
exit();
}
}else {
die("Query failed");
}
?>
on successful login the script should redirect to member-index.php
but no matter if i log in with the correct or incorrect details it always directs to login.php
The database connection is fine, I've test the sql & all table names/ row names are all correct.
Ive used this script on other sites and it has worked fine before and i have no idea what is going wrong this time.
As always everybody's help is very much appreciated.
Thank you

New Topic/Question
Reply




MultiQuote








|