Search code examples
codeignitercodeigniter-2

How to redirect to another page in codeigniter from one page?


I have written a code in the controller which is given below. This code is for login page. In this code, the if statement notification is properly working but the else part is not working. It is not redirect to the url given in the redirect() instead it is showing a blank page. Can anyone tell y it is like that and correct it for me ? I have used header() function but also it is not working.I have placed all the code inside the 'kw' folder. This code is properly working in the localhost but when uploaded the same code to the live server, its not working.Is it due to version of the CI ?

<?php
     if (!defined('BASEPATH'))
     exit('No direct script access allowed');
     class Login extends CI_Controller {
          public function __construct() {
                parent::__construct();
                $this->load->library('session');
                $this->load->library('validation');

                $this->load->model('studentsmodel');
     }
     public function index() {
            if (isset($_POST['submit'])) 
            {
               $post_data = array('stud_cin' => $this->input->post('stud_cin'),'stud_password' => $this->input->post('stud_password'), 
                            );


               $this->validation->set_data($post_data);
               $this->validation->set_rules('stud_cin','Cin','required|trim');
               $this->validation->set_rules('stud_password','Password','required|trim');
               if ($this->validation->run() === FALSE) 
               {

               } 
               else 
               {
                $this->load->model("studentsmodel");
                $this->load->helper('url');
                $result = $this->studentsmodel->loginCheck($post_data); 
                if (!$result){
                    $this->notifications->notify('Wrong username/password; Login Failed','error');

                }
                else{

                         $this->session->set_userdata('student_id', $result['id']);
                         $this->session->set_userdata('stud_cin', $result['stud_cin']);
                         $this->session->set_userdata('stud_photopath', $result['stud_photopath']);


                        redirect('student/profile/edit/id/'.$this->session->userdata('student_id'),'refresh');

                         //header('location:http://www.website.com/kw/application/controller/student/profile/edit/id/$this->session->userdata("student_id")');
                }

        }
    }        $this->load->view("login");
}
}

profile controller edit function

 public function index()
 {

    $student_id=$this->session->userdata('student_id');
     $data['profile_data'] =$this->studentsmodel->get_Cinprofile($student_id);

     $this->load->view("profileDetails.php", $data);
}


