CodeIgniter:cookies,会话,重定向过多?

时间:2016-02-03 11:10:31

标签: php codeigniter session cookies

我使用CodeIgniter 2.2创建了一个网站,但我遇到了会话和cookie的一些问题。

使用此配置在localhost中的一切正常(在application / config /中):

$config['base_url'] = 'http://localhost:8888/project/';
...
$config['cookie_prefix']    = "";
$config['cookie_domain']    = "";
$config['cookie_path']      = "/";
$config['cookie_secure']    = FALSE;

但是当我在我的服务器上传网站时,当我的CI网站(domain.com)进入子文件夹时,我或者 这些配置 ERR_TOO_MANY_REDIRECTS /测试/):

$config['cookie_domain']    = "";
$config['cookie_domain']    = ".domain.com";

出现此类错误后,访问domain.com/other也无法正常工作。我必须退出网络浏览器才能回来。

所以我尝试了另一个我在线程上找到的配置,我找不到了,现在这是登录过程(会话)不起作用。

$config['cookie_domain'] = str_replace("http:/","",str_replace("https://","", $config['base_url']));

用于登录的方法如下:

查看/模板/ header.php文件

<form method="post" action="<?php echo site_url('verifylogin/');?>">

    <input type="text" size="20" name="username" placeholder="username"/><br/>
    <input type="password" size="20" name="password" placeholder="password"/><br/>
    <input type="submit" value="Log in"/>
    <input type="hidden" name="current" value="http://<?php echo $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>" /><br/>

</form>

控制器/ verifylogin.php

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

class VerifyLogin extends CI_Controller {

  function __construct()
  {
    parent::__construct();
    $this->load->model('users_model','',TRUE);
    // does not seem to work $this->load->driver('session');
  }

  function index()
  {
    $data['title'] = 'Log in';

    //This method will have the credentials validation
    $this->load->library('form_validation');

    $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');

    $this->form_validation->run();
    redirect($this->input->post('current'), 'refresh');

    return $data;
  }

  function check_database($password)
  {
    //Field validation succeeded.  Validate against database
    $username = $this->input->post('username');

    //query the database
    $result = $this->users_model->login($username, $password);

    if($result)
    {
      $sess_array = array();
      foreach($result as $row)
      {
        $sess_array = array(
          'id' => $row->id,
          'username' => $row->name
        );
        $this->session->set_userdata('logged_in', $sess_array);

        $set = $this->users_model->get_settings($row->id);
        $this->session->set_userdata('settings', $set);
      }
      return TRUE;
    }
    else
    {
      $this->form_validation->set_message('check_database', 'Invalid username or password');
      return FALSE;
    }
  }
}
?>

1 个答案:

答案 0 :(得分:0)

尝试清除浏览历史记录,Cookie等。