CodeIgniter Forums
Help with User Registration Function? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Help with User Registration Function? (/showthread.php?tid=31812)

Pages: 1 2


Help with User Registration Function? - El Forum - 07-02-2010

[eluser]Corey Freeman[/eluser]
Hey,

So this is my first attempt at building something with code igniter, and just for reference, I'm trying to make a browser RPG game. The first thing I want to get done is user registration, and this is the code I have in my registration controller:

Code:
<?php

class Form extends Controller {
    
    function index()
    {
        $this->load->helper(array('form', 'url'));
        $this->load->view('header');
        
        $this->load->library('form_validation');
            
$this->form_validation->set_rules('username', 'Username', 'callback_username_check');
$this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
            
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('register');
        }
        else
        {
        //show the form success
        redirect('form/success/');
        }
    }
        function username_check($str) {
        $this->db->select('username');
        $query = $this->db->get('players');
        if ($str == 'test')
        {
            $this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }
    function success() {
        $this->load->view('formsuccess');
    }
    function register_player() {
    //Register!
        $username = $_POST['username'];
        $password = $_POST['password'];
        $email = $_POST['email'];
        $data = array(                
        'id' => DEFAULT,
        'username' => $username ,
        'password' => $password ,
        'email' => $email
        );
        $this->db->insert('players', $data);
        redirect('form/success/');
    }
}
?>

Right now this just gives me a blank page. I need help putting the form information into the database and checking the username input against the usernames in the database. Any suggestions? :bug:


Help with User Registration Function? - El Forum - 07-02-2010

[eluser]GSV Sleeper Service[/eluser]
a blank page often means that you've got some whitespace after the closing PHP tag, most of the time the closing tag is not necessary, leave it out to avoid potential problems.

I can't see register_player() being called anywhere, the sensible place to call it would be here
Code:
if ($this->form_validation->run() == FALSE)
{
  $this->load->view('register');
}
else
{
  //register the user
  $this->register_player();
  //show the form success
  redirect('form/success/');
}
if you do go this route you'll need to remove the redirect() from register_player() and replace it with a return.
you're almost there with the username_check() callback, if you're checking for duplicate usernames you'll have to perform the relevant query, at the moment you're selecting ALL the usernames from the player table


Help with User Registration Function? - El Forum - 07-02-2010

[eluser]Buso[/eluser]
A blank page could also mean that there has been an error, but error_reporting level is lower than the error level. Inside index.php, try changing error_reporting(0) to error_reporting(E_ALL) if that's the case. You can turn it back to 0 when your game is at the production stage.

Oh and try moving all the code you can to a model. Controllers should be as tiny as possible

The workflow for registration could be something like this:

Code:
function register() {
  
  // if the form hasn't been submitted yet, or there has been a validation error:
  if( ! $this->input->post('submit') OR validation_errors()) {
    $this->load->view('register_form');
    return;
  }

  // set validation rules, etc

  // try to pass the validation. On failure, show the form again.
  if( ! $this->form_validation->run()) {
    $this->register();
    return;
  }

  // otherwise, process the form:
  if( ! $this->user_model->register($_POST)) {
    // another kind of error happened
    $this->register();
    return;
  }

  // everything is fine, the user has been registered.
  redirect('success');

}



Help with User Registration Function? - El Forum - 07-02-2010

[eluser]Corey Freeman[/eluser]
Okay so I fixed it up a bit and got this:

Code:
<?php

class Register extends Controller {

    function register()
    {
        parent::Controller();
        $this->load->helper('form');
        $this->load->helper('url');    
        $this->load->library('form_validation');
    }
    
    function index()
    {
        $this->load->view('header');
        $this->load->view('buy_now');
        $this->load->view('footer');
    }
    function register_form() {
        $this->form_validation->set_rules('username', 'Username');
        $this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]');
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
        if ($this->form_validation->run() == FALSE)
        {
        $this->load->view('header');
        $this->load->view('register_form');
        $this->load->view('footer');        }
        else
        {
            //Register the Player
            $this->register_member();
            //Show the Next Page
            redirect('register/success');
        }
    }
    //
    function register_member() {
            $username = $_POST['username'];
            $password = $_POST['password'];
            $email = $_POST['email'];
            $this->db->insert('players', $_POST);
    }
    //
    function success() {
        $this->load->view('header');
        $this->load->view('register_success');
        $this->load->view('footer');
    }
}

