Jump to content

Is this the correct way to set a session?


Kryllster

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.