Page 1 of 1

Allow users access to 1 specific page when they are dead

Posted: Sun Jun 22, 2008 8:33 pm
by cdoyle
In my ezRPG game, I have successfully been able to stop players from doing anything after they lose a battle, by using this code

Code: Select all

 
<?php
$playerdead = $db->execute("SELECT p1.username as killedby, Time_Left FROM medical_ward m
                            INNER JOIN players p1 ON m.Killed_By_ID = p1.id
                            where playerdead_ID = $player->id");
                            
$playerdead1= $playerdead->fetchrow();
 
if($playerdead1 [Time_Left] > 0)
{
    echo "You were put in the hospital by " .$playerdead1[killedby] ."<p>";
    echo "You have \n"  . $playerdead1[Time_Left] . "\n Minutes remaining";
exit();
}
 
 
?>
 
Now what I need to do is, allow them to access their inventory page, so they can use their meds, to heal quicker.

On one forum, this was mentioned

Code: Select all

 
if($playerdead1 [Time_Left] > 0 && $PHP_SELF != 'inventory.php')
{
 
but this didn't work.
So I searched google, and was able to come up with this.

Code: Select all

 
if($playerdead1 [Time_Left] > 0 && $_SERVER['PHP_SELF'] != 'inventory.php')
 
but that still didn't work.
Anyone have any suggestions on how to allow users, to access only the inventory.php page when they are in the hospital?

Thanks

Re: Allow users access to 1 specific page when they are dead

Posted: Mon Jun 23, 2008 4:40 pm
by cdoyle
Anyone have any ideas on how to make what I need to do work?

Re: Allow users access to 1 specific page when they are dead

Posted: Mon Jun 23, 2008 5:10 pm
by hyder_m29
$_SERVER['PHP_SELF'] returns the entire path minus the hostname

So, if the file that is being accessed is http://www.sample.com/dir/anotherdir/inventory.php

..... it will return /dir/anotherdir/inventory.php

..... and not just inventory.php

Re: Allow users access to 1 specific page when they are dead

Posted: Mon Jun 23, 2008 5:16 pm
by cdoyle
Is there a way to make it look for just inventory.php?

Or another method that would work for what I need?

Re: Allow users access to 1 specific page when they are dead

Posted: Mon Jun 23, 2008 5:28 pm
by hyder_m29
I'm not so good at php myself, so if someone has a better answer, ignore me!

One way to do that is to explode the string into an array using "/" as delimiter and read the last element.


e.g.

Code: Select all

$arrPHPSelf = explode ("/", $_SERVER['PHP_SELF']);
$arrPHPSelf [count($arrPHPSelf) - 1]  ==> this should give you inventory.php

Re: Allow users access to 1 specific page when they are dead

Posted: Mon Jun 23, 2008 5:34 pm
by cdoyle

Code: Select all

$arrPHPSelf = explode ("/", $_SERVER['PHP_SELF']);
$arrPHPSelf [count($arrPHPSelf) - 1]
OK, going to try this out.

I add this to the code I'm already using right?

Re: Allow users access to 1 specific page when they are dead

Posted: Mon Jun 23, 2008 6:05 pm
by hyder_m29
cdoyle wrote:

Code: Select all

$arrPHPSelf = explode ("/", $_SERVER['PHP_SELF']);
$arrPHPSelf [count($arrPHPSelf) - 1]
OK, going to try this out.

I add this to the code I'm already using right?
Ahh, no, not exactly

add this line:

Code: Select all

$arrPHPSelf = explode ("/", $_SERVER['PHP_SELF']);
and replace this line:

Code: Select all

if($playerdead1 [Time_Left] > 0 && $_SERVER['PHP_SELF'] != 'inventory.php')
with this:

Code: Select all

if($playerdead1 [Time_Left] > 0 && $arrPHPSelf [ count ($arrPHPSelf) - 1 ])

Re: Allow users access to 1 specific page when they are dead

Posted: Mon Jun 23, 2008 6:15 pm
by cdoyle
hyder_m29 wrote:
cdoyle wrote:

Code: Select all

$arrPHPSelf = explode ("/", $_SERVER['PHP_SELF']);
$arrPHPSelf [count($arrPHPSelf) - 1]
OK, going to try this out.

I add this to the code I'm already using right?
Ahh, no, not exactly

add this line:

Code: Select all

$arrPHPSelf = explode ("/", $_SERVER['PHP_SELF']);
with this:

Code: Select all

if($playerdead1 [Time_Left] > 0 && $arrPHPSelf [ count ($arrPHPSelf) - 1 ])
I'm not seeing how it tells it to only open the inventory.php page, and block all others?

Re: Allow users access to 1 specific page when they are dead

Posted: Mon Jun 23, 2008 7:41 pm
by hitman6003
Use the __FILE__ special constant. It contains the name of the file.

http://www.php.net/language.constants.predefined

Re: Allow users access to 1 specific page when they are dead

Posted: Mon Jun 23, 2008 11:25 pm
by califdon
I get a bit worried when I see a member named hitman in a thread about users "when they are dead"!
:P