Jump to content

Help with session variables plz


Gheeda

Recommended Posts

Well there are quite some tutorials online. but here is something.

 

At the top of each script you want to use a session variable  place

<?php session_start(); ?>

This must be at the TOP of your page.

 

giving a value to a session variable (almost like a $_POST var) 

<?php  $_SESSION['moo'] = 'I am a monkey'; ?>

ON another page you just place:

 

<?php  
   echo $_SESSION['moo']; //or
   $string = $_SESSION['moo'];
   echo $string;
?>

 

Link to comment
Share on other sites

Hmm...no error but I dont see the data sticking. Do i have to place this code in a particular place? Like...within my submit button? Heres currently whats going on

 

-Battle.php has a dropdown menu of user specific spells....I need (on submit) for the selected spell 'id' to be placed into a session variable so my attack.php can pick up on it.

 

Of course I'm fetching my user spells with a row query...$sid is to = $row[1] and Session to = $sid....but only the selected spell..No matter what I do I cant get the submit data to anything...

 

Thoughts?

 

 

Link to comment
Share on other sites

well if a you are using a submit button the variables are going to be sent via the post variable. (if you use method post)

 

So in case you have 2 files an index.php with the form and a submit button. And a form action to functionality.php.

 

in functionality.php you retrieve the sent variable by asking for the $_POST variable:

Like"

<?php session_start(); ?> <!-- ON TOP ABOVE EVERYTHING!!! -->

 

in the <body>

<?php $_SESSION['my_session_string'] = $_POST['string_or_any_value']; ?>

 

 

