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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.