简体   繁体   中英

PHP | Codeigniter Redirect | Cannot modify header information

I have a problem when i try to use redirect() function from CodeIgniter.

The error is (when i try to call addMed() function)

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> 
Line: 48
Function: redirect
File: /a_directory/index.php
Line: 315
Function: require_once

My File "CabUser.php"


<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class CabUser extends CI_Controller {

  function __construct() {
    parent::__construct();
    $this->load->database();
  }

  public function index() {
    if ($this->session->has_userdata('for_login')) {
        $userPrivelegies = $this->session->userdata['for_login']['privelegies'];
        if($userPrivelegies) {
            redirect('CabMed', 'refresh');
        }
        else {
            $this->load->model('Cab_model');
              $this->Cab_model->getUserData();
        }
    }
    else {
        redirect('login');
    }
  }

  function logout() {
    if ($this->session->has_userdata('for_login')) {
      $this->session->sess_destroy('for_login');
    }
    redirect('home', 'refresh');
  }

  function addMed() {
    $data['numePrenumeMedic'] = $this->input->post('medic');
    $username = $this->session->userdata['for_login']['username'];
    $this->db->where('username', $username);
    $query = $this->db->get('Users');
    $ID = $query->row()->ID;

    $this->db->where('ID', $ID);
    $this->db->update('UserData', $data);

    echo "<script>
            alert('Succes!!!');
          </script>";

    redirect('CabUser', 'refresh'); //here's the problem
  }

  function upload() {
    if ($this->input->post('update')) {
      $data['numePrenume'] = $this->input->post('name');  
      $data['numePrenume'] = $this->input->post('name');
      $data['facebook'] = $this->input->post('facebook');
      $data['ok'] = $this->input->post('ok');
      $data['instagram'] = $this->input->post('instagram');
      $data['datanasterii'] = $this->input->post('year') . '-' . $this->input->post('month') .'-' . $this->input->post('day');
      $data['telefon'] = $this->input->post('telefon');
      $data['adresa'] = $this->input->post('address');
      $data['sex'] = $this->input->post('sex');

      $sess = $this->session->userdata('for_login', 'username');

      $this->db->where('username',$sess['username']);
      $query = $this->db->get('Users');

      $ID = $query->row()->ID;

      $this->db->where('ID', $ID);
      $this->db->update('Users', $data);

      $this->db->where('ID', $ID);
      $this->db->set('numePrenumePacient', $data['numePrenume']);
      $this->db->update('Pacienti');    

      echo "<script>
            alert('Succes!!!');
          </script>";
      redirect('CabUser', 'refresh');
    }
    else {
      echo "<script>
            alert('Error to update. \n Try later please...);
          </script>";
      redirect('CabUser', 'refresh');
    }


  }

}
?>

Your solution is,

Remove echo "<script> alert('Succes!!!'); </script>"; code from everywhere where you are using redirect() function. Because redirect function uses header function of php and before any header, you should not echo or send anything to browser.

And that is what the error says.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM