【问题标题】:How do i redirect properly in CodeIgniter 3?如何在 CodeIgniter 3 中正确重定向?
【发布时间】:2020-09-08 01:04:23
【问题描述】:

我是 codeigniter 的新手,我正在开发一个带有会话的基本身份验证系统。我正在使用版本3.1.11

请注意我正在我的本地主机服务器上开发。

项目网址: http://localhost/tutorial/codeigniter

登录网址: http://localhost/tutorial/codeigniter/login/index.php/auth/login

用户个人资料网址: http://localhost/tutorial/codeigniter/login/index.php/user/profile


另外,我的登录代码有效。


控制器:Auth.php

public function __construct()
{
  parent::__construct();
  $this->load->database();
  $this->load->helper('url');
}

public function login()
{
  $this->form_validation->set_rules("username", "Username", "required");
  $this->form_validation->set_rules("password", "Password", "required|min_length[7]");

  if($this->form_validation->run() == TRUE) {
    ....
    if ($row) {
      $this->session->set_flashdata("success", "Success!!!"); 

      $_SESSION["user_logged"] = TRUE;
      $_SESSION["username"] = $user->username;

      // redirect to profile page
      redirect('user/profile', 'refresh');
    } else {
      $data['error']="<div class='alert alert-danger'>Invalid login details</div>";
    }
  }

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

控制器:User.php

class User extends CI_Controller
{
  public function profile()
  {
    echo "Profile Page";
  }
}

问题:我想在用户成功登录后将用户重定向到http://localhost/tutorial/codeigniter/login/index.php/user/profile。我用于重定向的代码是redirect('user/profile', 'refresh');

上面的重定向代码的问题是它被重定向到http://localhost/tutorial/login/index.php/user/profile -- 不包括/codeigniter

您知道如何正确重定向到http://localhost/tutorial/codeigniter/login/index.php/user/profile 吗?任何帮助是极大的赞赏。谢谢

【问题讨论】:

    标签: javascript php html codeigniter


    【解决方案1】:

    请将application/config/config.php下的base_url设置为http://localhost/tutorial/codeigniter。 这将帮助您修复错误。

    谢谢

    【讨论】:

      【解决方案2】:

      你应该在application/config下的config.php中设置。打开 config.php 并放在下面一行。

      $config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'].'/tutorial/codeigniter/';
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-12-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多