Admin Login Form Doesn't Redirect
Posted: Sun Feb 06, 2011 2:53 pm
Hey guys,
I'm kind of a n00b with PHP and i'm trying to practice by building a mock e-comm site, but i'm having a problem with my admin login form. When the information is submitted the form just clears and doesn't redirect me to the index.php file i have set-up. That is, I don't get any error messages, the information disappears and I remain on the login page. My knowledge of php isn't where i'd like it to be yet, so i'm here for help! I'll post the code for both the admin login page and the index.php file.
ADMIN LOGIN PAGE
|
|
V
INDEX.PHP FILE
|
|
V
Any help and suggestions are greatly appreciated! Thanks!
I'm kind of a n00b with PHP and i'm trying to practice by building a mock e-comm site, but i'm having a problem with my admin login form. When the information is submitted the form just clears and doesn't redirect me to the index.php file i have set-up. That is, I don't get any error messages, the information disappears and I remain on the login page. My knowledge of php isn't where i'd like it to be yet, so i'm here for help! I'll post the code for both the admin login page and the index.php file.
ADMIN LOGIN PAGE
|
|
V
Code: Select all
<?php
session_start();
if (isset($_SESSION["username"])) {
header("location: index.php");
exit();
}
?>
<?php
if (isset($_POST["username"]) && isset($_POST["password"])){
$username = $_POST["username"]; // filter everything but numbers and letters
$password = $_POST["password"]; // filter everything but numbers and letters
include "../storescripts/connect_to_mysql.php";
$sql = mysql_query("SELECT id FROM admin WHERE username='$username' AND password='$password' LIMIT 1");
$existCount = mysql_num_rows($sql); // count the row nums
if ($existCount == 1) { // evaluate the count
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
}
$_SESSION["id"] = $id;
$_SESSION["username"] = $username;
$_SESSION["password"] = $password;
header("location: index.php");
exit();
} else {
echo 'That information is incorrect, try again <a href="index.php">Click Here</a>';
exit();
}
}
?>
<!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>Store Admin Area</title>
<link rel="stylesheet" type="text/css" href="../css/main_style.css" />
</head>
<body>
<div id="wrapper">
<div id="text"><br />
<div align="left" style="margin-left:100px; margin-top:100px;">
<h2>Please Login To Manage The Store</h2>
<br /><br />
<form id="form1" name="form1" method="post" action="admin_login.php">
<strong>Username</strong>
<input name="username" type="text" id="username" size="40" />
<br /><br />
<strong>Password</strong>
<input name="password" type="password" id="password" size="40" />
<br />
<br />
<input type="submit" name="button" id="button" value="Login" />
</form>
</div>
</div><!--closes wrapper-->
</body>
</html>
INDEX.PHP FILE
|
|
V
Code: Select all
<?php
session_start();
if (!isset($_SESSION["username"])) {
header("location: admin_login.php");
exit();
}
$usernameID = preg_replace('#[^0-9]#i', '', $_SESSION["id"]);
$username = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["username"]);
$password = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["password"]);
include "../storescripts/connect_to_mysql.php";
$sql = mysql_query("SELECT * FROM admin WHERE id='$usernameID' AND username='$username' AND password='$password' LIMIT 1"); // query the person
$existCount = mysql_num_rows($sql); // count the row nums
if ($existCount == 0) { // evaluate the count
echo "Your login session data is not on record in the database.";
exit();
}
?>
<!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>Store Admin Area</title>
<link rel="stylesheet" type="text/css" href="../css/main_style.css" />
</head>
<body>
<div id="wrapper">
<div id="text"><br />
<div align="left" style="margin-left:100px; margin-top:100px;">
<h2>Hello store manager, what would you like to do today?</h2>
<p><a href="inventory_list.php">Manage Inventory</a><br />
<a href="#">Manage Blah Blah </a></p>
</div>
<br />
<br />
<br />
</div><!--closes wrapper-->
</body>
</html>