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 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算