I am following a tutorial to do a log in system but unfortunately things are not working for me. When I try to log in, I am always re-directed to the log in form again instead of the k_panel.php that I wish! I already checked the code like a trillion times with the one supplied on the tutorial, but still nothing...
I Firebugged it and I got this error:
"document.logform.pass is undefined " and it was pointing on this part of the validation code "if ( document.logform.pass.value == "" ) { ".
What does this mean?! What should I do?
I will post the complete code bellow so that anybody can have a better picture:
<?php
if ($_POST['email']) {
//Connect to the database through our include
include_once "connect_to_mysql.php";
$email = stripslashes($_POST['email']);
$email = strip_tags($email);
$email = mysql_real_escape_string($email);
$password = ereg_replace("[^A-Za-z0-9]", "", $_POST['password']); // filter everything but numbers and letters
$password = md5($password);
// Make query and then register all database data that -
// cannot be changed by member into SESSION variables.
// Data that you want member to be able to change -
// should never be set into a SESSION variable.
$sql = mysql_query("SELECT * FROM members WHERE email='$email' AND password='$password' AND emailactivated='1'");
$login_check = mysql_num_rows($sql);
if($login_check > 0){
while($row = mysql_fetch_array($sql)){
// Get member ID into a session variable
$id = $row["id"];
session_register('id');
$_SESSION['id'] = $id;
// Get member username into a session variable
$username = $row["username"];
session_register('username');
$_SESSION['username'] = $username;
// Update last_log_date field for this member now
mysql_query("UPDATE members SET lastlogin=now() WHERE id='$id'");
// Print success message here if all went well then exit the script
header("location: k_panel.php?id=$id");
exit();
} // close while
} else {
// Print login failure message to the user and link them back to your login page
print '<br /><br /><font color="#fd0404", line-height:30px; vertical-align:middle; color:#fff; font-weight:bold;">Du finns inte i vårt system. Var vänlig och försök igen!</font><br />
<br /><a href="login.php">Klicka här</a> för att logga in.';
exit();
}
}// close if post
?>
<!DOCTYPE html>
<head>
<!-- Form Validation -->
<script type="text/javascript">
function validate_form ( ) {
valid = true;
if ( document.logform.email.value == "" ) {
alert ( "Var vänlig och skriv ditt användarnamn!" );
valid = false;
}
if ( document.logform.pass.value == "" ) {
alert ( "Var vänlig och skriv ditt lösenord!" );
valid = false;
}
return valid;
}
</script>
<!-- Form Validation -->
</head>
<body>
<form action="login.php" method="post" enctype="multipart/form-data" name="logform" id="logform" onsubmit="return validate_form ( );">
Skriv ditt e-post adress här:<br>
<input name="email" type="text" id="email" size="30" maxlength="64" class="user"/><br><br>
Skriv ditt lösenord här:<br>
<input name="password" type="password" id="password" size="30" maxlength="24" class="pass"/><br><br>
<input type="submit" name="submit" value="Login" class="login">
</form>
</body>
</html>
And here is the code of the page where it should be heading into (k_panel.php):
this is the code I have on the page that it should be heading to:
<?php
session_start(); // Must start session first thing
// See if they are a logged in member by checking Session data
$toplinks = "";
if (isset($_SESSION['id'])) {
// Put stored session variables into local php variable
$username = $_SESSION['username'];
$toplinks = '<p style="background:#48c248; line-height:30px; vertical-align:middle; color:#fff; font-weight:bold;">Välkomen . $username . </a> | <a href="logout.php">Log Out</a></p>';
} else {
header("Location: login.php");
}
?>