Codeigniter自己的库不会在首次加载页面时读取会话值

时间:2013-12-11 12:46:06

标签: javascript php ajax codeigniter session-variables

我正在尝试使用ajax将客户端javascript值分配给php会话。

以下是客户端代码,

<script type="text/javascript">
     var timeZone = 'Asia/Kolkata';
        $.ajax({
        url: '<?php echo base_url();?>index.php/schedule/setLocalTimeZone',
        type: 'POST',
            data: {
            timeZone: timeZone,
            },
        cache: true,
        success: function(response) {   
            //alert(response)
            }       
        }); 

      });
</script>

以下是我的控制器代码,

function setLocalTimeZone(){
    $tz = $_REQUEST['timeZone'];

    $this->session->set_userdata('TIMEZONE',$tz);


}

这里在控制器中我将时区值设置为php会话,然后我尝试在我自己的代码点火器库中使用此会话值

类TimeZone {

function getTimeZone(){

    $CI =& get_instance();
    $CI->load->library('session');
//here i'm trying to get "TIMEZONE" value from session but i couldnt get at first time when irefresh the page i can get the TIMEZONE value from session 
       $user_tz = $CI->session->userdata('TIMEZONE');

    return $user_tz;
    }

}

然后我在视图页面中调用这个库函数,所以当我只刷新它的给定值时,第一次页面加载时它没有给出任何值。

echo $Timezone = $this->timezone->getTimeZone();

所以我不知道问题出在哪里。是否codeigniter外部库ll没有读取我们通过ajax调用第一次设置的会话值?

提前谢谢你, Dhinesh.B

1 个答案:

答案 0 :(得分:0)

不是一个完美的解决方案,但在第一次加载时尝试重新加载一次页面。 这可能很有用。

使用javascript重新加载页面一次。

    <script type='text/javascript'>
    (function()
    {
      if( window.localStorage )
      {
        if( !localStorage.getItem( 'firstLoad' ) )
        {
          localStorage[ 'firstLoad' ] = true;
          window.location.reload();
        }  
        else
          localStorage.removeItem( 'firstLoad' );
      }
    })();

    </script>