public function edit() 
{   
$uri = $this->uri->uri_to_assoc(4);
$student_id=$uri['id'];
if(isset($_POST['btn_submit']))
{

   //echo "<pre>";print_r($_FILES);exit;

  $img_val      = $this->studentsmodel->getStudent_photo($student_id);
  $photo_val    = $img_val['stud_photopath'];
  $photo_unlink = "";


  if ($_FILES['stud_photopath']['name'] != "") 
  {
      /*echo "in";exit;*/
     $photo_chk    = explode("photo/", $photo_val);
     $photo_unlink = $photo_chk[1];
     $photo_path = "";
     $flag       = "";
     $f_type_chk = $_FILES['stud_photopath']['type'];

     if ($f_type_chk != "image/gif" && $f_type_chk != "image/jpg" && $f_type_chk != "image/jpeg"
          && $f_type_chk != "image/bmp" && $f_type_chk != "image/png" && $f_type_chk != "") 
     {
          $flag = "Select allowed file type and size for photo";
     }
     if ($_FILES['stud_photopath']['size'] >= 5242880) {  $flag = "Select allowed file type and size for photo";  }

      $target_path = getcwd() . "/public/photo/";
      $db_path     = "photo/";

     if ($_FILES['stud_photopath']['name'] != '')
     {
          $file_name     = $_FILES["stud_photopath"]["name"];
          $file_size     = $_FILES["stud_photopath"]["size"] / 1024;
          $file_type     = $_FILES["stud_photopath"]["type"];
          $file_tmp_name = $_FILES["stud_photopath"]["tmp_name"];
          $random        = rand(111, 999);
          $new_file_name = $random . $file_name;
          $newfile_name=$cin."_". $file_name;

          $upload_path   = $target_path . $newfile_name;;

         if (move_uploaded_file($file_tmp_name, $upload_path))  { $photo_path = addslashes($db_path . $newfile_name);  } /*end if*/

        else { $this->notifications->notify('Photo cannot upload', 'error');  }/*end else var_dump($this->validation->show_errors());*/
      }/*end if $_FILES['photo']['name']*/

     } /*END OF  if ($_FILES['photo']['name'] != "") */

  else  {   $photo_path = $photo_val; } /*END OF   ELSE ($_FILES['photo']['name'] != "") */



         $this->session->unset_userdata('stud_photopath');
         $this->session->set_userdata('stud_photopath', $photo_path);

         $data['photo_unlink'] = $photo_unlink;
        /* echo $dob_dd = $this->input->post('dob_dd');exit;
         $dob_mm =  $this->input->post('dob_mm');
         $dob_yy = $this->input->post('dob_yy');
         $dob       = $dob_dd . "-" . $dob_mm . "-" . $dob_yy;*/
         $stud_age_dob  =$this->input->post('stud_age_dob');
         $timestamp = strtotime($stud_age_dob);
         $dob      = date('Y-m-d', $timestamp);


         $validation_data  = array(
                              'stud_name'   => $this->input->post('stud_name'),
                              'stud_gender'    => $this->input->post('stud_gender'),
                              'stud_age_dob'  =>$this->input->post('stud_age_dob'),
                              'stud_mobile' => $this->input->post('stud_mobile'),
                              'stud_email' => $this->input->post('stud_email'),
                           );  
                      //echo "<pre>";print_r($validation_data); exit;          
                        $this->validation->set_data($validation_data);
                        $this->validation->set_rules('stud_name', 'Student Name', 'trim|alpha_space|required');
                        $this->validation->set_rules('stud_gender', 'Gender', 'required');
                        $this->validation->set_rules('stud_age_dob', 'DOB', 'required');

                        $this->validation->set_rules('stud_mobile', 'Mobile number', 'numeric|required');
                        $this->validation->set_rules('stud_email', 'Email Id', 'trim|required|valid_email|xss_clean');


                        if ($this->validation->run() === FALSE) 
                        { /* var_dump($this->validation->show_errors()); */ $this->notifications->notify('Please make all entries', 'error');} 
                        else 
                        {

                            $updation_data=array(

                                                  'stud_name'   => $this->input->post('stud_name'),
                                                  'stud_gender'   => $this->input->post('stud_gender'),
                                                  'stud_age_dob'    => $this->input->post('stud_age_dob'),
                                                  'stud_gaurdian'  =>$this->input->post('stud_gaurdian'),
                                                  'stud_mother'  =>$this->input->post('stud_mother'),
                                                  'stud_mobile'        => $this->input->post('stud_mobile'),
                                                  'stud_email'  => $this->input->post('stud_email'),
                                                  'stud_tel'  => $this->input->post('stud_tel'),
                                                  'stud_guardian_address' => $this->input->post('stud_guardian_address'),

                                                  'stud_pin' => $this->input->post('stud_pin'),
                                                  'stud_photopath' => $photo_path,
                                                  'stud_age_dob'   => $dob
                                                 );

                            /*echo "<pre>";print_r($updation_data); exit;   */           
                            $update_status=$this->studentsmodel->update_profile($updation_data, $student_id);

                            if($update_status==1)
                              {
                                  //$this->session->set_userdata('profile_status', 'Yes');
                                  /*$this->session->userdata('profile_status');*/
                                  redirect('student/profile/index/', 'refresh');
                              }
                              else
                              {
                                  $this->notifications->notify('profile updted failed', 'error');


                              }
                              $data['profile_data']=$_POST;




                        }
      $data['profile_data']=$_POST;
  }


   $data['profile_data']=$this->studentsmodel->get_Cinprofile($student_id);
   $this->load->view("profile_edit.php",$data);
}

Solution

    1. Check if you already load this helper

      $this->load->helper('url');

    2. This is the information about the redirect function

      redirect($uri = '', $method = 'auto', $code = NULL);

    Parameters:

    1. $uri (string) URI string $method (string)
    2. Redirect method (‘auto’, ‘location’ or ‘refresh’)
    3. $code (string) HTTP Response code (usually 302 or 303)

    Note:

    Don't forget add in the config file the value for base_uri

    https://www.codeigniter.com/user_guide/helpers/url_helper.html https://www.codeigniter.com/userguide3/libraries/config.html