Click to See Complete Forum and Search --> : registration script (trying)


vividona
11-18-2007, 03:12 AM
hi friends
Anybody here criticize this funny code? It is working but I need someone criticize it.

BHL.php


<?php

class BHL_DB {

protected $DB_SERVER;
protected $DB_USER;
protected $DB_PASS ;
protected $DB_NAME;
protected $connection;

function __construct($DB_SERVER = "localhost", $DB_USER = "root", $DB_PASS = "", $DB_NAME = "bahala"){
$this->DB_SERVER = $DB_SERVER;
$this->DB_USER = $DB_USER;
$this->DB_PASSS = $DB_PASS;
$this->DB_NAME = $DB_NAME;
$this->connection = $connection;
}

public function DBConnection(){

$this->connection = mysql_connect($this->DB_SERVER, $this->DB_USER, $this->DB_PASS) or die('Can not connect to BHL, try again' . mysql_error());
mysql_select_db($this->DB_NAME) or die(mysql_error());

}
}
$transaction = new BHL_DB();
$transaction->DBConnection();


?>



reginfo.php



<?php

class RegistrationInfo {

public $username;
public $password;
public $email;
public $DB_TBL;
public $ip;

function __construct($username, $password, $email, $DB_TBL, $ip){
$this->username = $username;
$this->password = $password;
$this->email = $email;
$this->DB_TBL = $DB_TBL;
$this->ip = $ip;

}

function DBuserTBL($DB_TBL){
$this->DB_TBL = "users";
}

function UserInfo($username, $password, $email, $DB_TBL, $ip){
if (isset($_POST['submit'])){
if($_POST['username'] != "" && strlen($_POST['username']) >= 4 && $_POST['password'] != "" && strlen($_POST['password']) >= 4 && $_POST['password'] == $_POST['password2'] && $_POST['email'] != "" && eregi("^([[:alnum:]]|_|\.|-)+@([[:alnum:]]|\.|-)+(\.)([a-z]{2,4})$", $_POST['email'])){
if(getenv('HTTP_X_FORWARDED_FOR')){
$this->ip = getenv('HTTP_X_FORWARDED_FOR');
if($this->ip == ""){
$this->ip = getenv('REMOTE_ADDR');
}
}else{
$this->ip = getenv('REMOTE_ADDR');
}
$date = DATE('Y-m-d');

$this->username = $_POST['username'];
$check = mysql_query("SELECT username FROM `$this->DB_TBL` WHERE username = '$this->username'") or die(mysql_error());
$check2 = mysql_num_rows($check);
if ($check2 != 0){
die('<font color = blue>Sorry, the <strong><font color = red>'.$this->username.'</font></strong> is already in use.!</font>');
}

$this->password = $_POST['password'];
$this->email = $_POST['email'];
}else{
die("<font color = blue>Please, <strong><font color = red>CHECK IT AGAIN</font></strong>there is something wrong!</font>");
}
$q = mysql_query ("INSERT INTO `$this->DB_TBL` (`username`, `password`, `email`, `ip`, `regdate`) VALUES ('".mysql_real_escape_string($_POST['username'])."', '".mysql_real_escape_string(md5($_POST['password']))."', '".mysql_real_escape_string($_POST['email'])."','".$this->ip."','".$date."')");
$result = mysql_query($q);

}
}

function getUserInfo(){
echo "<font color = blue>hi <font color = red>" . $this->username."</font></br> Thank you for your membership with us. Plz check it out and feel free to access our website</font>";

}
}
$person = new RegistrationInfo($username, $password, $email, $DB_TBL, $ip);
$person->DBuserTBL($DB_TBL);
$person->UserInfo($username, $password, $email, $DB_TBL, $ip);
echo $person->getUserInfo();


?>



registration.php


<?php
include('BHL.php');
include('reginfo.php') ;
include ('regform.html');

?>



regform.html


<body>
<h2>REGISTER</h2>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<table width="60%" border="1" cellspacing="2" cellpadding="2">
<tr>
<td>USER NAME: </td>
<td><input type="text" name="username" size="15" maxlength="30"
value="<?php if (isset($_POST['username'])) echo $_POST['username'];
?>" /></td>
</tr>
<tr>
<td>PASSWORD: </td>
<td><input type="password" name="password" size="10" maxlength="20"
/></td>
</tr>
<tr>
<td>VERIFY PASSWORD: </td>
<td><input type="password" name="password2" size="10" maxlength="20"
/></td>
</tr>
<tr>
<td>EMAIL ADDRESS: </td>
<td><input type="text" name="email" size="20" maxlength="40"
value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="submit" value="REGISTER" /></td>
</tr>
</table>
<p>
<input type="hidden" name="submitted" value="TRUE" />
</p>
</form>
</body>

vividona
11-18-2007, 03:19 AM
plz I need good and easy SESSION and COOKIES codes for login and logout

how can I do that

thx friends

alanzhao
11-18-2007, 11:04 AM
this is the simple script I use for login

<?php

include("./functions.php");

session_start();
session_unset();

$login_failed_msg = "";

if (!empty($_POST['login'])) {

if (!empty($_POST['username']) && !empty($_POST['password'])) {

$_SESSION["admin_username"] = md5($_POST['username']);
$_SESSION["admin_password"] = md5($_POST['password']);

if (is_admin()) {
header("Location: ./index.php");
exit;

} else {
$login_failed_msg = '<font color="#ff0000"><b>Login Failed!</b></font>';
}
}
}

?>
<html>

<head>
<title>Admin Login</title>
<style>
body, table, tr, td, input { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 8pt; }
</style>
</head>

<body>
<div align="center">
<table height="500">
<tr>
<td align="center">
<p><font size="4"><b>Admin Login</b></font></p>
<p><?php echo $login_failed_msg; ?></p>
<form method="post" action="./login.php">
<div align="center">
<table border="0" cellpadding="2" cellspacing="0" width="300" id="table1">
<tr>
<td width="93">Username:</td>
<td>
<input type="text" name="username" size="20" maxlength="20"></td>
</tr>
<tr>
<td width="93">Password:</td>
<td>
<input type="password" name="password" autocomplete="off" size="20" maxlength="20"></td>
</tr>
<tr>
<td width="93"></td>
<td><input type="submit" value="Login" name="login"></td>
</tr>
</table>
</div>
</form>
<p><font size="4" color="#999999"><b>Your IP <?=$_SERVER['REMOTE_ADDR']?> is logged!</b></font></p>
</td>
</tr>
</table>
</div>

</body>

</html>

function is_admin() {

$username = $_SESSION["admin_username"];
$password = $_SESSION["admin_password"];

$result = mysql_query("SELECT id FROM admin WHERE username = '$username' && password = '$password'");
if (mysql_num_rows($result) == 1) {

$r = mysql_fetch_array($result);

return true;
} else {
return false;
}
}

to protect pages, include this at the begining

if (!is_admin()) {
get_out();
}

username and password are all md5() encrypted.

hope it helps.

vividona
11-18-2007, 12:08 PM
thank you alanzhao

I should deal with it and convert it to oop

but what about my script is it good for beginner or bad?
what I should make it to be acceptable (in oop).

alanzhao
11-18-2007, 02:52 PM
I am not a big OOP fan so I can't comment on your script. If it's working for you it's good.

One thing I would like to comment is to separate the HTML from PHP code and find a template class works for you. Since you practice OOP and not using a template class seems to be inconsistent with your practice.