Also read this tutorial (http://w3schools.com/php/php_forms.asp) please, and post your code please.

Link to comment
Share on other sites

well if a you are using a submit button the variables are going to be sent via the post variable. (if you use method post)

 

So in case you have 2 files an index.php with the form and a submit button. And a form action to functionality.php.

 

in functionality.php you retrieve the sent variable by asking for the $_POST variable:

Like"

<?php session_start(); ?> // ON TOP ABOVE EVERYTHING!!!

 

in the <body>

<?php $_SESSION['my_session_string'] = $_POST['string_or_any_value']; ?>

 

 

Also read this tutorial (http://w3schools.com/php/php_forms.asp) please, and post your code please.

 

Still no luck. The below pages are gonna be messy because I've been trying stuff out on em' constantly....all efforts fruitless.

 

battle.php

<?php
include_once 'connect.php';
session_start();

include_once 'logo.php';
?>
<link href="style.css" rel="stylesheet" type="text/css" />
<div id="login2" div align="center">


<?php




if (isset($_SESSION['player']))
{
  $player=$_SESSION['player'];
}
else
{
  echo "Not Logged in <br><br> <A href='login.php'>Login</a>";
  exit;
}
?>
</div>
<?php
$playerinfo="SELECT * from players where name='$player'";
$playerinfo2=mysql_query($playerinfo) or die("could not get player stats!");
$playerinfo3=mysql_fetch_array($playerinfo2);

include_once 'statpanel.php';

$pid = $playerinfo3['id'];
$name = $playerinfo3['name'];
$playerhp = $playerinfo3['hpoints'];
$playerattack = $playerinfo3['attack'];
$playerdefense = $playerinfo3['defense'];

if ($playerhp < 1)
{
  echo "You are dead!" ;
  echo "<br><a href='inn.php>Heal at the lake";
  exit;
}
?>
<div id="table">

<?php

if (isset($_GET['randid']))
{
   $randid=$_GET['randid'];
   $iteminfo="SELECT * from inventory where randid='$randid' AND id ='$pid'";
$iteminfo2=mysql_query($iteminfo) or die("could not get item stats!");
$iteminfo3=mysql_fetch_array($iteminfo2);

if (!$iteminfo3['name'])
{
}
else
{

$iname = $iteminfo3['name'];
$stats = $iteminfo3['stats'];
$statadd = $iteminfo3['statadd'];
$type = $iteminfo3['type'];

if ($type == "healing")
{
   $newhp = $statadd + $playerhp;
   if ($newhp > $playerinfo3['maxhp'])
   {
    $newhp = $playerinfo3['maxhp'];
   }
    $updateplayer="update players set hpoints='$newhp' where id='$pid'";
  mysql_query($updateplayer) or die("Could not update player");

    $updateitem="DELETE from inventory where id='$pid' AND randid='$randid' limit 1";
  mysql_query($updateitem) or die("Could not delete item");
   
   $playerhp = $newhp;

  echo "Used " . $iname . " and recovered " . $statadd . ".<br>";
}

}}





$creature = $playerinfo3['creature'];
if ($creature != 0)
{
$creatureinfo="SELECT * from creatures where id='$creature'";
$creatureinfo2=mysql_query($creatureinfo) or die("could not get the creature you were fighting!");
$creatureinfo3=mysql_fetch_array($creatureinfo2);

}
else
{
  $creatureinfo="SELECT * from creatures order by rand() limit 1";
$creatureinfo2=mysql_query($creatureinfo) or die("could get a creature!");
$creatureinfo3=mysql_fetch_array($creatureinfo2);
$cid = $creatureinfo3['id'];
$updateplayer="update players set creature='$cid' where name='$name'";
  mysql_query($updateplayer) or die("Could not update player");
}

$creature = $creatureinfo3['name'];
$creaturehp = $creatureinfo3['hpoints'];
$creatureattack = $creatureinfo3['attack'];
$creaturedefense = $creatureinfo3['defense'];



?>
</div>
<div id="player">
<?php
/////player info
echo "<u> " . $playerinfo3['name'] . "</u><br>";
echo "Hit points = " . $playerhp . "<br>";
echo "Attack = " . $playerattack . "<br>";
echo "Defense = " . $playerdefense . "<br><br><br>";
?>
</div>
<div id="creature">
<img src="images/<?php echo $creatureinfo3['imgurl'];?>" <br>
<?php
///////creature info

echo "<u> " . $creatureinfo3['name'] . "</u><br>";
echo "Hit points = " . $creaturehp . "<br>";
echo "Attack = " . $creatureattack . "<br>";
echo "Defense = " . $creaturedefense . "<br><br><br>";
?>
</div>

<div id="options">
<?php
echo "<a href='attack.php'>Attack</a>";
echo "<br><a href='usemagic.php'>Use Magic</a>";
echo "<br><a href='useitem.php'>Use Item</a>";
echo "<br><a href='index.php'>Exit Arena</a>";


?>
</div>

<div id="logout">
<?php
echo "<br><a href='logout.php'><img src='images/logout.gif' border='0'></a>";
?>
</div>
<div id="skillselect">
<?php
$playerid = $playerinfo3['id'];

//Next line assumes that each player may have more than one skill in the "skills"
//...table with their player ID, thus returning more than 1 result
if ($skillresult = mysql_query("SELECT * FROM playermagic WHERE pid='$playerid'")) {
  echo "<form action='attack.php' method=post>";
    echo '<select name="skills">';
    echo "<option value=>*Select a Skill*</option>";
    while ($row = mysql_fetch_row($skillresult)) {

          //Next line assumes that the name of the skill is in the first column of the "skills" table.
          //If it is not, just modify the number. For example, if the name of the skill is in the
          //...fourth column, you would use $row[3] (remember arrays are 0-based).

          echo "<option value=\"$row[2]\">$row[2] <font color='ffffff'>____</font> $row[6]sp</option>";
    }
    $_SESSION['sid'] = $_POST;
    echo "</select>";
    echo '<input type="submit" value="Use skill" name="skill">';
    echo "</form>";
} else {
    echo "You do not have any usable skills";
}
?>

 

attack.php

<?php
include_once 'connect.php';
  session_start();
  include_once 'logo.php';
?>

  <link href="style.css" rel="stylesheet" type="text/css" />
<div id="login1" div align="center">

<?php
if (isset($_SESSION['player']))
{
  $player=$_SESSION['player'];
}
else
{
  echo "Not Logged in <br><br> <A href='login.php'>Login</a>";
  exit;
}
?>
</div>
<?php
   $string = $_SESSION['sid'];
$playerinfo="SELECT * from players where name='$player'";
$playerinfo2=mysql_query($playerinfo) or die("could not get player stats!");
$playerinfo3=mysql_fetch_array($playerinfo2);
include_once 'statpanel.php';
$pid = $playerinfo3['id'];
?>
</div>
<div id="table">
<?php

$creature = $playerinfo3['creature'];
if ($creature != 0)
{
$creatureinfo="SELECT * from creatures where id='$creature'";
$creatureinfo2=mysql_query($creatureinfo) or die("could not get the creature you were fighting!");
$creatureinfo3=mysql_fetch_array($creatureinfo2);
$gold = $creatureinfo3['gold'];
}
else
{
  echo "<a href='battle.php'>No Creature selected. Go Back!";
  exit;
}

$playerhp = $playerinfo3['hpoints'];
$playerattack = $playerinfo3['attack'];
$playerdefense = $playerinfo3['defense'];
$playerpass = 0;


$creature = $creatureinfo3['name'];
$creaturehp = $creatureinfo3['hpoints'];
$creatureattack = $creatureinfo3['attack'];
$creaturedefense = $creatureinfo3['defense'];
$creatureid = $creatureinfo3['id'];

if ($playerhp < 1)
{
  echo "You are dead!" ;
  echo "<br><a href='inn.php>Heal at the lake";
  exit;
}
?>
</div>

<div id="player">
<?php
///////////////////////players turn////////////////////

echo "<center><u> " . $playerinfo3['name'] . "'s Attack</u><br>";
if (isset($_GET['sid']))
{

   $spellinfo="SELECT * from playermagic where sid='$string' AND pid='$pid'";
$spellinfo2=mysql_query( $spellinfo) or die("could not get spell!");
$spellinfo3=mysql_fetch_array( $spellinfo2);
$sname = $spellinfo3['name'];
if ($spellinfo3['type'] == "combat")
   {
   if ($spellinfo3['scost'] > $playerinfo3['spoints'])
   {
   echo "You do not have enough Spell Points for this spell.<br>";
    echo "<a href='battle.php'>Go Back";
   exit;
   }
   else
   {
   $weaponinfo="SELECT * from playerweapons where equip=1 AND pid='$pid'";
$weaponinfo2=mysql_query( $weaponinfo) or die("could not get player weapon!");
$weaponinfo3=mysql_fetch_array( $weaponinfo2);
   $spoints = $spellinfo3['scost'];
   $moddamage = $spellinfo3['svalue'] / 10;
   $moddamage = (int)$moddamage;
   $sdamage =  $spellinfo3['svalue'];
   $totalspoints = $playerinfo3['spoints'];
   $sresult = $totalspoints - $spoints;
  $playerdamage = $sdamage;
   $newcreaturehp = $creaturehp - $playerdamage;
   echo " You Cast " . $sname . " and do " . $sdamage . " points of damage";

   $updateplayer="update players set spoints='$sresult' where name='$player'";
   mysql_query($updateplayer) or die("Could not update creature");
   

   if ($newcreaturehp < 1)
   {
     echo "<br>The " . $creature . " has been killed";

   $crmaxhp=$creatureinfo3['maxhpoints'];
       $updatecreature="update creatures set hpoints='$crmaxhp' where name='$creature' limit 1";
   mysql_query($updatecreature) or die("Could not update creature");



        if ($playerinfo3['level'] > $creatureinfo3['level'])
        {
         $firstmod = $playerinfo3['level'] - $creatureinfo3['level'];
         $secondmod = $firstmod * 10 ;
         if ($secondmod > 90){$secondmod = 90;}
         $thirdmod = ($secondmod / 100) * $creatureinfo3['exper'];
         $totalexper =$creatureinfo3['exper'] - $thirdmod;
        }
        else
        {
         $firstmod = $creatureinfo3['level'] - $playerinfo3['level'];
         $secondmod = $firstmod * 10 ;
         if ($secondmod > 90){$secondmod = 90;}
         $thirdmod = ($secondmod / 100) * $creatureinfo3['exper'];
         $totalexper =$creatureinfo3['exper'] + $thirdmod;
        }
          $totalexper = (int)$totalexper;

           echo "<br><b><big>You gain " . $totalexper . " experience and " . $gold . " gold.</b></big><br>";
      $updateplayer="update players set exper=exper+'$totalexper', gold=gold+'$gold', creature=0 where name='$player'";
  mysql_query($updateplayer) or die("Could not update player");



      echo "<a href='battle.php'>Go Back";
      exit;
   }
   $playerpass = 0;
     $updateplayer="update players set spoints=spoints-'$spoints'where name='$player'";
  mysql_query($updateplayer) or die("Could not update player");
  $updatecreaturef="update creatures set hpoints='$newcreaturehp' where name='$creature' limit 1";
  mysql_query($updatecreaturef) or die("Could not update creature");
   }}
}




if ($playerpass != 1)
{

$weaponinfo="SELECT * from playerweapons where equip=1 AND pid='$pid'";
$weaponinfo2=mysql_query( $weaponinfo) or die("could not get player weapon!");
$weaponinfo3=mysql_fetch_array( $weaponinfo2);

$playerattack = rand(1,20) + $playerattack + $weaponinfo3['rating'];
//////////////////////////////////////////////////////
$creaturedefense = rand(1,20) + $creaturedefense;

//echo $playerinfo3['name'] . "'s Attack roll is " . $playerattack . "<br>";
//echo $creature . "'s defense roll is " . $creaturedefense. "<br>";

if ($playerattack  > $creaturedefense)
{
  echo  $playerinfo3['name'] . " " . $weaponinfo3['hittext'] . " and hits!<br>";


  $weapdamage = $weaponinfo3['damage'];
  $halfdamage = $weaponinfo3['damage'] / 2;
  $playerdamage = rand($halfdamage,$weapdamage);
   $newcreaturehp = $creaturehp - $playerdamage;
  echo "For " . $playerdamage . " points of damage. <br>";
   if ($newcreaturehp < 1)
   {
     echo "The " . $creature . " has been killed";

   $crmaxhp=$creatureinfo3['maxhpoints'];
       $updatecreature="update creatures set hpoints='$crmaxhp' where name='$creature' limit 1";
   mysql_query($updatecreature) or die("Could not update creature");



        if ($playerinfo3['level'] > $creatureinfo3['level'])
        {
         $firstmod = $playerinfo3['level'] - $creatureinfo3['level'];
         $secondmod = $firstmod * 100 ;
         if ($secondmod > 90){$secondmod = 90;}
         $thirdmod = ($secondmod / 100) * $creatureinfo3['exper'];
         $totalexper =$creatureinfo3['exper'] - $thirdmod;
        }
        else
        {
         $firstmod = $creatureinfo3['level'] - $playerinfo3['level'];
         $secondmod = $firstmod * 10 ;
         if ($secondmod > 90){$secondmod = 90;}
         $thirdmod = ($secondmod / 100) * $creatureinfo3['exper'];
         $totalexper =$creatureinfo3['exper'] + $thirdmod;
        }
          $totalexper = (int)$totalexper;

           echo "<br><b><big>You gain " . $totalexper . " experience and " . $gold . " gold.</b></big><br>";
      $updateplayer="update players set exper=exper+'$totalexper', gold=gold+'$gold', creature=0 where name='$player'";
  mysql_query($updateplayer) or die("Could not update player");



      echo "<a href='battle.php'>Go Back";
      exit;
   }

  $updatecreature="update creatures set hpoints='$newcreaturehp' where name='$creature' limit 1";
  mysql_query($updatecreature) or die("Could not update creature");
}
else
{
  echo  $playerinfo3['name'] . " " . $weaponinfo3['hittext'] . " and misses!";
}}
?>
</div>
<div id="creature">
<?php
//////////////////////creatures turn //////////////////

echo "<center><u> " . $creature . "'s Attack</u><br>";



///////hit location//////////////////////////////////////
      $randlocation = rand(0,100);
      if ($randlocation <= 15)
      {$location = "arms";}
      elseif ($randlocation <= 70)
      {$location = "chest";}
      elseif ($randlocation <= 90)
      {$location = "legs";}
      elseif ($randlocation <= 100)
      {$location  = "head";}

   $playerarmor="SELECT * from playerarmor where pid='$pid' AND location='$location' AND equip=1";
   $playerarmor2=mysql_query($playerarmor) or die("Could not get player armmor");
   $playerarmor3=mysql_fetch_array($playerarmor2);

   $noarmor = 0;

   if(is_null($playerarmor3['rating']))
   {
   $playerarmor3['rating'] = 0;
   $noarmor = 1;
   }


   ////////////////////////////////////
$creatureattack = rand(1,20) + $creatureattack;
$playerdefense = rand(1,20) + $playerdefense + $playerarmor3['rating'];

//echo $creature . "'s Attack roll is " . $creatureattack . "<br>";
//echo $playerinfo3['name'] . "'s defense roll is " . $playerdefense . "<br>";

if ($creatureattack  > $playerdefense)
{

  echo $creature . " hits for a strike to the " . $location . "<br>";
   $creaturedamage = rand(1,6);
   $newplayerhp = $playerhp - $creaturedamage;
   echo "For " . $creaturedamage . " points of damage. <br>";
  if ($noarmor == 1)
  {
    ".<br>";
  echo "You are not wearing armor on your " . $location . ".<br>";
   $extradamage = rand(1,3);
   echo "You take an additional " . $extradamage . " points of damage.<br>";
  $newplayerhp = $newplayerhp - $extradamage;
          $updateplayer="update players set hpoints='$newplayerhp' where name='$player'";
  mysql_query($updateplayer) or die("Could not update player");
  }
     if ($newplayerhp < 1)
   {
     echo $playerinfo3['name'] . " has been killed<br>";
      echo "<a href='index.php'>Back to town.";
       exit;
   }

}
else
{
  echo $creature . " misses!";
}
echo "<br><br><a href='battle.php?creature=$creature'>Attack em' some more!";
?>

</div>

 

Having more issues now >.> Gonna have to redo my whole attack.php because of this new form. No attacks register any damage whatsoever, be it standard attack/item..or skill...

 

Link to comment
Share on other sites

OK look at the attached files, with on top I mean ON TOP  ::)

 

Having more issues now >.> Gonna have to redo my whole attack.php because of this new form. No attacks register any damage whatsoever, be it standard attack/item..or skill...

 

step by step, and first things first.

 

 

 

- edit:  Maybe also look at your divs, SOme tags are missing. Maybe use NetBeans, a free program to write nice code, with tips and hints

 

[attachment deleted by admin]

Link to comment
Share on other sites

OK look at the attached files, with on top I mean ON TOP  ::)

 

