A Register/Login Class

Hello,
Well it has been a while since any posts were made here so I guess its time for me to post something useful (if you're a coder).
I've been working on a new project and in the process I have been constructing a login/register class that could easily fit into any current script I think.
Here it is :)

<?php
class MemberFunctions
        {
        var $pass;
        var $username;
        var $email;
        var $userid;

        function CheckUsername($username)
                {
                $username = trim($username);
                if      (!empty($username))
                        {
               $check = mysql_query('SELECT `id`,`username` FROM `members` WHERE `username`="'.$username.'"');
               if       (mysql_num_rows($check) == 0)
                {
                list($_id,$un) = mysql_fetch_array($check);
                $this->username = $username;
                $this->userid = $_id;
                return true;
                }
               else
                                return false;
                        }
                else
                        return false;
                }

        function CheckPasswords($pw1,$pw2)
                {
                $pw1 = trim($pw1);
                $pw2 = trim($pw2);
                if      (!empty($pw1) && !empty($pw1))
                        {
                        $pw1 = md5(trim($pw1));
                        $pw2 = md5(trim($pw2));
                                if      ($pw1 == $pw2)
                                        {
                                        $this->pass = $pw1;
                                        return true;
                                        }
                                else
                                        return false;
                        }
                else
                        return false;
                }

        function CheckEmailAddress($email)
                {
                        $email = trim($email);
                        if      (!empty($email))
                                {
                                $this->email = $email;
                                return true;
                                }
                        else
                                return false;
                }

        function Register()
                {
                $insert = mysql_query("INSERT INTO `members` (username,password,email) VALUES ('".$this->username."','".$this->pass."','".$this->email."')");
                if      ($insert)
                        {
                        $_SESSION['loggedin'] = 1;
                        $_SESSION['username'] = $this->username;
                        $_SESSION['userid'] = $this->userid;
                        return true;
                        }
                else
                        return false;
                }

        function LoginCheckUsername($username)
                {
                if      (!empty($username))
                        {
               $check = mysql_query('SELECT `id`,`username` FROM `members` WHERE `username`="'.$username.'"');
               if       (mysql_num_rows($check) == 1)
                {
                list($_id,$un) = mysql_fetch_array($check);
                $this->username = $username;
                $this->userid = $_id;
                return true;
                }
               else
                                return false;
                        }
                else
                        return false;
                }

        function LoginCheckPassword($pw)
                {
                if      (!empty($pw))
                        {
                        $pw = md5($pw);
                  $check = mysql_query("SELECT `password` FROM `members` WHERE `password`='".$pw."' AND `username`='".$this->username."'");
                  if    (mysql_num_rows($check) == 1)
                        {
                        $this->pass = $pw;
                        return true;
                        }
                  else
                        return false;
                        }
                else
                        return false;
                }

        function Login()
                {
                if      ($this->pass != "" && $this->username != "")
                        {
                        $_SESSION['loggedin'] = 1;
                        $_SESSION['username'] = $this->username;
                        $_SESSION['userid'] = $this->userid;
                        return true;
                        }
                else
                        return false;
                }

        function Logout()
                {
                if      ($_SESSION['loggedin'] == 1)
                        {
                        $_SESSION['loggedin'] = 0;
                        return true;
                        }
                else
                        return false;
                }

        }
$_mem = new MemberFunctions();
?>
 

Feel free to offer any suggestions as to how the code can be improved as it is still in its early stages.

Update 02 Jan 2008:
I have updated the code to change a few things.

Here is also a way to use the class in your script:

<?php
if      (!$_mem->LoginCheckUsername($_POST['username']))
                {
                $_SESSION['lerror'][] = "Incorrect username entered.";
                }
        if      (!$_mem->LoginCheckPassword($_POST['password']))
                {
                $_SESSION['lerror'][] = "You have entered an incorrect password. Please try again.";
                }
        if      ($_SESSION['lerror'])
                {
                header('Location: login.php');
                exit;
                }
        else
                {
            if  (!$_mem->Login())
                {
                $_SESSION['lerror'][] = "There was a problem logging you in, please try again later.";
                header('Location: login.php');
                                exit;
                }
                else
                        {
                        $_loggedin = 1;
                        }
                }
?>
 

If there are any errors you can print them out using this code:

<?php
if(!empty($_SESSION['lerror'])){
   if(is_array($_SESSION['lerror'])){
        foreach($_SESSION['lerror'] as $value){
              echo $value."
"
;
         }
   }
   $_SESSION['lerror'] = NULL;
}
?>
 

Hope that helps :D

[Slashdot][Digg][Reddit][del.icio.us][Facebook][Technorati][Google][StumbleUpon]


  • John Says:
    1-3-2008 17:48:05

    I get this error - “No input file specified.”

  • papa_face Says:
    1-3-2008 18:10:38

    What is the code you used?
    Please show me the line that you received the error on :)
    Have you made sure that you have included the class in the page?

  • Mo Says:
    1-5-2008 19:51:16

    Hi,

    Can you provide a sql file that I can use to set up the database please ??

    Thanks

  • papa_face Says:
    1-5-2008 20:01:32

    Yes no problem :)
    I used this SQL with this class. The level field denotes a regular user or a admin user. An admin user has a level 2, and a normal user has a level 1.

    CREATE TABLE `members` (
      `id` int(11) NOT NULL auto_increment,
      `username` varchar(255) NOT NULL,
      `password` varchar(32) NOT NULL,
      `email` varchar(255) NOT NULL,
      `level` int(1) NOT NULL default '1' COMMENT '1=normal 2=admin',
      PRIMARY KEY  (`id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
    
  • Micah Says:
    1-20-2008 08:28:45

    I am trying to startup a web site where i would like to have a vip section. I assume this code is probably what im looking for. My problem is, i am VERY NEW to web design & all that stuff & i am unfamilliar with how to implement this into my existing webpages….i know it’s .php & it needs to be linked to the mysql database but because im so new @ this, i really dont know how to do any of that. Could you put up examples of how to imbed this into an existing basic webpage? I really dont know enough about it to do it & im running on a budget & a deadline! Thank you in advance for your help!

  • papa_face Says:
    1-20-2008 12:43:56

    Hello,
    I will try and put up a more detailed example of how to do this in the near future.

  • You must be logged in to post a comment.