Jump to content

[SOLVED] How do i use a functions return value in another function ?


jamesxg1

Recommended Posts

I have this,

 

<?php session_start();

class Login {

          private $username;
          private $password;
          private $salt = "pepper89378";

function SecureUsername($username) {

$this->username = mysql_real_escape_string(trim(addslashes(strip_tags(base64_encode($username)))));


return($this->username);

}


function SecurePassword($password, $salt) {

$this->salt = sha1(md5($salt));

$this->password = mysql_real_escape_string(trim(addslashes(strip_tags(sha1(md5($password . $this->salt))))));

return($this->password);

}



function LoginSecure($this->username, $this->password) {

$this->usernamecheck = "SELECT `username` FROM `members` WHERE username = '$this->username'";

$this->runusernamecheck = mysql_query($this->usernamecheck) or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR);

if(mysql_num_rows($this->runusernamecheck) = 1) {


$this->passwordcheck = "SELECT `password` FROM `members` WHERE username = '$this->username' AND password = '$this->password'";

$this->runpasswordcheck = mysql_query($this->passwordcheck) or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR);

if(mysql_num_rows($this->runpasswordcheck) = 1) {

$this->details = "SELECT * FROM `members` WHERE username = '$this->username' AND password = '$this->password'";

$this->rundetails = mysql_query($this->details) or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR);

while($details = mysql_fecth_array($this->rundetails)) {


$_SESSION['username'] = base64_decode($details['username']);
$_SESSION['user_id']  = is_numeric($details['user_id']);

if($details['ad'] = 1) {

header('Location: suspended.php?username=' . base64_decode($details['username']) . '); 

} else {

if($details['lf'] = 1) {

header('Location: controlpanel.php?first=1'); 

} else {

header('Location: controlpanel.php'); 

}

}

}

    } else {

      $this->wrongusername = "Wrong username, Please try again.";

      return($this->wrongusername);

      }

  } else {

      $this->wrongpassword = "Wrong password, Please try again.";

      return($this->wrongpassword);

      }

}

?>

 

I get this error,

 

Fatal error: Cannot re-assign $this in C:\Program Files\xampp\xampp\htdocs\work\Login.inc.php on line 31

 

Line 31 =

function LoginSecure($this->username, $this->password) {

 

Many thanks,

 

James.

well, since you are assigning the data members already, you don't need to return anything with those functions.

 

there are also a few syntax errors


if(mysql_num_rows($this->runpasswordcheck) = 1) {

 

should be


if(mysql_num_rows($this->runpasswordcheck) == 1) {

 

you want to use the comparison operator (==) not the assignment operator (=). you have to change that in a few places.

:), Changed.

 

<?php session_start();

class Login {

          private $username;
          private $password;
          private $salt = "pepper89378";

function SecureUsername($username) {

$this->username = mysql_real_escape_string(trim(addslashes(strip_tags(base64_encode($username)))));


}


function SecurePassword($password, $salt) {

$this->salt = sha1(md5($salt));

$this->password = mysql_real_escape_string(trim(addslashes(strip_tags(sha1(md5($password . $this->salt))))));


}



function LoginSecure($this->username, $this->password) {

$this->usernamecheck = "SELECT `username` FROM `members` WHERE username = '$this->username'";

$this->runusernamecheck = mysql_query($this->usernamecheck) or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR);

if(mysql_num_rows($this->runusernamecheck) == 1) {


$this->passwordcheck = "SELECT `password` FROM `members` WHERE username = '$this->username' AND password = '$this->password'";

$this->runpasswordcheck = mysql_query($this->passwordcheck) or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR);

if(mysql_num_rows($this->runpasswordcheck) == 1) {

$this->details = "SELECT * FROM `members` WHERE username = '$this->username' AND password = '$this->password'";

$this->rundetails = mysql_query($this->details) or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR);

while($details = mysql_fecth_array($this->rundetails)) {

if($details['ad'] == 1) {

header('Location: suspended.php?username=' . base64_decode($details['username']) . '); 

} else {


$_SESSION['username'] = base64_decode($details['username']);
$_SESSION['user_id']  = is_numeric($details['user_id']);

$this->insert = "INSERT INTO `memberlogs` (`username`, `user_id`, `time`, `date`, `ip`, `browser`) VALUES('', '', '', '', '', '')";

$this->runinsert = mysql_query($this->insert) or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR);


if($details['lf'] == 1) {

header('Location: controlpanel.php?first=1'); 

} else {

header('Location: controlpanel.php'); 

}

}

}

    } else {

      $this->wrongusername = "Wrong username, Please try again.";

      return($this->wrongusername);

      }

  } else {

      $this->wrongpassword = "Wrong password, Please try again.";

      return($this->wrongpassword);

      }

}

