To finalize my website, I wanted to add a login feature. Everything worked out well, then I decided to add some php validation to the form. Most of the coding was done by dreamweaver itself, however I added the validation if-questions and that's where the mistake. Everytime I fill out the form, I get an error saying that my password needs to have 8 characters, no matter how many characters it has. If I get rid of this if-question, it is saying that the errors variable is undefined. If I then fix that problem with isset(), it seems to skip the following two if-questions. I hope I was able to get my point across and am looking forward to a response, since I am getting a little bit frustrated with this code now :P
Thanks, Jan
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ?mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
// *** Redirect if username exists
$MM_flag="MM_insert";
if (isset($_POST[$MM_flag])) {
$MM_dupKeyRedirect="registrationFailed.php";
$loginUsername = $_POST['email'];
$LoginRS__query = sprintf("SELECT Email FROM `Start-Login` WHERE Email=%s", GetSQLValueString($loginUsername, "text"));
mysql_select_db($database_login, $login);
$LoginRS=mysql_query($LoginRS__query, $login) or die(mysql_error());
$loginFoundUser = mysql_num_rows($LoginRS);
//if there is a row in the database, the username was found - can not add the requested username
if($loginFoundUser){
$MM_qsChar = "?";
//append the username to the redirect page
if (substr_count($MM_dupKeyRedirect,"?") >=1) $MM_qsChar = "&";
$MM_dupKeyRedirect = $MM_dupKeyRedirect . $MM_qsChar ."requsername=".$loginUsername;
header ("Location: $MM_dupKeyRedirect");
exit;
}
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if (empty($_POST) === false) {
$required_fields = array('name', 'email', 'password', 'passwordconfirm');
foreach ($_POST as $key=>$value) {
if (empty($value) && in_array($key, $required_fields) === true) {
$errors[] = 'Please fill out all requiered fields';
$lul = true;
break 1;
}
}
}
if (empty($errors) === true) {
if (strlen(isset($_POST['password'])) < 8) {
$errors[] = 'Your password must be at least 8 characters long';
$lil = true;
}
if (isset($_POST['password']) !== isset($_POST['passwordconfirm'])) {
$errors[] = 'Your passwords do not match';
$lil = true;
}
if (filter_var(isset($_POST['email']), FILTER_VALIDATE_EMAIL)) {
$errors[] = 'You must provide a valid email address';
$lil = true;
}
}
isset($errors);
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form") && (isset($lul) === false) && (isset($lil) === false)) {
$insertSQL = sprintf("INSERT INTO `Start-Login` (Email, Password, Name, `role`) VALUES (%s, %s, %s, %s)",
GetSQLValueString($_POST['email'], "text"),
GetSQLValueString($_POST['password'], "text"),
GetSQLValueString($_POST['name'], "text"),
GetSQLValueString($_POST['role'], "text"));
mysql_select_db($database_login, $login);
$Result1 = mysql_query($insertSQL, $login) or die(mysql_error());
$insertGoTo = "login.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
} else {
echo '<pre>', print_r($_POST, true), '</pre>';
echo '<pre>', print_r($errors, true), '</pre>';
}