Page 1 of 1

Help with a class and login function

Posted: Sun Feb 28, 2010 11:47 pm
by chizeng
Dear kind fellow board members,

In a php script, I made this class and functions login() & setSession():

Code: Select all

class Login {
    function login($user,$pass) {
        $user = addslashes($user);
        $pass = addslashes($pass));
        $findUser = mysql_query("SELECT * FROM users WHERE userName='$user'") or die(mysql_error());
        $a = mysql_fetch_array($findUser);
        $realpass = $a['password'];
        if(mysql_num_rows($findUser) == "0") {
            nS("There is no such account.");
        }
        elseif (($pass == $realpass) && ($a['enabled'] != 0)) {
            $this->setSession($user);
        }
    }
    function setSession($userName) {
        $_SESSION['userName'] = "$userName";
    }
}
I then tried to call it after the post submission of a form like this:

Code: Select all

if (isset($_POST['user']) && ($_POST['action']=='login')) {
$log = new Login();
$u = $_POST['user'];
$p = $_POST['password'];
$log->login($u,$p);
However, I get this message upon an attempt to log in:

Warning: Missing argument 1 for Login::login(), called in /home/youchun/maps.chizeng.com/index.php on line 7 and defined in /home/youchun/maps.chizeng.com/core.php on line 97

Warning: Missing argument 2 for Login::login(), called in /home/youchun/maps.chizeng.com/index.php on line 7 and defined in /home/youchun/maps.chizeng.com/core.php on line 97
There is no such account.


Any help would be very useful. Thanks so much,


Chi

Re: Help with a class and login function

Posted: Sun Feb 28, 2010 11:50 pm
by jraede
When you have a function in your class with the same name as the class (I guess it's not case sensitive, that's good to know), that function will automatically run when you create a new object of that class. So, your login() function is running when you say new Login(), and because you aren't passing any variables for $username or $password, you are getting those errors. It should be :

Code: Select all

 
$u = $_POST['user'];
$p = $_POST['password'];
$log = new Login($u,$p);
 

Re: Help with a class and login function

Posted: Mon Mar 01, 2010 11:24 pm
by chizeng
Thanks so much for the clarification. This board is such a great place to learn new code.