I have just started working with PHP to try and get a form that will redirect a user based on the redirect URL that is in their database profile. However, the code that I currently have is not working properly. The login form submits and opens the PHP page that is supposed to process the information, but nothing happens after that. I will include all code and a live test for easier solutions.
Live Example: http://previews.justinwidener.com/
HTML CODE:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Testing</title>
<link href="styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form name="custlogin" id="custlogin" action="custlogin.php" method="post">
<input class="custlogin_input" name="username" type="text" placeholder="Username" /><br />
<input class="custlogin_input" name="password" type="password" placeholder="Password" /><br />
<input class="custlogin_sub" name="log_btn" type="submit" value="LOGIN" />
</form>
</body>
</html>
PHP CODE:
<?php
session_start();
if ( ($_SERVER['REQUEST_METHOD'] != 'POST') && !isset($_SESSION['username']) ){
header('Location: $redirect');
}
?>
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
$host="localhost";
$username="********";
$password="**********";
$db_name="************";
$tbl_name="testtable";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$username=$_POST['username'];
$password=$_POST['password'];
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
$sql = "SELECT * FROM $tbl_name WHERE username='$username' and password='$password'";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
if($count == 1){
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
$result = mysql_fetch_array($result);
$redirect = trim($result['redirect']);
if ($redirect == '') {
echo "No redirect value was set!";
}
else {
header('Location: $redirect');
exit;
}
}
else {
echo "Wrong password!";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Widener Web Designs - Preview</title>
<link href="styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
</body>
</html>
The database is making a connection I assume since no errors are being thrown, but the issue still lies in the failure to redirect. If this is a simple problem, please do not make me out to be a moron. I am trying to learn and have not had any luck getting this to work. Thanks in advance.