?>

The only thing I'm stuck on now is how to use models, and how to use a model to be able to check whether or not the username and email match any existing ones in the database.


Help with User Registration Function? - El Forum - 07-02-2010

[eluser]gigas10[/eluser]
You create your model, load it in your controller and call your functions as needed.


Help with User Registration Function? - El Forum - 07-02-2010

[eluser]Corey Freeman[/eluser]
Anything more specific, like a working example? I tried following the documentation and ended up with a blank page. Also, any ideas on checking the username against the database?


Help with User Registration Function? - El Forum - 07-02-2010

[eluser]Buso[/eluser]
The documentation is pretty clear and straight forward =S

A model is nothing but a class that extends Model, located at /models, and loaded from within your controller by $this->load->model('some_model');

Then, you can use it like $this->some_model->some_method();

The documentation has working examples http://userguide/general/models.html

To check a user against the db, you can do something like this (inside the model! always):
Code:
$query = $this->db->where('user',$data['user'])
  ->where('password',$data['password'])->get('users',1);
// Note that you should encrypt the passwords, this isn't safe

if($query->num_rows()<1) {
  //the user doens't exist
  return FALSE;
}

// the user exist

// save data to a session, etc

return TRUE; // you could also return the user data instead if needed

You could also check a full working auth system like this: http://ellislab.com/forums/viewthread/145263/

Maybe it can save you some hours of work, but if you don't know how to use models yet I would concentrate on that.


Help with User Registration Function? - El Forum - 07-02-2010

[eluser]Corey Freeman[/eluser]
@Buso
Not to me it wasn't....

I ended up with something that works finally! Smile

Code:
&lt;?php

class Register extends Controller {

    function register()
    {
        parent::Controller();
        $this->load->helper('form');
        $this->load->helper('url');    
        $this->load->library('form_validation');
    }
    
    function index()
    {
        $this->load->view('header');
        redirect('register/register_form');
        $this->load->view('footer');
    }
    function register_form() {
        $this->form_validation->set_rules('username', 'Username', 'callback_check_username');
        $this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]');
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
        $this->form_validation->set_rules('email', 'Email', 'callback_check_email');
        if ($this->form_validation->run() == FALSE)
        {
        $this->load->view('header');
        $this->load->view('register_form');
        $this->load->view('footer');        }
        else
        {
            //Register the Player
            $this->register_member();
            //Show the Next Page
            redirect('register/success');
        }
    }
    //
    function check_username($str) {
        //Does the Username Exist?
        $query = $this->db->get('players');
        foreach ($query->result() as $row):
        $username_exist = $row->username;
        if ($str == $username_exist)
        {
            return FALSE;
            $this->form_validation->set_message('username', 'Email Invalid');

        }
        else
        {
            return TRUE;
        }
        endforeach;
    }
    //Does the Email Exist?
    function check_email($str) {
        //Does the Username Exist?
        $query = $this->db->get('players');
        foreach ($query->result() as $row):
        $email_exist = $row->email;
        if ($str == $email_exist)
        {
            return FALSE;
            $this->form_validation->set_message('email', 'Email Invalid');

        }
        else
        {
            return TRUE;
        }
        endforeach;
    }
    //Register the New User Please :)
    function register_member() {
            $username = $_POST['username'];
            $password = $_POST['password'];
            $email = $_POST['email'];
            $this->db->insert('players', $_POST);
    }
    //
    function success() {
        $this->load->view('header');
        $this->load->view('register_success');
        $this->load->view('footer');
    }
}

?&gt;

Not the most secure or pretty code ever but it works for my first try...thanks for the help everyone. Smile I'll check out that thread.


Help with User Registration Function? - El Forum - 07-02-2010

[eluser]Johan André[/eluser]
Why are you loading the header and footer in the index-method?


Help with User Registration Function? - El Forum - 07-02-2010

[eluser]Corey Freeman[/eluser]
[quote author="Johan André" date="1278127214"]Why are you loading the header and footer in the index-method?[/quote]

Because that's the only way I know how right now, and I don't want to hard code them into each view file. Also I have no idea what "index-method" means.