2016-12-15 8 views
0

In index.php jQueryでセッション変数を設定しましたが、このページではトークンIDを取得していますが、home.phpのような別のページに表示したいとします。だから私は、あなたが他のすべてのファイル(自宅に含まれるべきファイル(header.phpの)であなたのセッション変数を作成することができhome.phpページにセッション変数を作成して別のページを表示する方法

var val = $.session.get('Token'); 
    alert(); //**getting here undefined**.how can solve this issue please tell me any one 

(function($){ 
 

 
    $.session = { 
 

 
     _id: null, 
 

 
     _cookieCache: undefined, 
 

 
     _init: function() 
 
     { 
 
      if (!window.name) { 
 
       window.name = Math.random(); 
 
      } 
 
      this._id = window.name; 
 
      this._initCache(); 
 

 
      // See if we've changed protcols 
 

 
      var matches = (new RegExp(this._generatePrefix() + "=([^;]+);")).exec(document.cookie); 
 
      if (matches && document.location.protocol !== matches[1]) { 
 
       this._clearSession(); 
 
       for (var key in this._cookieCache) { 
 
        try { 
 
        window.sessionStorage.setItem(key, this._cookieCache[key]); 
 
        } catch (e) {}; 
 
       } 
 
      } 
 

 
      document.cookie = this._generatePrefix() + "=" + document.location.protocol + ';path=/;expires=' + (new Date((new Date).getTime() + 120000)).toUTCString(); 
 

 
     }, 
 

 
     _generatePrefix: function() 
 
     { 
 
      return '__session:' + this._id + ':'; 
 
     }, 
 

 
     _initCache: function() 
 
     { 
 
      var cookies = document.cookie.split(';'); 
 
      this._cookieCache = {}; 
 
      for (var i in cookies) { 
 
       var kv = cookies[i].split('='); 
 
       if ((new RegExp(this._generatePrefix() + '.+')).test(kv[0]) && kv[1]) { 
 
        this._cookieCache[kv[0].split(':', 3)[2]] = kv[1]; 
 
       } 
 
      } 
 
     }, 
 

 
     _setFallback: function(key, value, onceOnly) 
 
     { 
 
      var cookie = this._generatePrefix() + key + "=" + value + "; path=/"; 
 
      if (onceOnly) { 
 
       cookie += "; expires=" + (new Date(Date.now() + 120000)).toUTCString(); 
 
      } 
 
      document.cookie = cookie; 
 
      this._cookieCache[key] = value; 
 
      return this; 
 
     }, 
 

 
     _getFallback: function(key) 
 
     { 
 
      if (!this._cookieCache) { 
 
       this._initCache(); 
 
      } 
 
      return this._cookieCache[key]; 
 
     }, 
 

 
     _clearFallback: function() 
 
     { 
 
      for (var i in this._cookieCache) { 
 
       document.cookie = this._generatePrefix() + i + '=; path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT;'; 
 
      } 
 
      this._cookieCache = {}; 
 
     }, 
 

 
     _deleteFallback: function(key) 
 
     { 
 
      document.cookie = this._generatePrefix() + key + '=; path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT;'; 
 
      delete this._cookieCache[key]; 
 
     }, 
 

 
     get: function(key) 
 
     { 
 
      return window.sessionStorage.getItem(key) || this._getFallback(key); 
 
     }, 
 

 
     set: function(key, value, onceOnly) 
 
     { 
 
      try { 
 
       window.sessionStorage.setItem(key, value); 
 
      } catch (e) {} 
 
      this._setFallback(key, value, onceOnly || false); 
 
      return this; 
 
     }, 
 
     
 
     'delete': function(key){ 
 
      return this.remove(key); 
 
     }, 
 

 
     remove: function(key) 
 
     { 
 
      try { 
 
      window.sessionStorage.removeItem(key); 
 
      } catch (e) {}; 
 
      this._deleteFallback(key); 
 
      return this; 
 
     }, 
 

 
     _clearSession: function() 
 
     { 
 
      try { 
 
       window.sessionStorage.clear(); 
 
      } catch (e) { 
 
       for (var i in window.sessionStorage) { 
 
        window.sessionStorage.removeItem(i); 
 
       } 
 
      } 
 
     }, 
 

 
     clear: function() 
 
     { 
 
      this._clearSession(); 
 
      this._clearFallback(); 
 
      return this; 
 
     } 
 

 
    }; 
 

 
    $.session._init(); 
 

 
})(jQuery); 
 
    
 
// Here i am set session variable 
 

 
$.session.set('Token',"1") ; 
 
var get_session_name =$.session.get('Token'); 
 
alert(get_session_name);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

+0

?例えば、https://github.com/js-cookie/js-cookie –

+0

標準のセッションクッキーを使って情報を保存したり取得したりするのはなぜですか? –

+0

文言、大文字 – Mikkel

答えて

0

をしています。この場合、PHPは)

ので、この

header.phpのようにセッションを作成

<?php 
    session_start(); 
    if (isset($_SESSION['username'])) { 
     // session already exists 
     echo "User ID:", $_SESSION['id'], "<br />" 
    } else { 
     // New PHP Session 

     $_SESSION['username'] = "loginprocess"; 
     $_SESSION['id'] = 444; 
    } 
?> 

可能home.php

<?php require "header.php"; ?> 
<!doctype html> 
<head></head> 
<body> 
<?php 
    if (isset($_SESSION["username"])) { 
     $loggenOnUser = $_SESSION["username"]; 
     echo "Existing User: ", $loggenOnUser, "<br />" 
    } else { 
     $loggenOnUser = " new user"; 
    } 
?> 
    ... 
     <div id="LoggedInUser" class="fluid "> 
      Hi. I'm <?php echo $loggenOnUser; ?> 
     </div> 

はEDIT:

header.phpのかfooter.phpのいずれかにあなたのセッションの初期化コードを動かしてみて、他のすべてのPHPページのヘッダとフッタを含めますこれにより、すべてのページでセッション変数にアクセスできます。

のような他のページのヘッダーとfooter.phpを含めるhome.phpのように:あなたが代わりに$の.sessionのコードの束のクッキーで動作するようにいくつかのlibやjQueryプラグインを使用していないのはなぜ

<?php require "header.php"; ?> 
. 
. 
//Body 
. 
. 
<?php require "footer.php"; ?> 
+0

私はjqueryが必要です、私は1つのjquery変数を持っています。私はセッションを設定する必要があります –

関連する問題