Share a PHP User login class

Source: Internet
Author: User
Tags set cookie setcookie
In PHP programming, user login is often used, here to share a good PHP user login class, the need for a friend reference.

PHP User Login class, code:

     Errreport ($this->err_mysql);        $this->err= $this->err_mysql;            }} function isLoggedIn ()//determine if login {if (isset ($_cookie[' sid '))//If the COOKIE has SID {            session_id ($_cookie[' Sid ');            Session_Start ();            $this->username=$_session[' username '];            $this->userid=$_session[' userid '];            $this->userlevel=$_session[' userlevel '];        return true;            } else//If the SID is not saved in the cookie, check the session {session_start () directly;        if (isset ($_session[' username ')) return true;    } return false;        } function Userauth ($username, $userpass)//user authentication {$this->username= $username;        $this->userpass= $userpass; $query = "SELECT * from". $this->authtable. "        ' WHERE ' username ' = ' $username '; ";        $result =mysql_query ($query); if (mysql_num_rows ($result)!=0)//Find this user {$row =mysql_fetch_array ($result);                if ($row [' bannd ']==1)//This user is banned {$this->errreport ($this->err_user);                $this->err= $this->err_user;            return false;                } elseif (MD5 ($userpass) = = $row [' userpass '])//password match {$this->userid= $row [' id '];                $this->userlevel= $row [' userlevel '];            return true;                } else//password mismatch {$this->errreport ($this->err_password);                $this->err= $this->err_password;            return false;            }} else//did not find this user {$this->errreport ($this->err_username);            $this->err= $this->err_username;        return false;    }} function Setsession ()//Set Session {$sid =uniqid (' Sid ');        Generate SID session_id ($SID);        Session_Start ();    $_session[' username ']= $this->username; Assigning a value to a session variable       $_session[' userid ']= $this->userid;        //..    $_session[' userlevel ']= $this->userlevel;        //.. if ($this->use_cookie)//Use cookies to save Sid {if (!setcookie (' Sid ', $sid, Time () + $this->cookietime, $th        Is->cookiepath)) $this->errreport ("Set Cookie Failed");    } else Setcookie (' Sid ', ', Time ()-3600);        Clears the SID in the cookie} function Userlogout ()//user logoff {session_start ();    unset ($_session[' username ');        Clears the username if in session (Setcookie (' Sid ', ", Time ()-3600))//Clears the SID in the cookie to return true;    else return false;    } function Errreport ($STR)//error {if ($this->error_report) echo "error: $str"; }}?>
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.