Page 13 of 35

Re: Video#5

Posted: Wed Jan 20, 2010 3:47 am
by Zerk
ZeroComp wrote:if (isset($_SESSION['palyer']))
theres your problem :)
HAHAHAHAHA! Lol, thanks....Silly typos :lol:

Re: Video#5

Posted: Wed Jan 20, 2010 4:36 am
by Jackolantern
Unfortunately, that is just one of the trade-offs for using a weakly-typed language like PHP. Strongly-typed languages such as C#, C, Java, etc. enforce you to declare your variables before use, which allows your IDE to easily tell you if you have mistyped a variable name (provided you have not mistyped it as the name of another declared variable). The benefit is that you have lots of various tricks you can use in weakly-typed languages that cannot be attempted in others.

Re: Video#5

Posted: Wed Jan 20, 2010 2:43 pm
by hallsofvallhalla
and you will really see the difference when you get to using javascript and php heavily together. Defining vars in one and not the other, mixing the . and + and other various things.

Re: Video#5

Posted: Wed Jan 20, 2010 2:49 pm
by SpikedRocker
I've updated my Paste-bin with one thing I found that was wrong, on my authenticate.php page I had it pointing to index.php, which I have yet to create, which I did make one to see if it would change something. It still directs at a blank page sitting with "authenticate.php?player=SpikedRocker&password=*******&submit=Login" like the GET method. Still not sure what else could be wrong but it does seem to be on the authenticate.php page. Still steaming forward with the tutorials hopefully someone sees something I don't.

Paste-bin page: http://abscured-vision-public.pastebin.com

UPDATE...I added some echo "Line #" ; lines into the different parts of the authenticate script to find where the script kicks out and it kicks out before

Code: Select all

if (isset($_POST['submit']))
{
$player=$_POST['player'];
$password=$_POST['password'];
$player=strip_tags($player);
$password=strip_tags($password);
$password=md5($password);
And doesn't make it to the next test echo line I inserted. So somewhere in there its kicking it out. I also get this error: "Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/content/s/p/i/spikedrocker/html/test/authenticate.php:1) in /home/content/s/p/i/spikedrocker/html/test/authenticate.php on line 3"

Re: Video#5

Posted: Sat Jan 30, 2010 3:43 pm
by Kismet
I've done something wrong :cry:.

I can register and login just fine, but my password isn't masked. For some reason the mask="x" thingy doesn't work for me. Here's my code:

register.php

Code: Select all

<?php
include 'connect.php';

?>

<form method ="post" action="reguser.php">
Type Username Here: <input type="text" name="player" size="21"><br>
Type Password Here: <input type="text" name="password" size "15" mask="x"><br>
ReType Password Again: <input type="text" name="pass2" size "15" mask="x"><br>
Type Email Address: <input type="text" name="email" size "60"><br>

<input type="submit" value="submit">
login.php

Code: Select all

<form method="POST" action="authenticate.php">
Username <input type="text" name="player" size="21"><br>
Password <input type="text" name="password" size="21" mask="x">
<br>
<input type="submit" value="login" name="submit">
My next problem is really odd. When fighting a creature, my hit points never go down, no matter how many times the creature hits me. But the creature's hitpoints go down just fine (as far as I can tell the code for the player and creature is exactly the same, so I'm really puzzled).

battle.php

Code: Select all

 <?php
include_once 'connect.php';

session_start();

if (isset($_SESSION['player']))
{
  $player=$_SESSION['player'];
}
else
{
  echo "Not Logged in <br><br> <A href='login.php'>Login</a>";
  exit;
}

$playerinfo="SELECT * from players where name='$player'";
$playerinfo2=mysql_query($playerinfo) or die("could not get player stats!");
$playerinfo3=mysql_fetch_array($playerinfo2);

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

if (isset($_GET['creature']))
{
   $creature=$_GET['creature'];
   $creatureinfo="SELECT * from creatures where name = '$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);
}

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

/////player info
echo "<u> " . $playerinfo3['name'] . "</u><br>";
echo "Hit points = " . $playerhp . "<br>";
echo "Attack = " . $playerattack . "<br>";
echo "Defense = " . $playerdefense . "<br><br><br>";

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

echo "<a href='attack.php?creature=$creature'>Attack!";

?>
attack.php

Code: Select all

 <?php
include_once 'connect.php';

session_start();

if (isset($_SESSION['player']))
{
  $player=$_SESSION['player'];
}
else
{
  echo "Not Logged in <br><br> <A href='login.php'>Login</a>";
  exit;
}

$playerinfo="SELECT * from players where name='$player'";
$playerinfo2=mysql_query($playerinfo) or die("could not get player stats!");
$playerinfo3=mysql_fetch_array($playerinfo2);

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

}
else
{
  echo "<a href='battle.php'>No Creature selected. Go Back!";
  exit;
}

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

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


///////////////////////players turn////////////////////

