Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Checking if user exists
#1
Ok guys, these weeks I have started developing a browser game since my PHP skills have developed. I am stuck at checking if username already exists.

The registration works perfect however I am able to register multiple accounts with the same name and I can't figure out whats the problem in my code:

Code:
<?php
include("../config/database.config.php");

Function Register()
{
    $conn = mysql_connect(HOST,USER,PASS);
    mysql_select_db(DATABASE,$conn);

    $username    = mysql_real_escape_string(trim($_POST['txtusername']));
    $password    = mysql_real_escape_string($_POST['txtpassword']);
    $email        = mysql_real_escape_string(trim($_POST['txtemail']));

    $username = stripslashes($username);
    $password = stripslashes($password);
    $email = stripslashes($email);

    $checkuser = mysql_query("SELECT * FROM users WHERE username = '". $username ."' OR email = '". $email ."'");

    // $password    = md5($password);
    $salt = sha1(md5($password));
    $password = md5($password.$salt);

    if (mysql_num_rows($checkuser) > 0 && empty($_POST['txtusername']) == true && empty($_POST['txtpassword']) == true && empty($_POST['txtemail']) === true)
    {

        header('location:../index.php?p=error');

    } else {
        $query ="insert into users(username,password,email)values('$username','$password','$email')";
        $res = mysql_query($query);
        header('location:../index.php?p=success');    
    }
}

Register();

Any suggestions?
Reply
#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
#3
Perfect, thats what I was learning lately. OOP is my favorite however I know the Class structure and the functions but the problem is that I don't know how to call correctly the functions from the classes.

I mean I know lets say $app = new App() etc...

But I need help with this I need to learn more Sad...
Reply
#4
you have to teach me this stuff

dont see if you dont like to watch people losing their head
Reply
#5
When I find some free time, I can help you learning oop if you want.

MfG Manuel
Reply
#6
i guess i need to find free time too
my work is kind of "you will be called when needed"
so i normaly work like 9 to 17 but some times i stay there till 21

dont see if you dont like to watch people losing their head
Reply
#7
Ok, but I´m sure we will both find some free time.

MfG Manuel
Reply
#8
Manu kann doch nix Big Grin
✝ RiP 
Weiter geht's  Cool
Reply
#9
Mehr als du Alter, selbst wenn ich keine Arme hätte yeah

MfG Manuel, der viel kuhler als dieser Milos ist
Reply
#10
PHP Code:
$manu->fuck($manu); 

<3 <3 <3

MfG :priest: Der Priester der Manu's :priest:

Edit: Haha wie ich gerade Fick dich auf PHPisch gesagt hab xD
✝ RiP 
Weiter geht's  Cool
Reply
#11
(19.06.2013, 17:52)Milu2K Wrote:
PHP Code:
$manu->fuck($manu); 

<3 <3 <3

MfG :priest: Der Priester der Manu's :priest:

Edit: Haha wie ich gerade Fick dich auf PHPisch gesagt hab xD

PHP Code:
$milos = new UnkuhlerMilos();

while(
$milos->isAlive()) {
    
$milos->punchHimself();
}

echo 
"MILOS DER UNKUHLE IST TOD!"

Ich verteidige meinen Manuel!!
Muahaha ... b2t.

so far
Yannici
Manchmal denke ich:
Reply
#12
Danke Schatz, ist lieb von dir :*

plöder Milos

MfG Manuel
Reply
#13
772551052551082551112551152553225511525511725599255107255115255 - bin gespannt ob das jmd. knackt ^^
naja einigen wir uns darauf, dass Milu zumindest mal eine Tastatur fast benutzen kann Tongue
LG
steffen
Reply
#14
(22.06.2013, 18:29)Steffen Wrote: naja einigen wir uns darauf, dass Milu zumindest mal eine Tastatur fast benutzen kann Tongue

#accepted. Big Grin

Und was soll man da denn bitteschön knacken können?! oô
Das einzigste was ich erkenne ist, dass "255" 'ne Trennung sein könnte oder so ^^

so far
Yannici
Manchmal denke ich:
Reply
#15
Wtf schon nur beim hinsehen mit einem Auge sieht man, dass das im Oktalsystem geschrieben ist.
Also Yannic, schäm dich... xD

Naja, seine Nachricht war:
Quote:IT'S NOT AN OCTAL VALUE...

Steffen, mich kriegste nicht! NIEMALS!!! xD

Mit pösen Grüssen
✝ RiP 
Weiter geht's  Cool
Reply
#16
naja es waren zwar einfach nur ASCII aber deine Nachricht stimmt zumindest fast: "Milos sXXXX"
LG
steffen
Reply
#17
(26.06.2013, 14:41)Steffen Wrote: naja es waren zwar einfach nur ASCII aber deine Nachricht stimmt zumindest fast: "Milos sXXXX"

ah lol da hat mich der converter verarscht und dachte wirklich du hättest "ITS NOT AN OCTAL VALUE!!!" hingeschrieben xD

Milos Sauft is das ergebniss Big Grin

haha und blind bin ich auch xD
✝ RiP 
Weiter geht's  Cool
Reply
#18
that german i guess i really have to learn it

dont see if you dont like to watch people losing their head
Reply




Users browsing this thread: 5 Guest(s)