如何从一个页面重定向到codeigniter中的另一个页面?

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

我已经在下面给出的控制器中编写了代码。此代码用于登录页面。在此代码中,if 语句通知正常工作,但 else 部分不工作。它不会重定向到 redirect() 中给出的 url,而是显示空白页。任何人都可以告诉你它是那样的并为我纠正它吗?我使用了 header() 函数,但也不是 working.I 将所有代码放在 'kw' 文件夹中。此代码在本地主机中正常工作,但是当将相同的代码上传到实时服务器时,由于 CI 的版本,它不是 working.I 吗?

<?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");
}
}

配置文件控制器编辑功能

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

从您的 redirect()

中删除 /id/
redirect('student/profile/edit/'.$this->session->userdata('student_id'));
  1. 检查你是否已经加载了这个助手

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

  2. 这是关于重定向功能的信息

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

参数:

  1. $uri(字符串)URI 字符串 $method(字符串)
  2. 重定向方法('自动', “位置”或“刷新”)
  3. $code(字符串)HTTP 响应代码(通常为 302 或 303)

注:

不要忘记在配置文件中添加 base_uri

的值

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