Jump to content

Fatal error and error array problem


fullyloaded

Recommended Posts

Hi,

I've been working on my sign up form and am having a few problems getting it to work. First problem i'm having is this error:

Fatal error: Call to undefined function validadres() in on line 22

i think it has to do with the function being in the wrong place but could be wrong. My second problem is the errors array, if i leave any of the text box fields empty or if the passwords don't match it will still add the user to the database and not show the error messages.

Thanks.

require("db.php");
if(!empty($_POST))
{
  $errors = array();
   if ($user == "") { $errors[] = "Please Enter A Username."; }
   if ($pass == "") { $errors[] = "Please Enter A Password."; }
   if ($confirmpass == "") { $errors[] = "Please Enter A confirmation password."; }
   if ($email == "") { $errors[] = "Please Enter Your E-mail Address."; }
   if ($comfirmemail == "") { $errors[] = "Please Enter Your Confirmation E-mail Address."; }
   if ($gender == "") { $errors[] = "Please Select A Gender."; }
   if ($month == "") { $errors[] = "Please Select A Month."; }
   if ($day == "") { $errors[] = "Please Select A Day."; }
   if ($year == "") { $errors[] = "Please Select A Year."; }
   if ($country == "") { $errors[] = "Please Select A Country."; }
   if ($firstname == "") { $errors[] = "Please Enter Your First Name."; }
   if ($lastname == "") { $errors[] = "Please Enter Your Last Name."; }
   if (strlen($pass) < $passLengthMIN ) { $errors[] = "The password contains to little chars."; }
   if (strlen($pass) > $passLengthMAX ) { $errors[] = "The password contains to much chars."; }
   if (strlen($user) < $userLengthMIN ) { $errors[] = "The username contains to little chars."; }
   if (strlen($user) > $userLengthMAX ) { $errors[] = "The username contains to much chars."; }
   if (validadres($email) == false ) { $errors[] = "The given e-mail address is not valid."; }
   if ($pass  <>  $confirmpass) { $errors[] = "Passwords do not match."; }
   if ($email  <>  $comfirmemail) { $errors[] = "Email Address do not match."; }
   $query = "SELECT id FROM users WHERE user = :user Or email = :email";
   $query_params = array(
   ':user' => $_POST['user'],
   ':email' => $email
);
try
{
    $stmt = $db->prepare($query);
    $result = $stmt->execute($query_params);
}
catch(PDOException $ex)
{
   die("Failed to run query: " . $ex->getMessage());
}
$row = $stmt->fetch();
if ($row = $stmt->fetch($result)){ 
    if  ($row["user"] == $user) { $errors[] = "Your username is already used by another member."; }
    if  ($row["email"] == $email) { $errors[] = "Your e-mail address is already registrated in our database."; }
}
if ($errors) {
    $errorstr = "<ul><li>" . implode("</li><li>", $errors) . "</li></ul>";
    } else {
    $errorstr = "";
}
   $query = "INSERT INTO users (user,pass,salt,email,month,day,year,firstname,lastname,gender,country
   ) VALUES (:user,:pass,:salt,:email,:month,:day,:year,:firstname,:lastname,:gender,:country)";
   $salt = dechex(mt_rand(0, 2147483647)) . dechex(mt_rand(0, 2147483647));
   $pass = hash('sha256', $_POST['pass'] . $salt);
   $query_params = array(
   ':user' => $_POST['user'],
   ':pass' => $pass,
   ':salt' => $salt,
   ':email' => $_POST['email'],
   ':month' => $_POST['month'],
   ':day' => $_POST['day'],
   ':year' => $_POST['year'],
   ':firstname' => $_POST['firstname'],
   ':lastname' => $_POST['lastname'],
   ':gender' => $_POST['gender'],
   ':country' => $_POST['country']
);
try
{
    $stmt = $db->prepare($query);
    $result = $stmt->execute($query_params);
}
catch(PDOException $ex)
{
   die("Failed to run query: " . $ex->getMessage());
  }
  /////////EMAIL VALID CODE////////////////////////
