Allow users access to 1 specific page when they are dead

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cdoyle
Forum Contributor
Posts: 102
Joined: Wed Feb 13, 2008 7:26 pm

Allow users access to 1 specific page when they are dead

Post 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
cdoyle
Forum Contributor
Posts: 102
Joined: Wed Feb 13, 2008 7:26 pm

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

Post by cdoyle »

Anyone have any ideas on how to make what I need to do work?
hyder_m29
Forum Newbie
Posts: 12
Joined: Mon Jun 23, 2008 9:36 am

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

Post 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
cdoyle
Forum Contributor
Posts: 102
Joined: Wed Feb 13, 2008 7:26 pm

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

Post by cdoyle »

Is there a way to make it look for just inventory.php?

Or another method that would work for what I need?
hyder_m29
Forum Newbie
Posts: 12
Joined: Mon Jun 23, 2008 9:36 am

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

Post 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
cdoyle
Forum Contributor
Posts: 102
Joined: Wed Feb 13, 2008 7:26 pm

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

Post 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?
hyder_m29
Forum Newbie
Posts: 12
Joined: Mon Jun 23, 2008 9:36 am

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

Post 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 ])
cdoyle
Forum Contributor
Posts: 102
Joined: Wed Feb 13, 2008 7:26 pm

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

Post 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?
hitman6003
Forum Newbie
Posts: 4
Joined: Mon Jun 23, 2008 7:36 pm

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

Post by hitman6003 »

Use the __FILE__ special constant. It contains the name of the file.

http://www.php.net/language.constants.predefined
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

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

Post by califdon »

I get a bit worried when I see a member named hitman in a thread about users "when they are dead"!
:P
Post Reply