A preserved archive of the Logical Gamers community forums, 2009-2025. The original threads and posts, served read-only. Registration, posting and private messages are gone for good.

[PHP] simple registration script

1.1k views · started by HTML ·
#1
[PHP] simple registration script
<?php

/*
* Register class written and updated by HTML AKA: TONY.
* Soon to replace simple if's with ternary..
* added a verify email part, username length and table variable.
*/

class register{

//Setting up Variables, you can add more, for example terms of service, age, verify_email, etc.

private $_username;
private $_password;
private $_email;
private $_verify_email;

//mysql fields
private $_table;
private $_namefield;
private $_passfield;
private $_mailfield;


//Giving the variables values
function __construct($u_name, $p_word, $e_mail, $v_mail)
{
$this->_username = $u_name;
$this->_password = $p_word;
$this->_email = $e_mail;
$this->_verify_email = $v_mail;
}

//checks to make sure fields are set
public function Run()
{
if(empty($this->_username) || empty($this->_password) || empty($this->_email) || empty($this->_verify_email))
{

echo "All fields are required to continue!";
}

else

{
$this->Username_length();
$this->checkEmail();
$this->verifyEmail();
$this->_mysql_check();
}

}

//Checks username length
public function Username_length()
{
echo ( strlen($this->_username) < 4 ? "Your username must be at least 4 letters long!" : null );
exit();
}



//check for @ sign in an email.
public function checkEmail()
{

if( strpos($this->_email, '@') === false )
{
exit("Your email address is missing '@'");
}

}


//Lets make sure the emails are matched.
public function verifyEmail()
{
echo ($this->_email != $this->_verify_email ? "Your email's don't match." : null );
exit();
}



// Set up the mysql_fields you needs.
public function mysql_fields($n_field, $p_field, $e_field, $table)
{
$this->_namefield = $n_field;
$this->_passfield = $p_field;
$this->_mailfield = $e_field;
$this->_table = $table;
}

//checks to see if the username and email exist in the database
private function _mysql_check()
{
$query = mysql_query("SELECT * FROM $this->_table WHERE '$this->_namefield' = '$this->_username' OR $this->_mailfield = '$this->_email' ");
if (mysql_num_rows($query) != 0)
{
echo "Username or email address already exists!";

}

else
{
$this->_mysql_insert();
}

}

//inserts values
private function _mysql_insert()
{
$sql = "INSERT INTO $this->_table
(
$this->_namefield,
$this->_passfield,
$this->_mailfield
)

VALUES
('$this->_username',
'$this->_password',
'$this->_email'
)";

$register = mysql_query($sql);


}



}

?>
#2
Update 001:
Added a mysql class so that you will be able to change the database values thanks to Chris letting me know I should.
#3
Update 002:
Added a new variable for mysql fields called table (forgot to add in 001)
Added verification of email and a check to make sure the email addresses match each other.
Added a username check function which will make sure the username is at least 4 characters long.