Kryllster Posted January 17, 2011 Share Posted January 17, 2011 I am having problems with this code sometimes it works other times it doesnt I cant figure out why. <?php session_start(); $xml = simplexml_load_file("xmlmonstor/skeleton.xml"); //get the hp, if it exists if (!isset($_SESSION['player_hp'])) { $player_hp = 30; } else { $player_hp = $_SESSION['player_hp']; } if (!isset($_SESSION['mob_hp'])) { $mob_hp = $xml->mob_hp; } else { $mob_hp = $_SESSION['mob_hp']; } // Set amount of starting turns if (!isset($_SESSION['turns'])) { $turns = 500; } else { $turns = $_SESSION['turns']; } $first = mt_rand(1,100); //find out who goes first if ($first < 60) { $player_damage = 5; $mob_hp -= $player_damage; } else { $mob_attack = $xml->attack; $player_hp -= $mob_attack; } if ($player_hp <= 0) { header("Location:south.php?p=defeat"); $turns = $turns - 1; $_SESSION['turns'] = $turns; exit(); } if($mob_hp <= 0) { header("Location:south.php?p=victory"); $turns = $turns - 1; $_SESSION['turns'] = $turns; exit(); } //Gotta store them back in sessions $_SESSION['player_hp'] = $player_hp; $_SESSION['mob_hp'] = $mob_hp; $_SESSION['turns'] = $turns; ?> Am I setting the sessions right or is there a better way to do it? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/224697-is-this-the-correct-way-to-set-a-session/ Share on other sites More sharing options...
JAY6390 Posted January 17, 2011 Share Posted January 17, 2011 Be sure to set your session variables before redirecting Quote Link to comment https://forums.phpfreaks.com/topic/224697-is-this-the-correct-way-to-set-a-session/#findComment-1160634 Share on other sites More sharing options...
Kryllster Posted January 17, 2011 Author Share Posted January 17, 2011 Thanks for the reply! How do I set my session variable before I redirect I think thats the problem I have been running into. Quote Link to comment https://forums.phpfreaks.com/topic/224697-is-this-the-correct-way-to-set-a-session/#findComment-1160640 Share on other sites More sharing options...
dragon_sa Posted January 17, 2011 Share Posted January 17, 2011 move the header(Location: ....) just above your exit; line so the sessions are added first at the moment you are redirecting before the rest is executed Quote Link to comment https://forums.phpfreaks.com/topic/224697-is-this-the-correct-way-to-set-a-session/#findComment-1160641 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.