Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
431 views
in Technique[技术] by (71.8m points)

php - array as a field name in codeigniter

This is the image of what the form look like

here is the code of the phone mobile or email field

<?php echo form_input(array('name'=>'options[phone][email]', 'value'=>set_value('options[phone][email]', (isset($user['phone']) || isset($user['email'])) ? $user['phone'] : $user['email']), 'class'=>'form-control')); ?>

This is the controller

{
    // validators
    $this->form_validation->set_error_delimiters($this->config->item('error_delimeter_left'), $this->config->item('error_delimeter_right'));
    $this->form_validation->set_rules('username', lang('users input username'), 'required|trim|min_length[5]|max_length[30]|callback__check_username');
    $this->form_validation->set_rules('first_name', lang('users input first_name'), 'required|trim|min_length[2]|max_length[32]');
    $this->form_validation->set_rules('last_name', lang('users input last_name'), 'required|trim|min_length[2]|max_length[32]');
    $this->form_validation->set_rules('options['phone']['email']', lang('users input email_phone'), 'required|trim|max_length[256]|regex_match[/[a-z0-9._-]+@[a-z0-9._-]{2,}.[a-z]{2,4}|^6[0-9]{8}$/]');
    $this->form_validation->set_rules('language', lang('users input language'), 'required|trim');
    $this->form_validation->set_rules('password', lang('users input password'), 'required|trim|min_length[5]');
    $this->form_validation->set_rules('password_repeat', lang('users input password_repeat'), 'required|trim|matches[password]');
        
    //your site secret key
    $secret = '6LcqU-4ZAAAAAE2fSjwGdlJRrwts_MBeQKOKNRN7';
    //get verify response data captcha
    $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
    $responseData = json_decode($verifyResponse);
    
    //we get the email and mobile number
    $post_email = $this->input->post('email');
    $post_phone = $this->input->post('phone');

    if ($this->form_validation->run() == TRUE)
    {
        if ($responseData->success) {
                    
            $check_protect = $this->protect_username->check_username($this->input->post('username', TRUE));
                    
            if ($check_protect == TRUE) {
                        
                // save the changes
                $validation_code = $this->users_model->create_profile($this->security->xss_clean($this->input->post()), $_SERVER["REMOTE_ADDR"]);

                if ($validation_code)
                {
                    //we check if it is the email or the mobile that the customer entered
                    if(isset($post_email) || isset($post_phone))
                    {
                        //In the case of email we send a mail 
                        if(isset($post_email))
                        {
                            $email_template = $this->template_model->get_email_template(1);
    
                            if($email_template['status'] == "1") {
    
                                // build the validation URL
                                $encrypted_email = sha1($this->input->post('email', TRUE));
                                $validation_url  = base_url('user/validate') . "?e={$encrypted_email}&c={$validation_code}";
    
                                // variables to replace
                                $site_name = $this->settings->site_name;
                                $name_user = $this->input->post('first_name') . ' ' . $this->input->post('last_name');
    
                                $rawstring = $email_template['message'];
    
                                // what will we replace
                                $placeholders = array('[SITE_NAME]', '[CHECK_LINK]', '[NAME]');
    
                                $vals_1 = array($site_name, $validation_url, $name_user);
    
                                //replace
                                $str_1 = str_replace($placeholders, $vals_1, $rawstring);
    
                                $this -> email -> from($this->settings->site_email, $this->settings->site_name);
                                $this->email->to($this->input->post('email', TRUE));
                                //$this -> email -> to($user['email']);
                                $this -> email -> subject($email_template['title']);
    
                                $this -> email -> message($str_1);
    
                                $this->email->send();
                            }
                            
                            
                        }
                        
                        //in the case of number we send sms
                        if(isset($post_phone)){
                            
                            $sms_template = $this->template_model->get_sms_template(1);
                            
                            $token = rand(10000000, 99999999);
                            
                            if($sms_template['status'] == "1"){
                                
                            $rawstring = $sms_template['message'];
    
                            // what will we replace
                            $placeholders = array('[CODE]');
    
                            $vals_1 = array($token);
    
                            //replace
                            $str_1 = str_replace($placeholders, $vals_1, $rawstring);
                                
                            $result = $this->sms->send_sms($user['phone'], $str_1);
                            
                            if ($result == TRUE) {
                            
                                $this->session->set_flashdata('message', lang('users security sms_success'));
    
                            } else {
    
                                $this->session->set_flashdata('error', lang('users security sms_fail'));
    
                            }

                            }
                        }
                        
                    }
                        $this->session->language = $this->input->post('language');
                        $this->lang->load('users', $this->user['language']);
                        $this->session->set_flashdata('message', sprintf(lang('users msg register_success'), $this->input->post('first_name', TRUE)));
                    
                
                }
                    else
                    {
                        $this->session->set_flashdata('error', lang('users error register_failed'));
                        redirect($_SERVER['REQUEST_URI'], 'refresh');
                    }
                        
                } else {
                        
                    $this->session->set_flashdata('error', lang('users balanve info4'));
                    redirect($_SERVER['REQUEST_URI'], 'refresh');
                        
                    
                    }
                    
            } else {
                    
                $this->session->set_flashdata('error', lang('users error register_failed'));
                redirect($_SERVER['REQUEST_URI'], 'refresh');
                    
                }
        
        // redirect home and display message
        redirect('login');
    }

    // setup page header data
    $this->set_title(lang('core button create'));

    $data = $this->includes;

    // set content data
    $content_data = array(
        'cancel_url'        => base_url(),
        'user'              => NULL,
        'password_required' => TRUE
    );

    // load views
    $data['content'] = $this->load->view('user/profile_form', $content_data, TRUE);
    $this->load->view($this->template, $data);
}

what i'm basically Trina do is to verify either the client enter a phone number or the email, after checking that i want to send him if is chose email and send an sms if he choose phone (for this verification i have use twilio


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...