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:
Current Solution:
Problem:
Questions:
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");
}
}
}
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.
It is not recommend to use cookies as storage for any informations like logic, permissions or sensitive informations (like characters). Use sessions or files and only give an unique id the user in a cookie to refer to this file. But make sure the file cannot be hijacked (I'm refering to Session Hijacking).
To save the informations in a file, use a serialized php array and write it with PHP5 functions to a file and read again from it and save it in an array for further use in your script.
http://ch1.php.net/manual/en/function.serialize.php http://ch1.php.net/manual/en/function.unserialize.php http://ch1.php.net/manual/en/function.file-get-contents.php http://ch1.php.net/manual/en/function.file-put-contents.php