Guest User

Untitled

a guest
Aug 22nd, 2018
1,917
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. <?
  2. // a class
  3. class User
  4. {
  5. public $user, $pass, $fname, $lname, $bday;
  6. public $user_err, $pass_err, $name_err;
  7.  
  8. public function __construct( $post_array ) {
  9. $this->user = $_POST['username'];
  10. $this->pass = $_POST['password'];
  11. $this->pass2 = $_POST['password2'];
  12. $this->fname = $_POST['first_name'];
  13. $this->lname = $_POST['last_name'];
  14. $this->bday = $_POST['birthday'];
  15. $this->city = $_POST['city'];
  16. $this->state = $_POST['state'];
  17. $this->address = $_POST['address'];
  18. $this->zip = $_POST['zip'];
  19.  
  20. $this->user_err = NULL;
  21. $this->pass_err = NULL;
  22. $this->name_err = NULL;
  23. $this->birth_err = NULL;
  24. $this->city_err = NULL;
  25. $this->state_err = NULL;
  26. $this->address_err = NULL;
  27. $this->zip_err = NULL;
  28. }
  29.  
  30. public function validate() {
  31.  
  32. // username isn't a duplicate
  33. if( !$this->user ) {
  34. $this->user_err = "Please specify username";
  35. } else if( duplicate( $this->user ) ) {
  36. $this->user_err = "That username is already in use";
  37. }
  38.  
  39. // passwords match and are at least 6 chars
  40. if( !$this->pass || strlen( $this->pass ) < 6 ) {
  41. $this->pass_err = "Password must be at least 6 characters";
  42. } else if( $this->pass != $this->pass2 ) {
  43. $this->pass_err = "Passwords do not match";
  44. }
  45. else
  46. $this->vpass = $_POST['password'];
  47.  
  48. // first/last name aren't blank
  49. if( !$this->fname && !$this->lname ) {
  50. $this->name_err = "Please provide a first and last name";
  51. }
  52. else if(!$this->fname){
  53. $this->name_err = "Please provide a first name";
  54. }
  55. else if(!$this->lname){
  56. $this->name_err = "Please provide a last name";
  57. }
  58.  
  59. if(!$this->bday){
  60. $this->birth_err = "Please enter your birthday";
  61. }
  62.  
  63. if(!$this->city){
  64. $this->city_err = "Please enter a city";
  65. }
  66.  
  67. if(!$this->state){
  68. $this->state_err = "Please enter a state";
  69. }
  70.  
  71. if(!$this->address){
  72. $this->address_err = "Please enter your address";
  73. }
  74.  
  75. if(!$this->zip){
  76. $this->zip_err = "Please enter your zip code";
  77. }
  78.  
  79. return !$this->has_errors();
  80. }
  81.  
  82. public function has_errors() {
  83. return $this->user_err || $this->pass_err || $this->name_err || $this->birth_err || $this->city_err || $this->state_err || $this->address_err || $this->zip_err;
  84. }
  85.  
  86. public function insert()
  87. {
  88. $sql = "
  89. INSERT INTO users
  90. (username, password, first_name, last_name, birthdate, city, state, address, zip)
  91. VALUES ( '$this->user', '$this->vpass' ,
  92. '$this->fname', '$this->lname', '$this->bday', '$this->city', '$this->state', '$this->address', '$this->zip');";
  93.  
  94. mysql_query( $sql ) or die( "Error( $sql): " . mysql_error() );
  95. }
  96. }
  97.  
  98. function duplicate( $username )
  99. {
  100. $sql = "SELECT id FROM users WHERE username = '$username'";
  101.  
  102. $result = mysql_query( $sql ) or die( "Error( $sql): " . mysql_error() );
  103.  
  104. return mysql_num_rows( $result ) > 0;
  105. }
  106.  
  107. ?>
Add Comment
Please, Sign In to add comment