PHP Register Form

  • bgraybr
  • Novice
  • Novice
  • bgraybr
  • Posts: 26

Post 3+ Months Ago

I'm trying to use PHP to create a registration form.

I have this:

Code: [ Select ]
 
<?php
 
//connect to database
require_once "connect.php";
 
$error = "Unknown error.";
 
//check the email, password, etc.
 
$email = $_POST["email"];
$name = $_POST["name"];
$pass = $_POST["password"];
$passval = $_POST["passwordval"];
 
 
  1.  
  2. <?php
  3.  
  4. //connect to database
  5. require_once "connect.php";
  6.  
  7. $error = "Unknown error.";
  8.  
  9. //check the email, password, etc.
  10.  
  11. $email = $_POST["email"];
  12. $name = $_POST["name"];
  13. $pass = $_POST["password"];
  14. $passval = $_POST["passwordval"];
  15.  
  16.  


I'm trying to make this part of the code make sure that the email is valid, that the username is the correct length, etc. I have a variable called $checkid, if its value is 1 the info will be send to the database and if its 0 it will echo an error ($error). The problem I'm having is that no matter what I enter the code always tells me that my password isn't long enough. What'd I do wrong?
Code: [ Select ]
 
$checkid = 1;
 
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $checkid = 0;
    $error = "Your email is not valid.";
}
   
if (strlen($name) < 4) {
    $checkid = 0;
    $error = "Your username must be at least 4 characters long.";
}
   
if (strlen($pass) < 6) {
    $checkid = 0;
    $error = "Your password must be at least 6 characters long.";
}
 
if ($pass != $passval) {
    $checkid = 0;
    $error = "Your passwords do not match";
}
 
//code that generates an id
 
$filename = 'genid.txt';
$handle = fopen($filename, 'r');
 
if (flock($handle, LOCK_SH)) {
    $hits = trim(fgets($handle)) + 1;
    flock($handle, LOCK_UN);
} else {
    $checkid = 0;
}
 
fclose($handle);
 
$handle = fopen($filename, 'w');
 
if (flock($handle, LOCK_EX)) {
    fwrite($handle, $hits);
    flock($handle, LOCK_UN);
} else {
    $checkid = 0;
}
 
fclose($handle);
 
//and finally send the data to the data base
 
