We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,124 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Confirm Password and Password not equal to username

Hello, this one is probably really simple, but I am a noob... I just want the password confirmation function to work and I would also like the password to not equal the username.

I am running into a problem with how I set up the logic for my variables. Trying to fix my registration and login page. All the other logic statements work just fine.

Thank you.

<?php

class Register
{
  private $username;
  private $password;
  private $confirm_password;
  private $passmd5;
  private $email;
  private $errors;
  private $token;

  public function __construct()
  {
    $this->errors 			 = array();
    $this->username 		 = $this->filter($_POST['ruser']);
    $this->password 	         	 = $this->filter($_POST['rpass']);
    $this->confirm_password     = $this->filter($_POST['rconfirm_rpass']);
    $this->email    		         = $this->filter($_POST['remail']);
    $this->token  			 = $_POST['token'];
    $this->passmd5                   = md5($this->password);
  }

  public function process()
  {
    if($this->valid_token() && $this->valid_data())
         $this->register();

    return count($this->errors)? 0 : 1;
  }

  public function filter($var)
  {
    return preg_replace('/[^a-zA-Z0-9@.]/','',$var);
  }

  public function register()
  {
   mysql_connect("localhost","root","") or die(mysql_error());
   mysql_select_db("password") or die (mysql_error());

   mysql_query("INSERT INTO users(username,password) VALUES ('{$this->username}','{$this->passmd5}')");

   if(mysql_affected_rows()< 1)
     $this->errors[] = 'Could Not Process Form';
  }

  public function user_exists()
  {
  mysql_connect("localhost","root","") or die(mysql_error());
  mysql_select_db("password") or die (mysql_error());
  $data = mysql_query("SELECT ID FROM users WHERE username = '{$this->username}'");

  return mysql_num_rows($data)? 1 : 0;
  }

  public function show_errors()
  {
    echo "<h3>Errors</h3>";

    foreach($this->errors as $key=>$value)
      echo $value."<br>";
  }

  public function valid_data()
  {
    if($this->user_exists())
      $this->errors[] = 'Username Already Taken';
   
    if(empty($this->username))
      $this->errors[] = 'Invalid Username';
   
    if(empty($this->password))
      $this->errors[] = 'Invalid Password';
	
	if(($this->$password !== $this->$confirm_password))
      $this->errors[] = 'Passwords Must Match';
	
	if(($this->$username == $this->$password))
      $this->errors[] = 'Password Cannot Be Your Username';
   
    if(empty($this->email) || !eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z]{2,4}$',$this->email))
      $this->errors[] = 'Invalid Email';
  return count($this->errors)? 0 : 1;
  }


  public function valid_token()
  {
   if(!isset($_SESSION['token']) || $this->token != $_SESSION['token'])
     $this->errors[] = 'Invalid Submission';

   return count($this->errors)? 0 : 1;
  }
}

?>
2
Contributors
6
Replies
51 Minutes
Discussion Span
1 Year Ago
Last Updated
7
Views
cheelo007
Light Poster
31 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

replace

if(($this->$password !== $this->$confirm_password))
$this->errors[] = 'Passwords Must Match';

if(($this->$username == $this->$password))
$this->errors[] = 'Password Cannot Be Your Username';

with

if(($this->password !== $this->confirm_password))
$this->errors[] = 'Passwords Must Match';

if(($this->username == $this->password))
$this->errors[] = 'Password Cannot Be Your Username';
jstfsklh211
Junior Poster
100 posts since Apr 2011
Reputation Points: 34
Solved Threads: 27
Skill Endorsements: 1

replace

if(($this->$password !== $this->$confirm_password))
$this->errors[] = 'Passwords Must Match';

if(($this->$username == $this->$password))
$this->errors[] = 'Password Cannot Be Your Username';

with

if(($this->password !== $this->confirm_password))
$this->errors[] = 'Passwords Must Match';

if(($this->username == $this->password))
$this->errors[] = 'Password Cannot Be Your Username';

Whoops lol, I could have sworn I had that fixed earlier, but I get this error no matter if the variable was properly defined or not.

Fatal error: Cannot access empty property in /public_html/me-class.register.php on line 77

cheelo007
Light Poster
31 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Whoops lol, I could have sworn I had that fixed earlier, but I get this error no matter if the variable was properly defined or not.

Fatal error: Cannot access empty property in /public_html/me-class.register.php on line 77

post the line of code thats giving you the error

jstfsklh211
Junior Poster
100 posts since Apr 2011
Reputation Points: 34
Solved Threads: 27
Skill Endorsements: 1

post the line of code thats giving you the error

77. if(($this->$password !== $this->confirm_password))
78. $this->errors[] = 'Passwords Must Match';

79. if(($this->$username == $this->password))
80. $this->errors[] = 'Password Cannot Be Your Username';

cheelo007
Light Poster
31 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

you only half way applied the last fix i gave you

when using class variables the syntax is $this->varName without the second '$'
you only changed it on one side of your comparisons

jstfsklh211
Junior Poster
100 posts since Apr 2011
Reputation Points: 34
Solved Threads: 27
Skill Endorsements: 1

you only half way applied the last fix i gave you

when using class variables the syntax is $this->varName without the second '$'
you only changed it on one side of your comparisons

Arrgh.. lol thank you very much. That fixed it!

Thanks again!

cheelo007
Light Poster
31 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0792 seconds using 2.78MB