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;
  }
}

?>

Recommended Answers

All 6 Replies

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';

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

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

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';

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

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.