Jump to content

what's wrong with this registration form!?


samoi

Recommended Posts

<?php

/**
* @author samoi
* @copyright 2009
*/
error_reporting(E_ALL & E_NOTICE);
include ('func.php');

if (!isset($_POST['submit'])) {

    echo '<form action="" method="post">
  username: <input type="text" name="username" />
  <br />
  password: <input type="password" name="password" />
  <br />
  email:
  <br />
  <input type="text" name="email" />
  <input type="submit" name="submit" value="register" />
  </form>';

} else {

    if (isset($_POST['username']) && isset($_POST['password']) && isset($_POST['email'])) {

        $user = $_POST['username'];
        $pass = md5($_POST['password']);
        $email = $_POST['email'];
        if (checkreg($user, $email)) {
            register($user, $pass, $email);

            echo 'Welcome ' . $user . ' You have been sent an email regarding your registration!';
        } else {
            echo 'There is a user or email with that user or email you entered!';
        }


    } else {
        echo 'All fields are required!';
    }

}
?>

 

 

this code keeps registering me if a record in function checkreg does not exist! but if it exist it says there is a user or email!

 

The problem is! if the inputs are empty! it registers empty values ! it doesn't return 'all input are required!' what is wrong with it!?

 

it drives me crazy!

 

Help please!

Link to comment
https://forums.phpfreaks.com/topic/176423-whats-wrong-with-this-registration-form/
Share on other sites

Try this [untested]

 

<?php

/**
* @author samoi
* @copyright 2009
*/
error_reporting(E_ALL & E_NOTICE);
include ('func.php');

if (!isset($_POST['submit'])) {
    echo '<form action="" method="post">
  username: <input type="text" name="username" />
  <br />
  password: <input type="password" name="password" />
  <br />
  email:
  <br />
  <input type="text" name="email" />
  <input type="submit" name="submit" value="register" />

  </form>';
} else {

    if (isset(!$_POST['username']) && isset(!$_POST['password']) && isset(!$_POST['email'])) {

       echo 'All fields are required!';

        } else {

        $user = $_POST['username'];
        $pass = md5($_POST['password']);
        $email = $_POST['email'];
        if (checkreg($user, $email)) {
            register($user, $pass, $email);

            echo 'Welcome ' . $user . ' You have been sent an email regarding your registration!';
}
else
{
            echo 'There is a user or email with that user or email you entered!';
}
}
}
?>

 

The problem is your use of isset, the following line will ALWAYS be true.

 

if (isset($_POST['username']) && isset($_POST['password']) && isset($_POST['email'])) {

 

What the isset function does is checks if the variable exists, in this case it checks the $_POST array to make sure there are items with the key of username, password and email. If your form has been submitted these values will always be set, because the values of text and password inputs are submitted even if the text/password boxes are empty. I would personally replace that line with this...

 

if (!empty($_POST['username']) && !empty($_POST['password']) && !empty($_POST['email'])) {

 

That way you are checking if it has a value. You could check isset aswell if you wanted, but you should find that as you've already checked for submit you shouldn't need to. Having said that I personally tend to have my form with the following items in it rather than checking the actual submit button...

 

<input type="hidden" name="submit" value="1" />
<input type="submit" value="Register/Login/Etc" />

 

It may seem a bit daft, and depending on what else you have on the form it can be. But it is possible on some browsers to submit a form with the Return key, and when that happens the <input type="submit" /> element is not always posted.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.