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
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:
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:
6 Responses
to “A Register/Login Class”
You must be logged in to post a comment.

I get this error – “No input file specified.”
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?
Hi,
Can you provide a sql file that I can use to set up the database please ??
Thanks
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.
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!
Hello,
I will try and put up a more detailed example of how to do this in the near future.