我如何在CodeIgniter 3中正确重定向?

问题描述 投票:0回答:2

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

Controller: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
2个回答
1
投票

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

谢谢


1
投票

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

$config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'].'/tutorial/codeigniter/';
© www.soinside.com 2019 - 2024. All rights reserved.