function validadres($email){
    $prereturn = true;
    if (strlen($email) < 5){$prereturn = false;}
    $partsNumber = split("@",$email);
    if (count($partsNumber) <> 2) {$prereturn = false;}
    else{
        list($user,$domain) = split("@",$email);
        if (strlen($user) < 1) {$prereturn = false;}
    }
    return $prereturn;
}
//////END//////////////////////////////////////////
  header("Location: login");
  exit;
}

 

 

Link to comment
Share on other sites

Start from here:

if (validadres($email) == false ) { $errors[] = "The given e-mail address is not valid."; }

 

You can not assign boolean or some values like this to the function. This is wrong!

Start to re-design the script, I found deprecated php functions too.

Link to comment
Share on other sites

Start from here:

if (validadres($email) == false ) { $errors[] = "The given e-mail address is not valid."; }

 

You can not assign boolean or some values like this to the function. This is wrong!

Start to re-design the script, I found deprecated php functions too.

 

This isn't an assignment, it's a comparison.

 

The issue is the function being declared within the if/else blocks. If you want it pre-parsed before the rest of the code, it must be outside any conditionals.

 

If you want it conditionally declared, you're going to have to declare it BEFORE using it.

Link to comment
Share on other sites

Fatal error: Call to undefined function validadres() in on line 22

"Address" is spelled wrong, that just bothers me because you'll never know the names of your functions if they're all spelled wrong.

 

Define all your functions in the global scope at the top of the file (or in a separate include file) so you know they're always accessible.

 

 

Link to comment
Share on other sites

The reason the user gets inserted is because that's what you told it to do. The INSERT statements are outside of the if test. So, the INSERT will always be executed.

if ($errors) {
    $errorstr = "<ul><li>" . implode("</li><li>", $errors) . "</li></ul>";
    } else {
    $errorstr = "";
}
   $query = "INSERT INTO users (user,pass,salt,email,month,day,year,firstname,lastname,gender,country
   ) VALUES (:user,:pass,:salt,:email,:month,:day,:year,:firstname,:lastname,:gender,:country)";

 

Also: Where are you getting $user, et. al. from?

if(!empty($_POST))
{
  $errors = array();
   if ($user == "") { $errors[] = "Please Enter A Username."; }
   if ($pass == "") { $errors[] = "Please Enter A Password."; }

That looks suspiciously like register_globals is on. THIS IS A BAD THING.

 

Even with register_globals OFF, you need to trim those fields. Otherwise, a user can just enter spaces. So, it should be: if (trim($_POST['user']) == '') { #error condition

 

Also: You are SELECTing the id from the users table when checking for an existing username or email address. But you are then checking the user column and the email column, neither of which is there. You are also double-fetching the results which will put you past the end of the result set if there is only one result, and you are not checking all of the results if there are more than 1 (uhh, I mean 2).

 

   $query = "SELECT id FROM users WHERE user = :user Or email = :email";
#
# ... snip ...
#
$row = $stmt->fetch();
if ($row = $stmt->fetch($result)){ 
    if  ($row["user"] == $user) { $errors[] = "Your username is already used by another member."; }
    if  ($row["email"] == $email) { $errors[] = "Your e-mail address is already registrated in our database."; }
}

 

Work on your indentation. It will make the code much easier to read. I completely missed the fact that validadres is inside of the if block. In fact, you may not have meant to put it there in the first place.

 

You are asking the user for a lot of information. And then you die if any of it is bad. This means they have to find their way back to the registration page, and re-type everything. Since they have no control over what user names are already in use, this will get frustrating and cause people to give up trying to register, loosing you potential users. Learn about "sticky fields". Basically, you want the form and processing in the same script, so you can reshow the form along with any error messages and all of the data that the user already entered.

 

 

I'm not hating on your code. I have made every one of these errors at one time or another. Actually, I'm kind of glad my first development server crashed and lost the hard drive. I'll never again have to look at the garbage I wrote when I was learning PHP.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.