?>

 

I still have the same error :S.

 

Many thanks,

 

James.

Oh, i'm foolish. Didn't even read the line drawing the error.

 

function LoginSecure($this->username, $this->password) {

 

you can't pass data members as variables into methods. Why would you? you already have access to them since they are encapulated in the class. Make the function this

function LoginSecure()

 

and it should work as expected (assuming there are no more errors in the function)

Sorry to re-post again,

 

But i get this error now,

 

<?php session_start();

class Login {

          private $username;
          private $password;
          private $salt = "pepper89378";

function SecureUsername($username) {

$this->username = mysql_real_escape_string(trim(addslashes(strip_tags(base64_encode($username)))));


}


function SecurePassword($password, $salt) {

$this->salt = sha1(md5($salt));

$this->password = mysql_real_escape_string(trim(addslashes(strip_tags(sha1(md5($password . $this->salt))))));


}



function LoginSecure() {

$this->usernamecheck = "SELECT `username` FROM `members` WHERE username = '$this->username'";

$this->runusernamecheck = mysql_query($this->usernamecheck) or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR);

if(mysql_num_rows($this->runusernamecheck) == 1) {


$this->passwordcheck = "SELECT `password` FROM `members` WHERE username = '$this->username' AND password = '$this->password'";

$this->runpasswordcheck = mysql_query($this->passwordcheck) or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR);

if(mysql_num_rows($this->runpasswordcheck) == 1) {

$this->details = "SELECT * FROM `members` WHERE username = '$this->username' AND password = '$this->password'";

$this->rundetails = mysql_query($this->details) or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR);

while($details = mysql_fecth_array($this->rundetails)) {

$this->username = base64_decode($details['username']);
$this->user_id  = is_numeric($details['user_id']);

if($details['ad'] == 1) {

header('Location: suspended.php?username=' . $this->username . '); 

} else {

$_SESSION['user_id']  = $this->user_id;
$_SESSION['username'] = $this->username;


$this->insert = "INSERT INTO `memberlogs` (`username`, `user_id`, `time`, `timea`, `date`, `ip`, `browser`) VALUES('$details['username']', 

'$details['user_id']', 'date('h:i')', 'date('A')', 'date("m-d-Y")', '', '')";

$this->runinsert = mysql_query($this->insert) or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR);


if($details['lf'] == 1) {

header('Location: controlpanel.php?first=1'); 

} else {

header('Location: controlpanel.php'); 

}

}

}

    } else {

      $this->wrongusername = "Wrong username, Please try again.";

      return($this->wrongusername);

      }

  } else {

      $this->wrongpassword = "Wrong password, Please try again.";

      return($this->wrongpassword);

      }

}

?>

 

 

 

Parse error: syntax error, unexpected T_STRING in C:\Program Files\xampp\xampp\htdocs\socialbuild\Login.inc.php on line 58

 

Line 58:

$_SESSION['user_id']  = $this->user_id;

 

Many thanks,

 

James.

Just a few lines above this

 

<?php
header('Location: suspended.php?username=' . $this->username . ');

 

should be

<?php
header('Location: suspended.php?username=' . $this->username);

 

If you look closely you will notice that the syntax highlighting in these forums also changes slightly after that line because of the single quote. You should get yourself some decent editor with syntax highlighting if you don't already have one. It makes a lot easier to spot errors/typos like this.

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.