Help with a class and login function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chizeng
Forum Newbie
Posts: 11
Joined: Sun Feb 28, 2010 10:52 pm

Help with a class and login function

Post 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
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: Help with a class and login function

Post 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);
 
chizeng
Forum Newbie
Posts: 11
Joined: Sun Feb 28, 2010 10:52 pm

Re: Help with a class and login function

Post by chizeng »

Thanks so much for the clarification. This board is such a great place to learn new code.
Post Reply