douwei9973 2014-08-17 22:15
浏览 65
已采纳

使用Cookie管理大量动态变量

I am building a website that hosts an online game. In this game there are lots of characters and monsters. Each of these has their own base set of numbers to determine the details of each particular character. I have one master character sheet/management pages to change/modify these characters.

Goal:

  • I need to load character information once, no matter what page the user goes to or if they refresh the page. If the character doesn't change I do not want the server to recalculate everything. If the character changes stats or they change to a new character I need to recalculate the character.

Current Solution:

  • I pass the character ID, charID as a URL parameter. A function checks if it a new character or the same. It checks to see when the character was last updated. It also checks to see if there is a cookie "_created_date" and what the date is. If it does not see the cookie, or the character has been updated it builds new cookies for the character information. This function dynamically builds 40-60 cookies with all kinds of information.

Problem:

  • When the function sees it should rebuild the cookies it does so, but it also logs out the user. I think this is because I am not unsetting the cookies properly. I think I am unsetting the login cookies as well.

Questions:

  1. Is using cookies the right approach to managing all of this information on a client by client basis. I think it is. But it is the first time I have ever done anything on this scale.
  2. What is the best way to manage a group of cookies? In a cookie array? Should they all have the same prefix or something?
  3. How can you delete/unset this group of cookies without messing with any other cookies on the website?
  4. I see cookies being built in /tools and in /tools/edit_character. (Function checks state of cookie on about 6 different pages.) Should all these pages be under the same directory? Will deleting/unsetting cookies in a directory also remove those in child directories?
  5. Should I stick with session vars or give them expiration dates out into the future? In the case of them coming back in a couple days.

I have already seen how passing all these cookies to the client has dramatically saved server resources and time which is great. However, logging my users out all of the time is a terrible user experience that I really need to correct.

Website is on apache server, mysql, php, jQuery, js, ajax, wordpress base (the pages that do this are all custom templates).

Thank you for your suggestions and help. If there is any other information I can provide please let me know.

Here is just a piece of the cookie building function.

function fyxt_setActiveCharCookie ($charID, $fyxtAccountID) {
    global $wpdb;
    setcookie("charID",$charID);
    $charLvl = curBaseCharLvl($charID);
    setcookie("charLvl", $charLvl); //sets session var

    //Begin to build character information /////////////////////////////////////////////////////////////////////
    $isNPC = isNPC($charID);
    setcookie("isNPC", $isNPC); //sets session var
    if ($isNPC == 1) {
        $hordeSize = hordeSize($charID);
        setcookie("hordeSize", $hordeSize); //sets session var
        $charL1Info = getNPCInfo($charID);
        foreach ($charL1Info as $n=>$v) {
                setcookie($n, $v); //loop to set vars for all char info
        }
   }
}

Unset Function

function fyxt_unsetActiveCharCookie () {
    foreach ($_COOKIES as $c_id => $c_value)
    {
        if ((strpos($c_id,'wordpress') !== false) || (strpos($c_id,'wp') !== false) || (strpos($c_id,'phpbb3') !== false) || (strpos($c_id,'__ut') !== false) || (strpos($c_id,'PHPSESSID') !== false)) {
            //don't delete
        } else { 
            setcookie($c_id, NULL, 1, "/fyxt-rpg-tools/", ".fyxtrpg.com");
        }
    }
}
  • 写回答

2条回答 默认 最新

  • dsmupo6631 2014-09-12 00:02
    关注

    After looking at this and trying a few things I have decided to add an intermediary table to the database. In this table I will store the calculated character stats. Then I can retrieve these easily with a single select.

    Also this intermediary table will help to tackle an issue I was having with keeping multiple characters "in memory" for more advanced calculations and comparisons. This way all that is stored in a table that can be referenced instead of storing a bunch of variables either on the server or the user’s computer.

    This also seems to have a good impact on server load. I retrieve the character info and rewrite this one row in the table after a level up or other character change. If no change, I can just hold the array that is originally retrieved from this intermediary table.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 关于#硬件工程#的问题:求一下解题思路
  • ¥15 运筹学对偶单纯行法构造扩充问题
  • ¥20 XP系统的老电脑一开机就提示找不到rundll.exe,付费求解
  • ¥15 milvus查询出来的score怎么转换成0-1之间的相似性
  • ¥15 多ip服务器站群如何搭建l2tp服务器
  • ¥15 lvgl V9移植到linux开发板
  • ¥15 VB.net中在窗体中创建一个button控件来关闭窗体,但是提示错误,我该怎么办
  • ¥15 网上下载好的程序但是arduinoIDE编程报错,运行不了,哪里出错了,能具体给改一下吗
  • ¥15 Sharepoint JS开发 付费技术指导
  • ¥15 输入程序运行仿真后,烟雾值不实时检测,变成固定值