if ($checkid == 1) {
 
    mysql_query("INSERT INTO users (userid, email, name, pass)
    VALUES ('".$genid."', '".$email."', '".$name."', '".$pass."')");
 
    //<!--begin html--> <- omg html comment
 
    echo "You have successfully created an account.";
 
    echo "<br />";
    echo $email;
    echo "<br />";
    echo $name;
    echo "<br />";
    echo $pass;
   
} elseif ($checkid == 0) {
    echo $error ." Click <a href=\"http://www.randomns0123.com/contact/\">here</a> if you have an error to report.";
}
 
mysql_close($con);
 
?>
 
  1.  
  2. $checkid = 1;
  3.  
  4. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  5.     $checkid = 0;
  6.     $error = "Your email is not valid.";
  7. }
  8.    
  9. if (strlen($name) < 4) {
  10.     $checkid = 0;
  11.     $error = "Your username must be at least 4 characters long.";
  12. }
  13.    
  14. if (strlen($pass) < 6) {
  15.     $checkid = 0;
  16.     $error = "Your password must be at least 6 characters long.";
  17. }
  18.  
  19. if ($pass != $passval) {
  20.     $checkid = 0;
  21.     $error = "Your passwords do not match";
  22. }
  23.  
  24. //code that generates an id
  25.  
  26. $filename = 'genid.txt';
  27. $handle = fopen($filename, 'r');
  28.  
  29. if (flock($handle, LOCK_SH)) {
  30.     $hits = trim(fgets($handle)) + 1;
  31.     flock($handle, LOCK_UN);
  32. } else {
  33.     $checkid = 0;
  34. }
  35.  
  36. fclose($handle);
  37.  
  38. $handle = fopen($filename, 'w');
  39.  
  40. if (flock($handle, LOCK_EX)) {
  41.     fwrite($handle, $hits);
  42.     flock($handle, LOCK_UN);
  43. } else {
  44.     $checkid = 0;
  45. }
  46.  
  47. fclose($handle);
  48.  
  49. //and finally send the data to the data base
  50.  
  51. if ($checkid == 1) {
  52.  
  53.     mysql_query("INSERT INTO users (userid, email, name, pass)
  54.     VALUES ('".$genid."', '".$email."', '".$name."', '".$pass."')");
  55.  
  56.     //<!--begin html--> <- omg html comment
  57.  
  58.     echo "You have successfully created an account.";
  59.  
  60.     echo "<br />";
  61.     echo $email;
  62.     echo "<br />";
  63.     echo $name;
  64.     echo "<br />";
  65.     echo $pass;
  66.    
  67. } elseif ($checkid == 0) {
  68.     echo $error ." Click <a href=\"http://www.randomns0123.com/contact/\">here</a> if you have an error to report.";
  69. }
  70.  
  71. mysql_close($con);
  72.  
  73. ?>
  74.  
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

That isn't a good script... what if the name is not long enough but your password is long enough... that would make the $checkid '0' and let the script create the account.

I've tried that before... really :lol:

A better way to do this is to check for the false values (that would produce an error... just like you're doing), but instead, create an array of errors... initiate the array before you start checking with an empty array, and then if there is an error, put it into the array.

Then, when you are checking if you are going to create the account or not, you count the size of the array and if it is greater then 0 (obviously) you create the account... otherwise, you print the errors for the user to read.

I don't know why you are getting that error... make sure the password you are putting in is actually greater then 6 characters... not even 6.
  • bgraybr
  • Novice
  • Novice
  • bgraybr
  • Posts: 26

Post 3+ Months Ago

I took your advice on using an array instead- but the problem now is it says the email is incorrect no matter what. I've been working on it for an hour and I'm stuck. Probably some stupid careless mistake that I overlooked, can any of you find the error?
http://testing.randomns0123.com/creator.txt
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

I don't know why you are getting problems... I tested your script and everything worked fine...

Try the following code:
Code: [ Select ]
<?php

require_once "connect.php";

$checkerr = array("");

//check the email, password, etc.

$email = $_POST["email"];
$name = $_POST["name"];
$pass = $_POST["password"];
$passval = $_POST["passwordval"];

if (filter_var($email, FILTER_VALIDATE_EMAIL) == false) {
    $checkerr[] = "The email you entered is not valid.";
} elseif (strlen($name) < 4) {
    $checkerr[] = "Your username must be more than 4 characters in length.";
} elseif (strlen($pass) < 6) {
    $checkerr[] = "Your password must be more than 6 characters in length.";
} elseif ($pass != $passval) {
    $checkerr[] = "The two passwords you entered do not match.";
}

//code that generates an id

$filename = 'genid.txt';
$handle = fopen($filename, 'r');

if (flock($handle, LOCK_SH)) {
    $genid = trim(fgets($handle)) + 1;
    flock($handle, LOCK_UN);
} else {
    $checkerr[] = "flock() failed. Contact me.";
}

fclose($handle);

$handle = fopen($filename, 'w');

if (flock($handle, LOCK_EX)) {
    fwrite($handle, $genid);
    flock($handle, LOCK_UN);
} else {
    $checkerr[] = "flock() failed twice. Contact me.";
}

fclose($handle);

//and finally send the data to the data base

if (count($checkerr) == 0) {

    mysql_query("INSERT INTO users (userid, email, name, pass)
    VALUES ('".$genid."', '".$email."', '".$name."', '".$pass."')");

    //<!--begin html--> <- omg html comment

    echo "You have successfully created an account.";

    echo "<br />";
    echo $email;
    echo "<br />";
    echo $name;
    echo "<br />";
    echo $pass;
    
} elseif (count($checkerr) != 0) {
    echo "<ol>";
    foreach($checkerr as $err)
    {
        echo "<li>{$err}</li>";
    }
    echo "</ol>";
    echo "<br />Click <a href=\"http://www.randomns0123.com/contact/\">here</a> if you have an error to report.";
}

mysql_close($con);

?>
  1. <?php
  2. require_once "connect.php";
  3. $checkerr = array("");
  4. //check the email, password, etc.
  5. $email = $_POST["email"];
  6. $name = $_POST["name"];
  7. $pass = $_POST["password"];
  8. $passval = $_POST["passwordval"];
  9. if (filter_var($email, FILTER_VALIDATE_EMAIL) == false) {
  10.     $checkerr[] = "The email you entered is not valid.";
  11. } elseif (strlen($name) < 4) {
  12.     $checkerr[] = "Your username must be more than 4 characters in length.";
  13. } elseif (strlen($pass) < 6) {
  14.     $checkerr[] = "Your password must be more than 6 characters in length.";
  15. } elseif ($pass != $passval) {
  16.     $checkerr[] = "The two passwords you entered do not match.";
  17. }
  18. //code that generates an id
  19. $filename = 'genid.txt';
  20. $handle = fopen($filename, 'r');
  21. if (flock($handle, LOCK_SH)) {
  22.     $genid = trim(fgets($handle)) + 1;
  23.     flock($handle, LOCK_UN);
  24. } else {
  25.     $checkerr[] = "flock() failed. Contact me.";
  26. }
  27. fclose($handle);
  28. $handle = fopen($filename, 'w');
  29. if (flock($handle, LOCK_EX)) {
  30.     fwrite($handle, $genid);
  31.     flock($handle, LOCK_UN);
  32. } else {
  33.     $checkerr[] = "flock() failed twice. Contact me.";
  34. }
  35. fclose($handle);
  36. //and finally send the data to the data base
  37. if (count($checkerr) == 0) {
  38.     mysql_query("INSERT INTO users (userid, email, name, pass)
  39.     VALUES ('".$genid."', '".$email."', '".$name."', '".$pass."')");
  40.     //<!--begin html--> <- omg html comment
  41.     echo "You have successfully created an account.";
  42.     echo "<br />";
  43.     echo $email;
  44.     echo "<br />";
  45.     echo $name;
  46.     echo "<br />";
  47.     echo $pass;
  48.     
  49. } elseif (count($checkerr) != 0) {
  50.     echo "<ol>";
  51.     foreach($checkerr as $err)
  52.     {
  53.         echo "<li>{$err}</li>";
  54.     }
  55.     echo "</ol>";
  56.     echo "<br />Click <a href=\"http://www.randomns0123.com/contact/\">here</a> if you have an error to report.";
  57. }
  58. mysql_close($con);
  59. ?>
  • bgraybr
  • Novice
  • Novice
  • bgraybr
  • Posts: 26

Post 3+ Months Ago

Bogey wrote:
I don't know why you are getting problems... I tested your script and everything worked fine...

Are you absolutely sure it worked?
You can check it on my site here. The script is still giving me the same error- "Your email is not valid.".

I changed this:
Code: [ Select ]
 
$checkerr = array("");
 
  1.  
  2. $checkerr = array("");
  3.  


To this:
Code: [ Select ]
 
$checkerr = array();
 
  1.  
  2. $checkerr = array();
  3.  


...because it was giving me a blank error like this:

1.
2. Your email is not valid.

...but now its still doing the email thing:

1. Your email is not valid.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

I don't get the error with the email... I get the error with the username though... I always the following error:
Quote:
Your username must be more than 4 characters in length.

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 90 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.