Having more issues now >.> Gonna have to redo my whole attack.php because of this new form. No attacks register any damage whatsoever, be it standard attack/item..or skill...

 

step by step, and first things first.

 

 

 

- edit:  Maybe also look at your divs, SOme tags are missing. Maybe use NetBeans, a free program to write nice code, with tips and hints

 

I couldn't be more frustrated. Battle & Attack were functioning fine before I started implementing this absolutely necessary dropdown menu. Gonna take a break from this before I decide to trash the whole project. Thanks for the help, its much appreciated.

Link to comment
Share on other sites

OK look at the attached files, with on top I mean ON TOP  ::)

 

Having more issues now >.> Gonna have to redo my whole attack.php because of this new form. No attacks register any damage whatsoever, be it standard attack/item..or skill...

 

step by step, and first things first.

 

 

 

- edit:  Maybe also look at your divs, SOme tags are missing. Maybe use NetBeans, a free program to write nice code, with tips and hints

 

Nothing..I couldn't be more frustrated. Battle & Attack were functioning fine before I started implementing this absolutely necessary dropdown menu. Gonna take a break from this before I decide to trash the whole project. Thanks for the help, its much appreciated.

Link to comment
Share on other sites

I assume you just ran the two file I made? That should give you an idea of session variables, exactly the thing you asked for.

 

