0
votes

in my code their is an error what is solution for this

A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /home/websites/public_html/application/controllers/user.php:64)

Filename: helpers/url_helper.php

Line Number: 546

this is my code

function register()
{
                     $email=$_POST['email'];
                    $pass=$_POST['pass'];
                     $cpass=$_POST['cpass'];
            $valid = array(
                                    array(
                                        'field'   => 'email',
                                        'label'   => 'Username(Email)',
                                        'rules' => 'trim|required|valid_email|is_unique[register.email]'
                                    ), 
                                    array(
                                        'field'   => 'pass',
                                        'label'   => 'Password',
                                        'rules'   => 'trim|required||matches[cpass]|min_length[8]|max_length[30]|'
                                    ),
                                    array(
                                        'field'   => 'cpass',
                                        'label'   => 'Confirm Password',
                                        'rules'   => 'trim|required'
                                    )
                            );
                $this->form_validation->set_rules($valid); 
        $this->form_validation->set_error_delimiters('<div class="error_txt">', '</div>');
        if ($this->form_validation->run() == FALSE)
        {
            echo "error";
            redirect('/home/'); 
        }
        else
        {

            $this->load->model('User_model');       
            $rs=$this->User_model->register($_POST);

            redirect('/home/'); 
        }
}
3
You should select an answer as the best answer if you found one helpful. - Naveed Hasan

3 Answers

5
votes

Its probably happening here:

echo "error";

Instead try to return the error to the caller and then display the error from the view.

0
votes

Do not use redirect in ajax calls. You can achieve your goal with javascript redirect. Try this snippet:

if ($this->form_validation->run() == FALSE)
{
  echo "error";
}
else
{
  $this->load->model('User_model');       
  $rs=$this->User_model->register($_POST);
}

In JAVASCRIPT:

window.location = '<?php echo site_url('home'); ?>';
0
votes

redirect actually sends a Location header to the browser.

Your output has already started. The server has to send the headers before any output is started. You are doing an echo before redirect. So its throwing an error as it cant send headers after output has started.

remove that echo and it should work for you.