doushu5805 2013-05-26 13:55
浏览 32
已采纳

codeigniter会话仅适用于一个页面而不适用于链接到该页面的其他页面[关闭]

I have login with username and password to the page it will login successfull and that page has link for other pages but i am setting username to session variable but it will say session is not availabe.

    verifylogin.php

    <?php 

class VerifyLogin extends CI_Controller {



 function __construct()
 {
   parent::__construct();
   $this->load->model('login/user','',TRUE);
 }

 function index()
 {
   //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');

   if($this->form_validation->run() == FALSE)
   {
     //Field validation failed.&nbsp; User redirected to login page

     $this->load->view('index');

   }
   else
   {
      //$this->load->view('home_view');
     //Go to private area
     $password = $this->input->post('password');
     if($this->check_data($password))
     {
     redirect('login_f/home', 'refresh');
     }
     else
     {
         $data['username'] = $this->input->post('username');
         $data['rannum'] = $this->send_confirm_email();      
         $this->load->view('/login/verify_code2',$data);
     }
   }

 }

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


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


   if($result)
   {


     if($value == 0)
     {  
         $sess_array = array();
         foreach($result as $row)
         {
           $sess_array = array(
             //'id' => $row->id,
             'username' => $row->username
           );
           $this->session->set_userdata('logged_in', $sess_array);
         }
         return TRUE;
     }


   }
   else
   {
     $this->form_validation->set_message('check_database', 'Invalid username or password');
     return false;
   }
 }

    function check()
    {
        $this->load->view('index');
    }





}
?>

home.php


<?php 
session_start(); //we need to call PHP's session object to access it through CI
class Home extends CI_Controller {

 function __construct()
 {
   parent::__construct();
 }

 function index()
 {
   if($this->session->userdata('logged_in'))
   {
     $session_data = $this->session->userdata('logged_in');
     $data['username'] = $session_data['username'];
     $this->load->view('login/home_view', $data);
   }
   else
   {
     //If no session, redirect to login page


     redirect('login_f/login', 'refresh');


   }
 }

 function logout()
 {
   $this->session->unset_userdata('logged_in');
   session_destroy();
   redirect('login_f/home', 'refresh');
 }

}

?>
  • 写回答

1条回答 默认 最新

  • dsgrgaz321973284 2013-05-26 13:58
    关注

    Any chance the user is redirected to a different server? The session is available only on the same domain. from the PHP manual:

    A visitor accessing your web site is assigned a unique id, the so-called session id. This is either stored in a cookie on the user side or is propagated in the URL.

    The session support allows you to store data between requests in the $_SESSION superglobal array. When a visitor accesses your site, PHP will check automatically (if session.auto_start is set to 1) or on your request (explicitly through session_start() or implicitly through session_register()) whether a specific session id has been sent with the request. If this is the case, the prior saved environment is recreated.

    I've used this manual, maybe it will help you find your error.

    If everything looks ok, one more thing to check is whether the session files are being created. There might be a permissions problem. to check this, create a page:

    <?php phpinfo(); ?>
    

    make sure that the session.save_path directory exists and is writable, and that session.auto_start is set to on if you want to start the session automatically. you can also start it manually by running the command session_start();

    to set it on, in the php.ini file (usually located in /etc/php.ini):

    session.auto_start = 1
    

    to change the sessions folder, change session.save_path:

    session.save_path = "/path/to/session_files"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?