Jump to content

Please help echoing text


lingo5

Recommended Posts

Hi,

I have this variable that checks which page we're on:

 

<?php 
$checkit = $_SERVER['PHP_SELF']; 
?>

 

and I need to echo some text depending which page we're on.

I am using this code to do so:

<?php if ($checkit=="index.php") {
echo "this is the home page";
}
if ($checkit=="index2.php") {
echo "this is the other page";
}?>

...but nothing is printed. what am i doing wrong?

Thanks

Link to comment
Share on other sites

@Muddy_Funcster - Probably because he has a page which is included in both and he wants to test which page he's on to echo out the relevant text.

 

@scootstah - Saying PHP_SELF is not safe is generalising the use of the variable. It is completely dependent on how and where your using it so saying it is simple "not safe" is incorrect.

 

@lingo5 - You can use the following code to get the file name

 

$file = explode("/", $_SERVER['PHP_SELF']);
echo $file[count($file) - 1];

 

If your using PHP v5.2.0+ you can use

 

$pathInfo = pathinfo($_SERVER['PHP_SELF']);
echo $pathInfo['filename'];

Link to comment
Share on other sites

@scootstah - Saying PHP_SELF is not safe is generalising the use of the variable. It is completely dependent on how and where your using it so saying it is simple "not safe" is incorrect.

 

PHP_SELF opens up XSS attacks. It should be avoided, especially when there are better ways to work. Such as SCRIPT_FILENAME or SCRIPT_NAME.

Link to comment
Share on other sites

@Muddy_Funcster - Probably because he has a page which is included in both and he wants to test which page he's on to echo out the relevant text.

 

AGAIN - Why not code it into the actual page? if you are looking up a page through include you have access to any variable on either page and can as such echo determinant infomation.  Job done. :shrug:

 

PS, unless they picked the wrong sex in the profile page - the OP would be a she, not a he. :P

Link to comment
Share on other sites

AGAIN - Why not code it into the actual page? if you are looking up a page through include you have access to any variable on either page and can as such echo determinant infomation.  Job done. :shrug:

 

Sometimes you may want to have things change in your overall layout depending on what page you are on. For example using menus which expand; you may want to expand the current page item.

Link to comment
Share on other sites

PS, unless they picked the wrong sex in the profile page - the OP would be a she, not a he. :P

 

Lol.

 

I understand what your saying, I personally would never do it this way but I'm answering the question asked. If we were to discuss "better ways" of doing stuff we could have endless topics as there is never a right or wrong way and people have opinions therefore, the discussion would never be solved.

 

@scootstah - Once again I'll reiterate my point. It's entirely dependent on how your using it. You've obviously read the article in Pikachu's signature and generalised it to all situations. The article linked by Pikachu is correct given that situation, in this situation however, it's not really applicable.

Link to comment
Share on other sites

thanks all!!...

I have used CPD's code to sort this out.

I did't know using $_SERVER was not safe....thanks for pointing that out.

...and yes Funster...I'm a grl  8)

 

Using the $_SERVER variable is not "not safe". Its perfectly safe provided you use it in a safe manner.

Link to comment
Share on other sites

AGAIN - Why not code it into the actual page? if you are looking up a page through include you have access to any variable on either page and can as such echo determinant infomation.  Job done. :shrug:

 

Sometimes you may want to have things change in your overall layout depending on what page you are on. For example using menus which expand; you may want to expand the current page item.

...you know what, theres clearly no point, it's just not getting through. :facewall:

 

@Lingo, glad it all worked out for you, now quick: lets all run away beofre we start that "better ways of doing stuff" thing that CPD thought of  ;D

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.