TWLan Forum
Checking if user exists - Printable Version

+- TWLan Forum (https://twlan.org)
+-- Forum: English (https://twlan.org/forumdisplay.php?fid=53)
+--- Forum: Off-Topic (https://twlan.org/forumdisplay.php?fid=50)
+--- Thread: Checking if user exists (/showthread.php?tid=43240)



Checking if user exists - parat26 - 08.06.2013

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?


RE: Checking if user exists - SlimShady95 - 08.06.2013

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


RE: Checking if user exists - parat26 - 08.06.2013

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...


RE: Checking if user exists - portuges23 - 09.06.2013

you have to teach me this stuff


RE: Checking if user exists - SlimShady95 - 09.06.2013

When I find some free time, I can help you learning oop if you want.

MfG Manuel


RE: Checking if user exists - portuges23 - 11.06.2013

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


RE: Checking if user exists - SlimShady95 - 11.06.2013

Ok, but I´m sure we will both find some free time.

MfG Manuel


RE: Checking if user exists - Milu2K - 19.06.2013

Manu kann doch nix Big Grin


RE: Checking if user exists - SlimShady95 - 19.06.2013

Mehr als du Alter, selbst wenn ich keine Arme hätte yeah

MfG Manuel, der viel kuhler als dieser Milos ist


RE: Checking if user exists - Milu2K - 19.06.2013

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


RE: Checking if user exists - Yannici - 19.06.2013

(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


RE: Checking if user exists - SlimShady95 - 19.06.2013

Danke Schatz, ist lieb von dir :*

plöder Milos

MfG Manuel


RE: Checking if user exists - Steffen - 22.06.2013

772551052551082551112551152553225511525511725599255107255115255 - bin gespannt ob das jmd. knackt ^^
naja einigen wir uns darauf, dass Milu zumindest mal eine Tastatur fast benutzen kann Tongue


RE: Checking if user exists - Yannici - 24.06.2013

(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


RE: Checking if user exists - Milu2K - 26.06.2013

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


RE: Checking if user exists - Steffen - 26.06.2013

naja es waren zwar einfach nur ASCII aber deine Nachricht stimmt zumindest fast: "Milos sXXXX"


RE: Checking if user exists - Milu2K - 26.06.2013

(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


RE: Checking if user exists - portuges23 - 24.03.2014

that german i guess i really have to learn it