Welcome Guest, Not a member yet? Register   Sign In
Cannot modify header information - headers already sent by
#1

[eluser]whygod[/eluser]
hi guys,
Can you help me find the error of this controller codes,

The error message is,
Code:
A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at /home/siteremoved/public_html/admins9b/application/controllers/jaz.php:110)
Filename: core/Common.php
Line Number: 438

controller: jaz.php
Code:
<?php
if (! defined('BASEPATH')) exit('No direct script access allowed');

class Jaz extends MY_Controller
{
public $data = array(
                   '' => '',
    );

    function __construct()
    {
        parent::__construct();
        $this->load->helper('general');
        $this->load->model('users_model');
  $this->load->library('email');
     }

public function index()
{
  $this->load->view('jaz/login_form');
}

public function process_login()
{
  $this->form_validation->set_rules('username', 'Username', 'required');
  $this->form_validation->set_rules('password', 'Password', 'required');
  
  if ($this->form_validation->run() == FALSE)
  {
   //If validation has error display form again with error messages.  
   $this->index();
  }
  else
  {
   //If form validation has no error catch $username and $password
   //Call login method
   $username = $this->input->post('username', TRUE);
   $password = $this->input->post('password', TRUE);
  
   if($this->users_model->login($username, $password))
   {
    //If login successful.
    //display welcome and username.
    echo '<br><br>Welcome back '. $username;
    echo '<br>';
    echo anchor('jaz/logout', 'Logout', 'title="logout"');
   }
   else
   {
    //If login failed
    //Display error message and redirect back to login page.
    echo 'Ivalid username or password.';
   }
  }  
}

public function register_form()
{
  $this->load->view('jaz/register_form');
}

public function process_register()
{
  //capture data and validate form
  $this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[25]|is_unique[users.username]');
  $this->form_validation->set_rules('password', 'Password', 'required|matches[confirm_pass]');
  $this->form_validation->set_rules('confirm_pass', 'Password Confirmation', 'required');
  $this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[users.email]');

  $this->form_validation->set_rules('first_name', 'Firstname', 'required');
  $this->form_validation->set_rules('last_name', 'Lastname', 'required');
  $this->form_validation->set_rules('company', 'Company', 'required');
  $this->form_validation->set_rules('contact_number', 'Contact number', 'required');  
  $this->form_validation->set_rules('country', 'Country', 'required');
  $this->form_validation->set_rules('website', 'Website', 'required');
  
  if ($this->form_validation->run() == FALSE)
  {
   //If validation has error display form again with error messages.  
   $this->register_form();
  }
  else
  {
   //If validation has no errors capture data and insert into database
   $password = $this->input->post('password', TRUE);
   $password = sha1($password);

   $data1 = array(
       'group_id' => 2,
       'ip_address' => $_SERVER['REMOTE_ADDR'],
       'created_on' => date("mdy"),
       'last_login' => date("mdy"),
       'username' => $this->input->post('username', TRUE),
       'password' => $password,
       'email' => $this->input->post('email', TRUE),
      );
  
   $data2 = array(
       'userid' => 0,
       'first_name' => $this->input->post('first_name', TRUE),
       'last_name' => $this->input->post('last_name', TRUE),
       'company' => $this->input->post('company', TRUE),
       'contact_number' => $this->input->post('contact_number', TRUE),
       'country' => $this->input->post('country', TRUE),
       'website' => $this->input->post('website', TRUE),
      );

   //Insert into database
   $id = $this->users_model->save($data1, $data2);
   echo 'Data successfull saved.';
  
   //check for email validity first.
   if($this->jaz_lib->check_email_valid($data1['email'])) {
    //Email activation link.
    $this->jaz_lib->email_activation($data1['username'], $data1['email']);
    
   }
   else
   {
    echo 'Email activation is not sent, because email is not valid.';    
   }
  
  /*
   //email userinfo to user and send activation email.
   $from = 'From: admin@'. base_url() . '.com';
   $to = $email;
   $subject = 'Your account info at '. base_url();
   $body = 'username: ' .$username. ' password: ' .$password;
    
   if (mail ($to, $subject, $body, $from)) {
    //echo 'MAIL - OK';
   }else{
    echo 'MAIL - NOT OK';
    $this->home_page($message = '<br><br><br><br><br><br><br><br><br><b>Mail okay!</b>');
   }
  
   //display activation message
   $this->home_page($message = '<br><br><br><br><br><br><br><br><br><b>Please check your email to activate your account!</b>');
   */
  }
}

//activate a newly registed account.
public function activate_account($email)
{
  if($this->jaz_model->activate_account($email))
  {
   echo 'We have activated your account, you can now login.';
   echo '<br><br>';
   echo anchor('/jaz/', 'Click here to login', 'title="Click here"');
  }
  else
  {
   echo 'Activation failed!';
  }
}

public function logout()
{
  $this->jaz_lib->logout();
  
  $this->index();
}

}

I hope someone will help me.
Thanks in advanced.
#2

[eluser]CroNiX[/eluser]
Very common problem. Don't echo from a controller. It gets sent before CI's buffered output gets sent, so the headers aren't present before the output.

Views are the place for that (output). Loading a view dumps the view into CI's buffer. Then when your controller is done processing, CI outputs the buffer.
#3

[eluser]whygod[/eluser]
Thanks CroNIX.

Will try...




Theme © iAndrew 2016 - Forum software by © MyBB