Hello,
I don't have much time, so i will just make a little explanation.
This is a simple PHP Script for Register Pages, I aint releasing the Form because i havent done it, i just did the PHP Script to reply to another thread and then i tought it was gonna be useful for other peoples.
First, it uses PDO for MySql, it requires a higher version of PHP and needs to be enabled if you use Wampserver. Wont work on AppServ. Usually Host Providers already have this enabled so you wont need to worry, otherwise, if it doesnt work, you should ask them to enable PDO for MySql on your Apache Server.
Second, i havent done the Errors handle, i have made the functions, but if you need it to show messages, you gotta do it yourself.
The functions, Configurations and Handle are on the same file, so you gotta split them if you want.
If you need a guide for how to use PDO, i recommend this [Only registered and activated users can see links. Click Here To Register...]. Its not hard and i recommend who use mysql_* to start learning PDO. I think its so cool.
The Code:
*Damn, pressed CTRL+W twice, hate this*
FAQ:
Why do i you use bindValue if you can save n lines by doing different?
So newbies can see +- how it works and it will become easier for them to edit. With a query where ill set 20 values, it will take 20 lines, but at least newbies will get the touch, but yes, it can be done in 1 line.
Well, i wont rewrite everything, i gotta go.
Later i Edit with something else.
I am accepting suggestions for improvements.
Good luck
I don't have much time, so i will just make a little explanation.
This is a simple PHP Script for Register Pages, I aint releasing the Form because i havent done it, i just did the PHP Script to reply to another thread and then i tought it was gonna be useful for other peoples.
First, it uses PDO for MySql, it requires a higher version of PHP and needs to be enabled if you use Wampserver. Wont work on AppServ. Usually Host Providers already have this enabled so you wont need to worry, otherwise, if it doesnt work, you should ask them to enable PDO for MySql on your Apache Server.
Second, i havent done the Errors handle, i have made the functions, but if you need it to show messages, you gotta do it yourself.
The functions, Configurations and Handle are on the same file, so you gotta split them if you want.
If you need a guide for how to use PDO, i recommend this [Only registered and activated users can see links. Click Here To Register...]. Its not hard and i recommend who use mysql_* to start learning PDO. I think its so cool.
The Code:
Code:
<?php
/*
* Generic Register Page
* Author: Felipe Vieira Vendramini
* Configuration
*/
$MySql["Host"] = "localhost"; // host, duuh
$MySql["User"] = "root"; // Username
$MySql["Pass"] = "test"; // Password
$MySql["Data"] = "zf"; // Database
$Database = new PDO('mysql:host='.$MySql["Host"].';dbname='.$MySql["Data"].';charset=utf8', $MySql["User"], $MySql["Pass"], array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
$Database->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
/*
* Common Functions
*/
function CheckIfNegative($Number)
{
if($Number < 0)
$Number *= (-1);
return $Number;
}
function CheckINT($Number)
{
return filter_var($Number, FILTER_VALIDATE_INT);
}
function ValidateUser($string){
$expression = "[A-Za-z0-9]";
return preg_match($expression, $string);
}
function ValidateEmail($string){
return filter_var($string, FILTER_VALIDATE_EMAIL);
}
/*
* Form validation
* Fill:
* Username: varchar(16)
* Password: varchar(16)
* SecurityCode: bigint(8)
* Email: varchar(64)
*/
if(!ValidateUser($_POST["Username"]) || !ValidateUser($_POST["Password"])){
//Handle Invalid Username or Password
}
$Username = $_POST["Username"];
$Password = $_POST["Password"];
if(strlen($_POST["Username"]) < 6 || strlen($_POST["Username"]) > 16){
//Handle Username Lenght Error
}
if(strlen($_POST["Password"]) < 6 || strlen($_POST["Password"]) > 16){
//Handle Password Lenght Error
}
$CheckName = $Database->prepare("SELECT username FROM `account` WHERE `username`=:name");
$CheckName->bindValue(':name', $Username, PDO::PARAM_STR);
$CheckName->execute();
$CountN = $CheckName->rowCount();
if($CountN > 0){
//Handle Existing Username
}
if(!ValidateEmail($_POST["Email"])){
//Handle Invalid E-mail
}
$Email = $_POST["Email"];
$CheckMail = $Database->prepare("SELECT email FROM `account` WHERE `email`=:mail");
$CheckMail->bindValue(':mail', $Email, PDO::PARAM_STR);
$CheckMail->execute();
$CountM = $CheckMail->rowCount();
if($CountM > 0){
//Handle Existing Mail
}
$SecurityCode = CheckIfNegative($_POST["SecurityCode"]);
if(!CheckINT($SecurityCode)){
//Handle Invalid format Security Code
}
if($SecurityCode < 10000000){
//Handle 8 Digits Security Code Error
}
$InsertRow = $Database->prepare("INSERT INTO account (`username`,`password`,`email`,`code`) VALUES (:name, :pass, :mail, :code)");
$InsertRow->bindValue(':name', $Username, PDO::PARAM_STR);
$InsertRow->bindValue(':pass', $Password, PDO::PARAM_STR);
$InsertRow->bindValue(':mail', $Email, PDO::PARAM_STR);
$InsertRow->bindValue(':code', $SecurityCode, PDO::PARAM_INT);
$Check = $InsertRow->execute();
if(!$Check){
//Handle Insert failed
}
die("OK");
FAQ:
Why do i you use bindValue if you can save n lines by doing different?
So newbies can see +- how it works and it will become easier for them to edit. With a query where ill set 20 values, it will take 20 lines, but at least newbies will get the touch, but yes, it can be done in 1 line.
Well, i wont rewrite everything, i gotta go.
Later i Edit with something else.
I am accepting suggestions for improvements.
Good luck