Hi all, after thinking for sometimes, I thought it will be grat opportunity to learn if I will start from scratch and build my own register/login system. Here is the thread that I will be posting the progress and I hope you guys will help me.
The code below is what I have so far. Just put two scripts in the same directory and that is! I hope you will help me :)
Thanks!
class.php
<?php
//php login sytem
class LoginRegister{
function __construct(){
}
function displogin($status){
if ($status == "login"){
// post login page
$enc = base64_encode('login');
$html = <<<LOGIN
<form action = $_SERVER[PHP_SELF]?do=$enc, method = POST>
<p>Username: <input type=text name = username /></p>
<p>Password: <input type=password name = password /></p>
<input type=submit value=Login />
</form>
LOGIN;
echo $html;
}//end if
else if ($status == "register"){
//post register page
$enc = base64_encode('register');
$html = <<<LOGIN
<form action = $_SERVER[PHP_SELF]?do=$enc, method = POST>
<p>Username: <input type=text name = username /></p>
<p>Password: <input type=password name = password /></p>
<input type=submit value=Register />
</form>
LOGIN;
echo $html;
}// end elese if
}
function auth($username, $password){
$sql = "SELECT * FROM users WHERE username = '$username' AND password = '$password' ";
$res = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($res)==1){
echo "sucessful logged in as ". $username;
}//end if
else{
echo "<p style = 'color:red; font-weight:bold;'>Username or password not correct.
<br /> New? Register!</p>";
$this->displogin('register');
}// end else
}
function checkempty($username, $password, $mode){
if (empty($username) or empty($password)){
echo "<p style = 'color:red; font-weight:bold;'>Empty Values are not allowed</p>";
$this->displogin('login');
}//end if
else{
//do checking
switch($mode){
case 'login':
$this->auth($username, $password);
case 'register':
$this->adduser($username, $password);
default:
echo "<p style = 'color:red; font-weight:bold;'>Wrong Values are not allowed</p>";
$this->displogin('login');
}//end switch
}//end else
}
function login($uname, $passwd){
//username
$username = stripslashes($uname);
$username = mysql_real_escape_string($uname);
//passsword
$password = stripslashes($passwd);
$password = mysql_real_escape_string($passwd);
//check for empty variables
$this->checkempty($username, $password, 'login');
}
function register($uname, $passwd){
//username
$username = stripslashes($uname);
$username = mysql_real_escape_string($uname);
//passsword
$password = stripslashes($passwd);
$password = mysql_real_escape_string($passwd);
//check for empty variables
$this->checkempty($username, $password, 'register');
}
function adduser($username, $password){
$sql = "INSERT INTO users(username, password) VALUES('$username', '$password')";
//redirect to login page
echo "<p style = 'color:green; font-weight:bold;'>Thanks for registering. You can now login</p>";
$this->displogin('login');
mysql_query($sql) or die(mysql_error());
}
}//end class
?>
index.php
<?php
require "class.php";
$obj = new LoginRegister();
$conn = mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("admin", $conn)or die(mysql_error());
if ((isset($_GET['do']))){
if (($_GET['do'])==(base64_encode('login'))){
$obj->login($_POST['username'], $_POST['password']);
}//end middle first if
else if(($_GET['do'])== (base64_encode('register'))){
$obj->register($_POST['username'], $_POST['password']);
}
else{
echo "<p style = 'color:red; font-weight:bold;'>Please Login</p>";
$obj->displogin('login');
//debug
echo base64_encode('login').'<br />';
echo $_GET['do'];
}//end else middle
}//end last if
else{
echo "<p style = 'color:green; font-weight:bold;'>Please Login</p>";
$obj->displogin('login');
}//end else
?>