0

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>';
}

1 Answer 1

1
if (strlen(isset($_POST['password'])) < 8) {

would you try to strlen on and boolean so try to

if(isset($_POST['password'] && strlen($_POST['password']) < 8) {

Moreover you do this

if (isset($_POST['password']) !== isset($_POST['passwordconfirm'])) {

which do not check if the passwords are equals, it check if there is one set and one not this should something like this

if (isset($_POST['password'],$_POST['passwordconfirm']) 
    && $_POST['password'] !== $_POST['passwordconfirm']) {

same here

if (filter_var(isset($_POST['email']), FILTER_VALIDATE_EMAIL)) {

should be

if (isset($_POST['email']) && !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {

You may look at the documentation for isset(), strlen()

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the fast response. It fixed the problem with the password length and confirmation, however, it still doesnt matter if I put in an invalid email address :/ ...Moreover, it now doesnt give me an error message telling me what went wrong, instead it only puts out "1" ?
you have in ur code a random isset($errors); you may want there an if(..)
thanks, that fixed the problem with the error message only putting out a 1. However, for some reason the email validation still is not working, and that is the biggest problem, since I will use that as the users id....
@ProBeastC did you read that already in my answer there is something about the email
yeah I read that, and tried to fix it that way.. but it still doesnt work
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.