Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Checking if user exists
#2
PHP Code:
if (mysql_num_rows($checkuser) > && empty($_POST['txtusername']) == true && empty($_POST['txtpassword']) == true && empty($_POST['txtemail']) === true

has to be

PHP Code:
if (mysql_num_rows($checkuser) > || 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 || 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] || strlen($password[0]) || strlen($password[1]))
            {
                
$error[] = tr('home_signup_error_password');
            }
            elseif (
$password[0] != $password[1])
            {
                
$error[] = tr('home_signup_error_password2');
            }
            
            
/**
             * email
             */
            
if ('' == $email || strlen($email) )
            {
                
$error[] = tr('home_signup_error_email');
            }
            elseif (!
filter_var($emailFILTER_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 (== 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 (
== $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
Reply


Messages In This Thread
Checking if user exists - by parat26 - 08.06.2013, 14:49
RE: Checking if user exists - by SlimShady95 - 08.06.2013, 15:52
RE: Checking if user exists - by parat26 - 08.06.2013, 20:00
RE: Checking if user exists - by portuges23 - 09.06.2013, 00:49
RE: Checking if user exists - by SlimShady95 - 09.06.2013, 20:03
RE: Checking if user exists - by portuges23 - 11.06.2013, 13:50
RE: Checking if user exists - by SlimShady95 - 11.06.2013, 14:18
RE: Checking if user exists - by Milu2K - 19.06.2013, 13:05
RE: Checking if user exists - by SlimShady95 - 19.06.2013, 16:54
RE: Checking if user exists - by Milu2K - 19.06.2013, 17:52
RE: Checking if user exists - by Yannici - 19.06.2013, 21:39
RE: Checking if user exists - by SlimShady95 - 19.06.2013, 22:31
RE: Checking if user exists - by Steffen - 22.06.2013, 18:29
RE: Checking if user exists - by Yannici - 24.06.2013, 13:34
RE: Checking if user exists - by Milu2K - 26.06.2013, 14:23
RE: Checking if user exists - by Steffen - 26.06.2013, 14:41
RE: Checking if user exists - by Milu2K - 26.06.2013, 14:47
RE: Checking if user exists - by portuges23 - 24.03.2014, 18:52



Users browsing this thread: 3 Guest(s)