user1346624
user1346624

Reputation: 1

Codeigniter :A PHP Error was encountered

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/'); 
        }
}

Upvotes: 0

Views: 8918

Answers (3)

Broncha
Broncha

Reputation: 3794

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.

Upvotes: 0

Code Prank
Code Prank

Reputation: 4250

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'); ?>';

Upvotes: 0

Lowkase
Lowkase

Reputation: 5699

Its probably happening here:

echo "error";

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

Upvotes: 5

Related Questions