Details:
I am creating a registration page for a game, and the game has two server with different ip's/host and different database, the other one is hosted on Euro and the other is hosted on USA, and what I am planning on my registration page are when the user successfully fill up the form and click "Register" button, the info the user put will be send to the two different database, I search a topic related to my problem, and follow the instructions to fixed it, but it didn't do any good thing. So if you will ask on my code here it is.
Database.php
<?php
class Database{
private static $instance = null;
private $stmt = null;
private $host = '';
private $dbname = '';
private $username = '';
private $password = '';
public function __construct(){
$dns = "mysql:dbname=".$this->dbname.";host:".$this->host;
$username= $this->username;
$password= $this->password;
$db = null;
$this->db = new PDO($dns,$username,$password);
}
public static function getInstance(){
if(self::$instance == null){
try{
self::$instance = new Database();
}catch(PDOException $e){
echo $e->getCode();
}
}
return self::$instance;
}
public function query($stmt){
$this->stmt = $this->db->prepare($stmt);
}
//get sql statement
public function getStmt(){
return $this->stmt;
}
public function bind($index, $value, $type = null){
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($index, $value, $type);
}
public function execute(){
return $this->stmt->execute();
}
public function resultset(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function single()
{
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
public function __destruct(){
$this->db = null;
}
}
I'm sorry if i didn't add the database info, just for security sake :), so I think all I need to do is to add something on the Database.php , but I really don't know what.
now here is the validation process code of my registration page
RegistrationProcess.php
<?php
include_once 'Input.php';
include_once 'Database.php';
include_once 'Helper.php';
//variables
$db = Database::getInstance();
$username = '';
$password = '';
$email = '';
$country = '';
$city = '';
$question = '';
$answer = '';
$ip = Helper::get_ip_address();
$valid = true;
$msg = '';
if (Input::hasPost()) {
if (Input::post('username') === null) {
$msg = 'Please put your username.';
$valid = false;
$username = Input::post('username');
} else if (Input::post('password') === null || Input::post('con_password') === null ) {
$msg = 'Input password properly!';
$valid = false;
} else if (Input::post('password') !== Input::post('con_password')) {
$msg = 'Password and confirm password must be the same.';
$valid = false;
} else if (Input::post('country') === 'select') {
$msg = 'Select a country.';
$valid = false;
} else if (Input::post('email') === null) {
$msg = 'Put your email.';
$valid = false;
} else if (!filter_var(Input::post('email'), FILTER_VALIDATE_EMAIL)) {
$msg = 'Invalid email.';
$valid = false;
} else if (Input::post('question') === 'select') {
$msg = 'Select a question.';
$valid = false;
} else if (Input::post('answer') === null) {
$msg = 'Put your answer.';
$valid = false;
}
if ($valid) {
$db = Database::getInstance();
$username = Input::post('username');
$password = Input::post('con_password');
$email = Input::post('email');
$country = Input::post('country');
$question = Input::post('question');
$answer = Input::post('answer');
//banned account
$sql = "SELECT * FROM accounts WHERE IP = :ip AND State = 1";
$db->query($sql);
$db->bind(':ip', $ip);
$banned = $db->single();
// > 1 account
$sql = "SELECT * FROM accounts WHERE IP = :ip";
$db->query($sql);
$db->bind(':ip', $ip);
$anotherAccount = $db->resultset();
//already exist name
$sql = "SELECT * FROM accounts WHERE Username = :username";
$db->query($sql);
$db->bind(':username', $username);
$existAccount = $db->single();
if ($existAccount && (count($existAccount) > 0)) {
$msg = 'The Username is already exist.';
$valid = false;
} else if ($banned && (count($banned) > 0)) {
$msg = 'Sorry you cannot register because your banned!';
$valid = false;
} else if ($anotherAccount && (count($anotherAccount) > 5)) {
$msg= 'Sorry you cannot register because you made to many accounts.';
$valid = false;
} else {
$sql = "INSERT INTO accounts (`Username`, `Password`, `IP`, `Email`, `Question`, `answer`, `Country`)
VALUES (:username, :password, :ip, :email, :question, :answer, :country)";
$db->query($sql);
$db->bind(':username', $username);
$db->bind(':password', $password);
$db->bind(':ip', $ip);
$db->bind(':email', $email);
$db->bind(':question', $question);
$db->bind(':answer', $answer);
$db->bind(':country', $country);
if ($db->execute()) {
$msg = "The user has been succesfully added into the database! You can now log into the community and in-game!";
$msg .= " Thank you for registering for West Gaming!<br>";
$msg .= "<span class='management'>- West Gaming Staff</span>";
$valid = true;
unset($_POST);
} else {
$msg = 'There is a problem in our system <br>';
$msg .= 'Please register again with the same data.';
$valid = false;
}
}
}
}
and that is.. hope you can help me and if you do, Thanks in advance.