echo "<u> " . $playerinfo3['name'] . "'s Attack</u><br>";
$playerattack = rand(1,20) + $playerattack;
$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'] . " hits! <br>";
  $playerdamage = rand(1,6);
   $newcreaturehp = $creaturehp - $playerdamage;
  echo "For " . $playerdamage . " points of damage. <br>";
   if ($newcreaturehp < 1)
   {
     echo "The " . $creature . " has been killed";
       
        $updatecreature="DELETE from creatures where name='$creature' limit 1";
  mysql_query($updatecreature) or die("Could not update creature");
     
      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'] . " misses!<br>";
}
//////////////////////creatures turn //////////////////

echo "<u> " . $creature . "'s Attack</u><br>";
$creatureattack = rand(1,20) + $creatureattack;
$playerdefense = rand(1,20) + $playerdefense;

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

if ($creatureattack  > $playerdefense)
{
  echo $creature . " hits! <br>";
  $creaturedamage = rand(1,6);
   $newplayerhp = $playerhp - $creaturedamage;
   echo "For " . $creaturedamage . " points of damage. <br>";
   if ($newplayerhp < 1)
   {
     echo $playerinfo3['name'] . " has been killed<br>";
      echo "<a href='gameover.php'>Continue";
      exit;
   }
  $updateplayer="update players set hpoints='$newplayerhp' where name='player1'";
  mysql_query($updateplayer) or die("Could not update player");
}
else
{
  echo $creature . " misses!";
}
echo "<br><br><a href='battle.php?creature=$creature'>Battle Again!";
?>
Also, after I've killed both the goblin and the orc, the next battle returns an empty creature (no name or stats). Is it supposed to do that?

I hope somebody can tell me where I've messed up. Thanks muchly.

Re: Video#5

Posted: Sat Jan 30, 2010 5:46 pm
by Jackolantern
I think I see why the player is taking no damage. In your UPDATE query to deposit the new player's hitpoint total, you are using this query:

Code: Select all

$updateplayer="update players set hpoints='$newplayerhp' where name='player1'";
However, you are setting up your player's name variable:

Code: Select all

if (isset($_SESSION['player']))
{
  $player=$_SESSION['player'];
}
Either change the $_SESSION variable holder to $player1, or change your query script to $player. Right now your query is not finding anything, because it is looking for a name that holds "".

As for logging in and hiding the password, simply change your password input fields to:

Code: Select all

<input type="password" ....>
If you set your input type to "password", it will automatically hide it.

Re: Video#5

Posted: Sat Jan 30, 2010 6:21 pm
by Kismet
You must have the eyes of a hawk, how on earth did you spot that errant 1? I've lost count of how many times I've gone over this code looking for the mistake, and I missed it every single time :roll:.

Thankyou very, very much. You have fixed both issues for me :mrgreen:.

Onto the next tutorial!!!

Re: Video#5

Posted: Sat Jan 30, 2010 7:20 pm
by Jackolantern
Kismet wrote:You must have the eyes of a hawk, how on earth did you spot that errant 1? I've lost count of how many times I've gone over this code looking for the mistake, and I missed it every single time :roll:.

Thankyou very, very much. You have fixed both issues for me :mrgreen:.

Onto the next tutorial!!!
Your very welcome :) It can sometimes be very useful to show others our code because research has shown that when our minds make a mistake, our brain keeps making the same mistake every time we try to look over it. Kind of like blazing a path through the jungle. It feels natural to walk along the newly-beaten path, but if you got lost the first time, you are heading in that same direction.

In the future, a great way to help prevent these very-difficult-to-find query problems is to check your query right after it runs during testing. For example, right after you run your UPDATE query near the bottom of the script, you could alter your code like this:

Code: Select all

$updateplayer="update players set hpoints='$newplayerhp' where name='player1'";
mysql_query($updateplayer) or die("Could not update player");
  
//Remove after testing
$checkQuery="SELECT * from players where name='$player1'";
$holderQuery = mysql_query($checkQuery) or die("Could not check updated player HP");
$holderArray = mysql_fetch_assoc($holderQuery);
if($newplayerhp != $holderArray['hpoints']) {
      echo "Error updating player hitpoints. Please check db and try again.";
      exit;
}
(Of course this checking code was written assuming that you are looking up a playername in the 2nd query based on a variable. I know that is what they are eventually changed to, but I am not sure if you are still in the videos where the query looks up a constant playername {like in your original query above}. Also, be sure to remove this code before the game goes live, because this check should not be needed since your update query should be properly tested to be working by that point and it would just cause uneeded performance overhead.)

Re: Video#5

Posted: Sun Jan 31, 2010 5:13 am
by Kismet
Thankyou Jackolantern. Up till now I've only ever used Python, which is very good at telling you where you messed up. PHP seems to be very different, if you make a mistake, you're pretty much left on your own to find it.

This is not such a bad thing methinks, cause I seem to learn more from making mistakes than the other way around :roll:.

Anyway, thanks again for all your help.

Re: Video#5

Posted: Sun Jan 31, 2010 6:33 am
by Jackolantern
Yeah, most web development technologies really have to leave it up to you to find the bugs because you aren't testing inside an IDE. Some of the next-generation IDEs are starting to work on this however, with big companies like Zend claiming they are working on real in-IDE debugging including stepping, code-on-the-fly, etc.