doubi6215 2016-12-15 06:50
浏览 38

如何创建会话变量并显示另一个页面

In index.php I set a session variable in jQuery, and in this page I am getting my token id , but I want to display it in another page like home.php. So I am doing in home.php page

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>

</div>
  • 写回答

1条回答 默认 最新

  • dounai9294 2016-12-15 07:00
    关注

    you can create your session variable in a file(header.php) which should be included in every other file(home.php in this case)

    So,create session like this

    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;
        }
    ?>
    

    Possible 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:

    Try moving your session initialization code to either header.php or footer.php and include header and footer in every other php page, this will allow access to the session variable in every page.

    Include header and footer.php in other pages like home.php like:

    <?php require "header.php"; ?>
    .
    .
    //Body
    .
    .
    <?php require "footer.php"; ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?