title: PHP | Codeigniter Redirect | Cannot modify header information
body: <p>I have a problem when i try to use redirect() function from CodeIgniter. 
  <br/><br/>
  The error is (when i try to call <code>addMed()</code> function)</p>
  
  <pre><code>A PHP Error was encountered
  Severity: Warning
  Message: Cannot modify header information - headers already sent by (output started at /a_directory/CabUser.php:46)
  Filename: helpers/url_helper.php
  Line Number: 561
  Backtrace:
  File: /a_directory/CabUser.php&gt; 
  Line: 48
  Function: redirect
  File: /a_directory/index.php
  Line: 315
  Function: require_once
  </code></pre>
  
  <p><strong>My File "CabUser.php"</strong></p>
  
  <p><br/></p>
  
  <pre><code>&lt;?php
  defined('BASEPATH') OR exit('No direct script access allowed');
  
  class CabUser extends CI_Controller {
  
    function __construct() {
      parent::__construct();
      $this-&gt;load-&gt;database();
    }
  
    public function index() {
      if ($this-&gt;session-&gt;has_userdata('for_login')) {
          $userPrivelegies = $this-&gt;session-&gt;userdata['for_login']['privelegies'];
          if($userPrivelegies) {
              redirect('CabMed', 'refresh');
          }
          else {
              $this-&gt;load-&gt;model('Cab_model');
                $this-&gt;Cab_model-&gt;getUserData();
          }
      }
      else {
          redirect('login');
      }
    }
  
    function logout() {
      if ($this-&gt;session-&gt;has_userdata('for_login')) {
        $this-&gt;session-&gt;sess_destroy('for_login');
      }
      redirect('home', 'refresh');
    }
  
    function addMed() {
      $data['numePrenumeMedic'] = $this-&gt;input-&gt;post('medic');
      $username = $this-&gt;session-&gt;userdata['for_login']['username'];
      $this-&gt;db-&gt;where('username', $username);
      $query = $this-&gt;db-&gt;get('Users');
      $ID = $query-&gt;row()-&gt;ID;
  
      $this-&gt;db-&gt;where('ID', $ID);
      $this-&gt;db-&gt;update('UserData', $data);
  
      echo "&lt;script&gt;
              alert('Succes!!!');
            &lt;/script&gt;";
  
      redirect('CabUser', 'refresh'); //here's the problem
    }
  
    function upload() {
      if ($this-&gt;input-&gt;post('update')) {
        $data['numePrenume'] = $this-&gt;input-&gt;post('name');  
        $data['numePrenume'] = $this-&gt;input-&gt;post('name');
        $data['facebook'] = $this-&gt;input-&gt;post('facebook');
        $data['ok'] = $this-&gt;input-&gt;post('ok');
        $data['instagram'] = $this-&gt;input-&gt;post('instagram');
        $data['datanasterii'] = $this-&gt;input-&gt;post('year') . '-' . $this-&gt;input-&gt;post('month') .'-' . $this-&gt;input-&gt;post('day');
        $data['telefon'] = $this-&gt;input-&gt;post('telefon');
        $data['adresa'] = $this-&gt;input-&gt;post('address');
        $data['sex'] = $this-&gt;input-&gt;post('sex');
  
        $sess = $this-&gt;session-&gt;userdata('for_login', 'username');
  
        $this-&gt;db-&gt;where('username',$sess['username']);
        $query = $this-&gt;db-&gt;get('Users');
  
        $ID = $query-&gt;row()-&gt;ID;
  
        $this-&gt;db-&gt;where('ID', $ID);
        $this-&gt;db-&gt;update('Users', $data);
  
        $this-&gt;db-&gt;where('ID', $ID);
        $this-&gt;db-&gt;set('numePrenumePacient', $data['numePrenume']);
        $this-&gt;db-&gt;update('Pacienti');    
  
        echo "&lt;script&gt;
              alert('Succes!!!');
            &lt;/script&gt;";
        redirect('CabUser', 'refresh');
      }
      else {
        echo "&lt;script&gt;
              alert('Error to update. \n Try later please...);
            &lt;/script&gt;";
        redirect('CabUser', 'refresh');
      }
  
  
    }
  
  }
  ?&gt;
  </code></pre>