(This post was last modified: 08.06.2013, 16:05 by SlimShady95.)
08.06.2013, 15:52
PHP Code:
if (mysql_num_rows($checkuser) > 0 && empty($_POST['txtusername']) == true && empty($_POST['txtpassword']) == true && empty($_POST['txtemail']) === true)
has to be
PHP Code:
if (mysql_num_rows($checkuser) > 0 || empty($_POST['txtusername']) == true || empty($_POST['txtpassword']) == true || empty($_POST['txtemail']) == true)
Edit: why don't you use mvc? Or generally oop? It's not very good, if everything of your game is coded like the part above. First, it's very ineffective and secondly it's going to be very messed up.
Here is an example how I do registrations:
PHP Code:
// [...]
class Home extends CI_Controller
{
// [...]
/**
* register
*/
public function signup()
{
if (false != $this->input->post())
{
$username = escape(trim($this->input->post('username')));
$password[] = escape(trim($this->input->post('password')));
$password[] = escape(trim($this->input->post('password2')));
$email = escape(trim($this->input->post('email')));
$error = array();
// check all inputs
/**
* username
*/
if ('' == $username || 3 > strlen($username) || 15 < strlen($username))
{
$error[] = tr('home_signup_error_username');
}
elseif (!preg_match('/\b([a-zA-Z0-9\-_ ]*)\b/', $username))
{
$error[] = tr('home_signup_error_username2');
}
elseif ($this->users_model->user_exists($username))
{
$error[] = tr('home_signup_error_username3');
}
/**
* password
*/
if ('' == $password[0] || '' == $password[1] || 5 > strlen($password[0]) || 5 > strlen($password[1]))
{
$error[] = tr('home_signup_error_password');
}
elseif ($password[0] != $password[1])
{
$error[] = tr('home_signup_error_password2');
}
/**
* email
*/
if ('' == $email || 5 > strlen($email) )
{
$error[] = tr('home_signup_error_email');
}
elseif (!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$error[] = tr('home_signup_error_email2');
}
elseif (true == $this->users_model->user_exists($email, 'email'))
{
$error[] = tr('home_signup_error_email3');
}
// handles the template
if (0 == count($error))
{
// send the activation email
$this->load->config('email');
$from = $this->config->item('email_from');
$this->load->helper('email');
// -.-
$this->users_model->add_user($username, $password[0], $email);
$user_data = $this->users_model->get_data($username, array('id', 'activation_key'));
$url = base_url() . 'home/activation/' . $user_data['id'] . '/' . $user_data['activation_key'];
$para1 = tr('home_signup_email_paragraph1');
$para2 = tpr('home_signup_email_paragraph2', array('url' => $url));
$subject = tr('home_signup_email_subject');
$message = '<p>' . $para1 . '</p><p><a href="' . $url . '">' . $url . '</a></p><p>' . $para2 . '</p>';
if(true == send_email($email, $from, $subject, $message))
{
$data['name'] = $username;
$data['email'] = $email;
$template = 'signup/success';
}
else
{
exit();
}
}
else
{
$data['error'] = $error;
$template = 'signup/error';
}
}
else
{
$data = array();
$template = 'signup';
}
load_view('home/' . $template, $data);
}
// [...]
}
with the model:
PHP Code:
// [...]
class Users_Model extends CI_Model
{
/**
* checks, if an user does allready exists
*
* @param mixed the value
* @param string the type (standard is username)
* @return bool true, if the user does exists, false
* if he does not
*/
public function user_exists($value, $type = 'username')
{
$possible = array('username', 'email', 'id');
if (!in_array($type, $possible))
{
return false;
}
$query = $this->db->query('SELECT `id` FROM `users` WHERE `' . $type . '` = \'' . $value . '\'');
if (0 == $query->num_rows())
{
return false;
}
return true;
}
// ---------------------------------------------------------------------
/**
* adds an user to the database
*
* @param string the name of the user
* @param string the password of the user
* @param string the email address of the user
* @return true, if the adding was successfull
* false, if the user could not be added
*/
public function add_user($username, $password, $email)
{
$check = array(
'username' => $username,
'email' => $email
);
foreach ($check as $type => $value)
{
if (true == $this->user_exists($value, $type))
{
return false;
}
}
$data = array(
'username' => $username,
'password' => user_hash($username, $password, $email),
'email' => $email,
'register_time' => time(),
'activation_key' => activation_hash(),
);
$query = $this->db->insert('users', $data);
}
// [...]
}
MfG Manuel