CodeIgniter: Tank Auth, Adding Date of Birth Issues

試著忘記壹切 提交于 2019-12-11 20:53:39

问题


Ok, So I have added a Date of Birth section for the registration process. I have done it in a weird way probably but it is a logical way to do it in my mind so as long as it works I am fine with it. I have created 3 dob fields (dob1, dob2, dob3 - for month, day, year). The problem I have right now is when a user registers the dob values are not stored properly into the database (it seems the values are just random, they show up as random numbers and letters). So I am curious as to what I am doing wrong. Here is what I have done.

EDIT: I have found the problem, but still am looking for a solution. The ordering is mixed up somewhere. My first 2 letters of email go into dob1, first 2 letters of password go into dob2 (dont know if pass or confirm pass), dob1 goes to firstname, dob2 goes to lastnam, username is fine, dob3 goes to email. So I have mixed up the ordering of my array somewhere but I am nut sure where it counts.

EDIT2: I have found my issue. Answer is below.

Register function in controllers/auth.php

function register()
        {
                if ($this->tank_auth->is_logged_in()) {                                                                 // logged in
                        redirect('');

                } elseif ($this->tank_auth->is_logged_in(FALSE)) {                                              // logged in, not activated
                        redirect('/auth/send_again/');

                } elseif (!$this->config->item('allow_registration', 'tank_auth')) {    // registration is off
                        $this->_show_message($this->lang->line('auth_message_registration_disabled'));

                } else {
                        $use_username = $this->config->item('use_username', 'tank_auth');
                        if ($use_username) {
                                $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|min_length['.$this->config->item('username_min_length', 'tank_auth').']|max_length['.$this->config->item('username_max_le\
ngth', 'tank_auth').']|alpha_dash');
                        }

                        $this->form_validation->set_rules('dob1', 'DOB1', 'trim|required|xss_clean|min_length[2]|max_length[2]');
                        $this->form_validation->set_rules('dob2', 'DOB2', 'trim|required|xss_clean|min_length[2]|max_length[2]');
                        $this->form_validation->set_rules('dob3', 'DOB3', 'trim|required|xss_clean|min_length[2]|max_length[4]');
                        $this->form_validation->set_rules('firstname', 'First Name', 'trim|xss_clean|min_length[2]|max_length[50]');
                        $this->form_validation->set_rules('lastname', 'Last Name', 'trim|xss_clean|min_length[2]|max_length[50]');

                        $this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean|valid_email');
                        $this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean|valid_email|callback_is_email_domain[ku.edu]');
                        $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|min_length['.$this->config->item('password_min_length', 'tank_auth').']|max_length['.$this->config->item('password_max_length', '\
tank_auth').']|alpha_dash');
                        $this->form_validation->set_rules('confirm_password', 'Confirm Password', 'trim|required|xss_clean|matches[password]');

                        $captcha_registration   = $this->config->item('captcha_registration', 'tank_auth');
                        $use_recaptcha                  = $this->config->item('use_recaptcha', 'tank_auth');
                        if ($captcha_registration) {
                                if ($use_recaptcha) {
                                        $this->form_validation->set_rules('recaptcha_response_field', 'Confirmation Code', 'trim|xss_clean|required|callback__check_recaptcha');
                                } else {
                                        $this->form_validation->set_rules('captcha', 'Confirmation Code', 'trim|xss_clean|required|callback__check_captcha');
                                }
                        }
                        $data['errors'] = array();

                        $email_activation = $this->config->item('email_activation', 'tank_auth');

                        if ($this->form_validation->run()) {                                                            // validation ok
                                if (!is_null($data = $this->tank_auth->create_user(
                                                $use_username ? $this->form_validation->set_value('username') : '',

                                                $this->form_validation->set_value('dob1'),
                                                $this->form_validation->set_value('dob2'),
                                                $this->form_validation->set_value('dob3'),
                                                $this->form_validation->set_value('firstname'),
                                                $this->form_validation->set_value('lastname'),

                                                $this->form_validation->set_value('email'),
                                                $this->form_validation->set_value('password'),
                                                $email_activation))) {                                                                  // success

                                        $data['site_name'] = $this->config->item('website_name', 'tank_auth');

                        $email_activation = $this->config->item('email_activation', 'tank_auth');

                        if ($this->form_validation->run()) {                                                            // validation ok
                                if (!is_null($data = $this->tank_auth->create_user(
                                                $use_username ? $this->form_validation->set_value('username') : '',

                                                $this->form_validation->set_value('dob1'),
                                                $this->form_validation->set_value('dob2'),
                                                $this->form_validation->set_value('dob3'),
                                                $this->form_validation->set_value('firstname'),
                                                $this->form_validation->set_value('lastname'),

                                                $this->form_validation->set_value('email'),
                                                $this->form_validation->set_value('password'),
                                                $email_activation))) {                                                                  // success

                                        $data['site_name'] = $this->config->item('website_name', 'tank_auth');

                                        if ($email_activation) {                                                                        // send "activate" email
                                                $data['activation_period'] = $this->config->item('email_activation_expire', 'tank_auth') / 3600;

                                                $this->_send_email('activate', $data['email'], $data);

                                                unset($data['password']); // Clear password (just for any case)

                                                $this->_show_message($this->lang->line('auth_message_registration_completed_1'));

                                        } else {
                                                if ($this->config->item('email_account_details', 'tank_auth')) {        // send "welcome" email

                                                        $this->_send_email('welcome', $data['email'], $data);
                                                }
                                                unset($data['password']); // Clear password (just for any case)

                                                $this->_show_message($this->lang->line('auth_message_registration_completed_2').' '.anchor('/auth/login/', 'Login'));
                                        }
                                } else {
                                        $errors = $this->tank_auth->get_error_message();
                                        foreach ($errors as $k => $v)   $data['errors'][$k] = $this->lang->line($v);
                                }
                        }
                        if ($captcha_registration) {
                                if ($use_recaptcha) {
                                        $data['recaptcha_html'] = $this->_create_recaptcha();
                                } else {
                                        $data['captcha_html'] = $this->_create_captcha();
                                }
                        }
                        $data['use_username'] = $use_username;
                        $data['captcha_registration'] = $captcha_registration;
                        $data['use_recaptcha'] = $use_recaptcha;
                        $this->load->view('auth/register_form', $data);
                }
        }

Libraries: Tank_Auth.php - I know this is not the whole dob, but calling this function returns nothing, i figure if I can return 1 dob then i can easily return them all. EDIT: I just realized I did not include all of my changes for this file.

else {  
    $this->ci->session->set_userdata(array(     
        'user_id'  => $user->id,    
        'dob1'     => $user->dob1,
        'dob2'     => $user->dob2,
        'dob3'     => $user->dob3,
        'firstname'  => $user->firstname,
        'lastname'  => $user->lastname,   
        'username'  => $user->username, 
        'status'  => ($user->activated == 1) ? STATUS_ACTIVATED : STATUS_NOT_ACTIVATED, 

         ));

. . .

function get_dob()
        {
                return $this->ci->session->userdata('dob1');
        }

. . .

function create_user($username, $firstname, $lastname, $email, $password, $email_activation, $dob1, $dob2, $dob3)
{
    if ((strlen($username) > 0) AND !$this->ci->users->is_username_available($username)) {
        $this->error = array('username' => 'auth_username_in_use');
        $hashed_password = $hasher->HashPassword($password);
        $data = array(
            'firstname' => $firstname,
            'lastname' => $lastname,
            'dob1' => $dob1,
            'dob2' => $dob2,
            'dob3' => $dob3,
            'username' => $username,
            'password' => $hashed_password,
            'email' => $email,

Views: registration_form.php

<?php
if ($use_username) {
        $username = array(
                'name'  => 'username',
                'id'    => 'username',
                'value' => set_value('username'),
                'maxlength'     => $this->config->item('username_max_length', 'tank_auth'),
                'size'  => 30,
        );
}
$email = array(
        'name'  => 'email',
        'id'    => 'email',
        'value' => set_value('email'),
        'maxlength'     => 80,
        'size'  => 30,
);
$firstname = array(
        'name'  => 'firstname',
        'id'    => 'firstname',
        'value' => set_value('firstname'),
        'maxlength'     => 50,
        'size'  => 30,
);
$lastname = array(
        'name'  => 'lastname',
        'id'    => 'lastname',
        'value' => set_value('lastname'),
        'maxlength'     => 50,
        'size'  => 30,
);
$password = array(
        'name'  => 'password',
        'id'    => 'password',
        'value' => set_value('password'),
        'maxlength'     => $this->config->item('password_max_length', 'tank_auth'),
        'size'  => 30,
);
$confirm_password = array(
        'name'  => 'confirm_password',
        'id'    => 'confirm_password',
        'value' => set_value('confirm_password'),
        'maxlength'     => $this->config->item('password_max_length', 'tank_auth'),
        'size'  => 30,
);
$dob1 = array(
        'name'  => 'dob1',
        'id'    => 'dob1',
        'value' => set_value('dob1'),
        'maxlength'     => 2,
        'size'  => 30,
);
$dob2 = array(
        'name'  => 'dob2',
        'id'    => 'dob2',
        'value' => set_value('dob2'),
        'maxlength'     => 2,
        'size'  => 30,
);
$dob3 = array(
        'name'  => 'dob3',
        'id'    => 'dob3',
        'value' => set_value('dob3'),
        'maxlength'     => 4,
        'size'  => 30,
);
$captcha = array(
        'name'  => 'captcha',
        'id'    => 'captcha',
        'maxlength'     => 8,
);
?>
<?php echo form_open($this->uri->uri_string()); ?>
<table>
        <?php if ($use_username) { ?>
        <tr>
                <td><?php echo form_label('Username', $username['id']); ?></td>
                <td><?php echo form_input($username); ?></td>
                <td style="color: red;"><?php echo form_error($username['name']); ?><?php echo isset($errors[$username['name']])?$errors[$username['name']]:''; ?></td>
        </tr>
        <?php } ?>
        <tr>
                <td><?php echo form_label('Email Address', $email['id']); ?></td>
                <td><?php echo form_input($email); ?></td>
                <td style="color: red;"><?php echo form_error($email['name']); ?><?php echo isset($errors[$email['name']])?$errors[$email['name']]:''; ?></td>
        </tr>
        <tr>
                <td><?php echo form_label('First Name', $firstname['id']); ?></td>
                <td><?php echo form_input($firstname); ?></td>
                <td style="color: red;"><?php echo form_error($firstname['name']); ?><?php echo isset($errors[$firstname['name']])?$errors[$firstname['name']]:''; ?></td>
        </tr>
        <tr>
                <td><?php echo form_label('Last Name', $lastname['id']); ?></td>
                <td><?php echo form_input($lastname); ?></td>
                <td style="color: red;"><?php echo form_error($lastname['name']); ?><?php echo isset($errors[$lastname['name']])?$errors[$lastname['name']]:''; ?></td>
        </tr>
        <tr>
                <td><?php echo form_label('Password', $password['id']); ?></td>
                <td><?php echo form_password($password); ?></td>
                <td style="color: red;"><?php echo form_error($password['name']); ?></td>
        </tr>
        <tr>
                <td><?php echo form_label('Confirm Password', $confirm_password['id']); ?></td>
                <td><?php echo form_password($confirm_password); ?></td>
                <td style="color: red;"><?php echo form_error($confirm_password['name']); ?></td>
        </tr>
        <tr>
                <td><?php echo form_label('Date of Birth', $dob1['id']); ?></td>
                <td><?php echo form_input($dob1); ?></td>
                <td><?php echo form_input($dob2); ?></td>
                <td><?php echo form_input($dob3); ?></td>
                <td style="color: red;"><?php echo form_error($dob1['name']); ?><?php echo isset($errors[$dob1['name']])?$errors[$dob1['name']]:''; ?></td>
        </tr>

        <?php if ($captcha_registration) {
                if ($use_recaptcha) { ?>
        <tr>
                <td colspan="2">
                        <div id="recaptcha_image"></div>
                </td>
                <td>
                        <a href="javascript:Recaptcha.reload()">Get another CAPTCHA</a>
                        <div class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type('audio')">Get an audio CAPTCHA</a></div>
                        <div class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type('image')">Get an image CAPTCHA</a></div>
                </td>
        </tr>
        <tr>
                <td>
                        <div class="recaptcha_only_if_image">Enter the words above</div>
                        <div class="recaptcha_only_if_audio">Enter the numbers you hear</div>
                </td>
                <td><input type="text" id="recaptcha_response_field" name="recaptcha_response_field" /></td>
                <td style="color: red;"><?php echo form_error('recaptcha_response_field'); ?></td>
                <?php echo $recaptcha_html; ?>
        </tr>
        <?php } else { ?>
        <tr>
                <td colspan="3">
                        <p>Enter the code exactly as it appears:</p>
                        <?php echo $captcha_html; ?>
                </td>
        </tr>
        <tr>
                <td><?php echo form_label('Confirmation Code', $captcha['id']); ?></td>
                <td><?php echo form_input($captcha); ?></td>
                <td style="color: red;"><?php echo form_error($captcha['name']); ?></td>
        </tr>
<?php }
        } ?>
</table>
<?php echo form_submit('register', 'Register'); ?>
<?php echo form_close(); ?>

**ALSO: If you know how to change the width of the td tag for the registration_form.php then please let me know. I am struggling with that also the simple does not work.


回答1:


I found where I messed up. I was passing the fields in the wrong order when I called the create_user() function in auth.php. I when into the Tank_Auth.php file and reordered my parameters for that function to match what I was passing in the auth.php.

function create_user($username, $firstname, $lastname, $email, $password, $email_activation, $dob1, $dob2, $dob3)

changed to

function create_user($username, $dob1, $dob2, $dob3, $firstname, $lastname, $email, $password, $email_activation)

This matched the create_user() call i had in the auth.php:

if (!is_null($data = $this->tank_auth->create_user(
                                                $use_username ? $this->form_validation->set_value('username') : '',

                                                $this->form_validation->set_value('dob1'),
                                                $this->form_validation->set_value('dob2'),
                                                $this->form_validation->set_value('dob3'),
                                                $this->form_validation->set_value('firstname'),
                                                $this->form_validation->set_value('lastname'),

                                                $this->form_validation->set_value('email'),
                                                $this->form_validation->set_value('password'),
                                                $email_activation)))


来源:https://stackoverflow.com/questions/12379703/codeigniter-tank-auth-adding-date-of-birth-issues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!