Indeed. But applying the same idea to my pages resulted in a big ass headache. Chances are I'm too frustrated to think clearly. I'll give it another go shortly..

 

edit: This battle stuff ( and equally so, the pvp ) is the last of my worries...I would have this problem ALL sorted out if I could somehow set the forum after the loop.... (LIKE BELOW)......but of course this doubles the forum....Stupid ass $sid variable is giving me so much trouble. I hope people get to play the game too but If I cant get this battle stuff sorted...there's no point :P

 

 

if ($skillresult = mysql_query("SELECT * FROM playermagic WHERE pid='$playerid'")) {
    echo '<select name="skills">';
    echo "<option value=>*Select a Skill*</option>";
    while ($row = mysql_fetch_row($skillresult)) {
          $sid = $row['1'];
          echo "<form action=\"attack.php?sid=$sid\" method=\"post\">";
          //Next line assumes that the name of the skill is in the first column of the "skills" table.
          //If it is not, just modify the number. For example, if the name of the skill is in the
          //...fourth column, you would use $row[3] (remember arrays are 0-based).

          echo "<option value=\"$row[2]\">$row[2] <font color='ffffff'>____</font> $row[6]sp</option>";
    }

    echo "</select>";
    echo '<input type="submit" value="Use skill" name="skill">';
    echo "</form>";

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.