Codeigniter中的流错误

时间:2012-06-23 13:25:27

标签: php codeigniter flow

当它不应该出现在我的codeigniter脚本中时出现错误。 我假设它与我的代码流程有关,但我无法理解它。

以下是页面:

http://77.96.119.180/beer/user/activate/

您可以看到底部显示的错误,不应出现

  

“该用户名不存在。”

这是我的CodeIgniter类代码:

public function activate($code = '', $username = '')
    {
        $go = 0;
        $form = '';
        // This function lets a user activate their account with the code or link they recieved in an email
        if($code == '' || $username == '' || isset($_POST[''])){

            $this->load->library('form_validation');
            $this->load->helper('form');

            $this->form_validation->set_rules('code', 'Activation Code', 'trim|required|xss_clean|integer');
            $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');

            if ($this->form_validation->run() == FALSE){
                // No code, so display a box for them to enter it manually
                $form .= validation_errors();
                $form .= form_open_multipart('user/activate', array('class' => 'formee'));

                $form .= form_label('Enter your activation code below and click \'Activate\' to start using your account. If you need a hand please contact live chat.', 'activation_code');
                $form .= form_input(array('name' => 'code'));

                $form .= form_label('And enter your username', 'username');
                $form .= form_input(array('name' => 'username'));

                $data = array(
                    'name'        => 'submit',
                    'value'       => 'Activate',
                    'class'       => 'right',
                    'style'       => 'margin-top:10px;',
                );

                $form .= form_submit($data);
                $form .= form_close();
            }else{
                $go = 1;
                // Put POST variables into variables
                $code = $this->input->post('code');
                $username = $this->input->post('username');
            }
        }else{
            // Code recieved through the GET or POST variable XSS clean it and activate the account
            $go = 1;

            // Put GET variables into variables
            $code = $this->uri->segment(3);
            $username = $this->uri->segment(4);
        }

        if($go = 1){
            // Activate!

            // Check if user exists
            $query = $this->db->get_where('users', array('username' => $username, 'confirmation' => $code), 1);
            if ($query->num_rows() > 0){
                // Username exsists, activate the account
                $data = array(
                   'is_validated' => 1
                );

                $this->db->where('username', $username);
                $this->db->update('users', $data);

                $form .= '<div class="formee-msg-success">Acount activated, <a href="#">click here</a> to login.</div>'; 

            }else{
                // Username doesn't exsist or code doesn't match, find out which
                $form .= '<div class="formee-msg-error">That username doesn\'t exsist.</div>'; 
            }
        }


        $data = array(
            'title' => $this->lang->line('activate_title'),
            'links' => $this->gen_login->generate_links(),
            'content' => $form
        );
        $this->parser->parse('beer_template', $data);
    }

2 个答案:

答案 0 :(得分:0)

if if statment:

if ($this->form_validation->run() == FALSE)

在else语句中删除$ go = 1.

else
{
   // Put POST variables into variables
   $code = $this->input->post('code');
   username = $this->input->post('username');
 }

当form_validation-&gt; run()= TRUE且激活码或用户名为空时,此代码块将始终执行。如果没有激活码或用户名,您不希望尝试激活。

此外,下面的代码行没有做任何事情。您正在引用键[''],它将始终返回相同的结果。

isset($_POST[''])

从我读过的文档中你需要输入$ _POST的密钥。恩。 $ _ POST [ '代码']。你可以尝试isset($ _ POST),但我不是100%肯定这也可以。您可以在此处详细了解:http://us.php.net/isset

编辑: 正如Mischa在评论中指出的那样,你在if语句中指定了$ go = 1。

答案 1 :(得分:0)

所以在盯着这一段时间之后,这是我的错,但我很困惑,没有人把它拿起来!

问题在于:

if($go = 1){

应该是

if($go == 1){

解决了这个问题。

但由于你的建议,我现在正在重写它。

  

编辑:刚刚注意到Mischa对此发表了评论!