srule_
12-31-2007, 05:21 PM
Hey, This is the first class I have designed that is actually useful. I am preaty new to the concepts of OOP so I would appreciate some feedback on my class.
registerClass.php
<?php
class Register {
//Data Memeber
private $username;
private $email;
private $password;
private $errors;
//Contructor Function
public function __construct(){
$this->username = '';
$this->email = '';
$this->password = '';
$this->errors = '0';
}
//--------------------------------------------------------------
//Check Username
public function checkUsername($username) {
if (empty($username))
{
echo $this->errors = 'You forgot to enter your username.';
}
elseif (strlen($username) <2 OR strlen($username) >13 )
{
echo $this->errors = 'Your username must be between 3 and 12 characters';
}
else
{
$this->username = $username;
}
}
//--------------------------------------------------------------
//Check email
public function checkEmail($email){
if (empty($email))
{
echo $this->errors = 'You forgot to enter your email.';
}
else
{
$query = "SELECT user_id FROM members WHERE email='$email'";
$result = mysql_query($query);
if (mysql_num_rows($result) == 0) {
$this->email = $email;
}
else {
echo $this->errors = 'That e-mail is already registerd';
}
}
}
//--------------------------------------------------------------
//Check Password
public function checkPassword($password1, $password2){
if (!empty($password1))
{
if ($password1 != $password2)
{
echo $this->errors = 'Your password did not match the confirmed password.';
}
elseif (strlen($password1) <2 OR strlen($password1) >13 )
{
echo $this->errors = 'Your password must be between 3 and 12 characters';
}
else
{
$this->password = $_POST['password1'];
}
}
else
{
echo $this->errors = 'You forgot to enter your password.';
}
}
//--------------------------------------------------------------
//Enter Info To Database if all is ok
public function enterData($theQuery){
if ($this->errors == '0')
{
// Make the querys to insert new memeber into the database.
$query = $theQuery;
$result = @mysql_query ($query); // Run the query.
if ($result) { // If it ran OK.
// Send an email, if desired.
echo "THANK YOU, you can now log in";
} else { // If it did not run OK.
echo $this->errors = 'You could not be registered due to a system error. We apologize for any inconvenience.</p>';
}
}
}
}
?>
register.php:
<?php
if (isset($_POST['submitted'])) {
include('connectClass.php');
include('registerClass.php');
//Make Database Connection
$makeConnection = new MySqlConnect('root', '', 'localhost', 'textgame');
//Create an Object from Register User Class
$newUser = new Register();
//Check Data Entered in the form
$newUser->checkUsername($_POST['username']);
$newUser->checkEmail($_POST['email']);
$newUser->checkPassword($_POST['password1'], $_POST['password2']);
//Enter New User Into The Database
$newUser->enterData("INSERT INTO members(username, email, password, registration_date) VALUES ('".$_POST['username']."', '".$_POST['email']."', SHA('".$_POST['password']."'), NOW() )");
}
?>
<form action="registration.php" method="post">
<p>Username: <input type="text" name="username" value="<?php if (isset($_POST['username'])) echo $_POST['first_name']; ?>" /></p>
<p>Email Address: <input type="text" name="email" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" /> </p>
<p>Password: <input type="password" name="password1"/></p>
<p>Confirm Password: <input type="password" name="password2"/></p>
<p><input type="submit" name="submit" value="Register" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
registerClass.php
<?php
class Register {
//Data Memeber
private $username;
private $email;
private $password;
private $errors;
//Contructor Function
public function __construct(){
$this->username = '';
$this->email = '';
$this->password = '';
$this->errors = '0';
}
//--------------------------------------------------------------
//Check Username
public function checkUsername($username) {
if (empty($username))
{
echo $this->errors = 'You forgot to enter your username.';
}
elseif (strlen($username) <2 OR strlen($username) >13 )
{
echo $this->errors = 'Your username must be between 3 and 12 characters';
}
else
{
$this->username = $username;
}
}
//--------------------------------------------------------------
//Check email
public function checkEmail($email){
if (empty($email))
{
echo $this->errors = 'You forgot to enter your email.';
}
else
{
$query = "SELECT user_id FROM members WHERE email='$email'";
$result = mysql_query($query);
if (mysql_num_rows($result) == 0) {
$this->email = $email;
}
else {
echo $this->errors = 'That e-mail is already registerd';
}
}
}
//--------------------------------------------------------------
//Check Password
public function checkPassword($password1, $password2){
if (!empty($password1))
{
if ($password1 != $password2)
{
echo $this->errors = 'Your password did not match the confirmed password.';
}
elseif (strlen($password1) <2 OR strlen($password1) >13 )
{
echo $this->errors = 'Your password must be between 3 and 12 characters';
}
else
{
$this->password = $_POST['password1'];
}
}
else
{
echo $this->errors = 'You forgot to enter your password.';
}
}
//--------------------------------------------------------------
//Enter Info To Database if all is ok
public function enterData($theQuery){
if ($this->errors == '0')
{
// Make the querys to insert new memeber into the database.
$query = $theQuery;
$result = @mysql_query ($query); // Run the query.
if ($result) { // If it ran OK.
// Send an email, if desired.
echo "THANK YOU, you can now log in";
} else { // If it did not run OK.
echo $this->errors = 'You could not be registered due to a system error. We apologize for any inconvenience.</p>';
}
}
}
}
?>
register.php:
<?php
if (isset($_POST['submitted'])) {
include('connectClass.php');
include('registerClass.php');
//Make Database Connection
$makeConnection = new MySqlConnect('root', '', 'localhost', 'textgame');
//Create an Object from Register User Class
$newUser = new Register();
//Check Data Entered in the form
$newUser->checkUsername($_POST['username']);
$newUser->checkEmail($_POST['email']);
$newUser->checkPassword($_POST['password1'], $_POST['password2']);
//Enter New User Into The Database
$newUser->enterData("INSERT INTO members(username, email, password, registration_date) VALUES ('".$_POST['username']."', '".$_POST['email']."', SHA('".$_POST['password']."'), NOW() )");
}
?>
<form action="registration.php" method="post">
<p>Username: <input type="text" name="username" value="<?php if (isset($_POST['username'])) echo $_POST['first_name']; ?>" /></p>
<p>Email Address: <input type="text" name="email" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" /> </p>
<p>Password: <input type="password" name="password1"/></p>
<p>Confirm Password: <input type="password" name="password2"/></p>
<p><input type="submit" name="submit" value="Register" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>