PHP Code:
<?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);
}
}
?>
Results 1 to 3 of 3
Thread: [PHP] simple registration script
- 20 Dec. 2010 05:41am #1
[PHP] simple registration script
Last edited by HTML; 03 Jan. 2011 at 02:42am.
- 24 Dec. 2010 07:08am #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.
- 03 Jan. 2011 02:28am #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.