Hi all, after thinking for sometimes, I thought it will be great 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
[CODE=php]
<?php
//php login sytem
class LoginRegister{
function __construct(){
}
function displogin($stat us){
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($sq l) 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;'>U sername or password not correct.
<br /> New? Register!</p>";
$this->displogin('reg ister');
}// end else
}
function checkempty($use rname, $password, $mode){
if (empty($usernam e) or empty($password )){
echo "<p style = 'color:red; font-weight:bold;'>E mpty Values are not allowed</p>";
$this->displogin('log in');
}//end if
else{
//do checking
switch($mode){
case 'login':
$this->auth($username , $password);
case 'register':
$this->adduser($usern ame, $password);
default:
echo "<p style = 'color:red; font-weight:bold;'>W rong Values are not allowed</p>";
$this->displogin('log in');
}//end switch
}//end else
}
function login($uname, $passwd){
//username
$username = stripslashes($u name);
$username = mysql_real_esca pe_string($unam e);
//passsword
$password = stripslashes($p asswd);
$password = mysql_real_esca pe_string($pass wd);
//check for empty variables
$this->checkempty($us ername, $password, 'login');
}
function register($uname , $passwd){
//username
$username = stripslashes($u name);
$username = mysql_real_esca pe_string($unam e);
//passsword
$password = stripslashes($p asswd);
$password = mysql_real_esca pe_string($pass wd);
//check for empty variables
$this->checkempty($us ername, $password, 'register');
}
function adduser($userna me, $password){
$sql = "INSERT INTO users(username, password) VALUES('$userna me', '$password')";
//redirect to login page
echo "<p style = 'color:green; font-weight:bold;'>T hanks for registering. You can now login</p>";
$this->displogin('log in');
mysql_query($sq l) or die(mysql_error ());
}
}//end class
?>
[/CODE]
index.php
[CODE=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_enco de('login'))){
$obj->login($_POST['username'], $_POST['password']);
}//end middle first if
else if(($_GET['do'])== (base64_encode( 'register'))){
$obj->register($_POS T['username'], $_POST['password']);
}
else{
echo "<p style = 'color:red; font-weight:bold;'>P lease Login</p>";
$obj->displogin('log in');
//debug
echo base64_encode(' login').'<br />';
echo $_GET['do'];
}//end else middle
}//end last if
else{
echo "<p style = 'color:green; font-weight:bold;'>P lease Login</p>";
$obj->displogin('log in');
}//end else
?>
[/CODE]
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
[CODE=php]
<?php
//php login sytem
class LoginRegister{
function __construct(){
}
function displogin($stat us){
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($sq l) 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;'>U sername or password not correct.
<br /> New? Register!</p>";
$this->displogin('reg ister');
}// end else
}
function checkempty($use rname, $password, $mode){
if (empty($usernam e) or empty($password )){
echo "<p style = 'color:red; font-weight:bold;'>E mpty Values are not allowed</p>";
$this->displogin('log in');
}//end if
else{
//do checking
switch($mode){
case 'login':
$this->auth($username , $password);
case 'register':
$this->adduser($usern ame, $password);
default:
echo "<p style = 'color:red; font-weight:bold;'>W rong Values are not allowed</p>";
$this->displogin('log in');
}//end switch
}//end else
}
function login($uname, $passwd){
//username
$username = stripslashes($u name);
$username = mysql_real_esca pe_string($unam e);
//passsword
$password = stripslashes($p asswd);
$password = mysql_real_esca pe_string($pass wd);
//check for empty variables
$this->checkempty($us ername, $password, 'login');
}
function register($uname , $passwd){
//username
$username = stripslashes($u name);
$username = mysql_real_esca pe_string($unam e);
//passsword
$password = stripslashes($p asswd);
$password = mysql_real_esca pe_string($pass wd);
//check for empty variables
$this->checkempty($us ername, $password, 'register');
}
function adduser($userna me, $password){
$sql = "INSERT INTO users(username, password) VALUES('$userna me', '$password')";
//redirect to login page
echo "<p style = 'color:green; font-weight:bold;'>T hanks for registering. You can now login</p>";
$this->displogin('log in');
mysql_query($sq l) or die(mysql_error ());
}
}//end class
?>
[/CODE]
index.php
[CODE=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_enco de('login'))){
$obj->login($_POST['username'], $_POST['password']);
}//end middle first if
else if(($_GET['do'])== (base64_encode( 'register'))){
$obj->register($_POS T['username'], $_POST['password']);
}
else{
echo "<p style = 'color:red; font-weight:bold;'>P lease Login</p>";
$obj->displogin('log in');
//debug
echo base64_encode(' login').'<br />';
echo $_GET['do'];
}//end else middle
}//end last if
else{
echo "<p style = 'color:green; font-weight:bold;'>P lease Login</p>";
$obj->displogin('log in');
}//end else
?>
[/CODE]
Comment