lingo5 Posted March 12, 2012 Share Posted March 12, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/258737-please-help-echoing-text/ Share on other sites More sharing options...
TOA Posted March 12, 2012 Share Posted March 12, 2012 Echo $_SERVER['PHP_SELF'] (or $checkit) and see if it holds what you expect. Quote Link to comment https://forums.phpfreaks.com/topic/258737-please-help-echoing-text/#findComment-1326398 Share on other sites More sharing options...
scootstah Posted March 12, 2012 Share Posted March 12, 2012 $_SERVER['PHP_SELF'] is not safe, so don't use it. Quote Link to comment https://forums.phpfreaks.com/topic/258737-please-help-echoing-text/#findComment-1326401 Share on other sites More sharing options...
Muddy_Funster Posted March 12, 2012 Share Posted March 12, 2012 I don't understand why you are doing that, why not just code the info into the actual page you are on? Quote Link to comment https://forums.phpfreaks.com/topic/258737-please-help-echoing-text/#findComment-1326405 Share on other sites More sharing options...
cpd Posted March 12, 2012 Share Posted March 12, 2012 @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']; Quote Link to comment https://forums.phpfreaks.com/topic/258737-please-help-echoing-text/#findComment-1326408 Share on other sites More sharing options...
scootstah Posted March 12, 2012 Share Posted March 12, 2012 @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. Quote Link to comment https://forums.phpfreaks.com/topic/258737-please-help-echoing-text/#findComment-1326410 Share on other sites More sharing options...
Muddy_Funster Posted March 12, 2012 Share Posted March 12, 2012 @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. PS, unless they picked the wrong sex in the profile page - the OP would be a she, not a he. Quote Link to comment https://forums.phpfreaks.com/topic/258737-please-help-echoing-text/#findComment-1326418 Share on other sites More sharing options...
scootstah Posted March 12, 2012 Share Posted March 12, 2012 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. 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. Quote Link to comment https://forums.phpfreaks.com/topic/258737-please-help-echoing-text/#findComment-1326420 Share on other sites More sharing options...
lingo5 Posted March 12, 2012 Author Share Posted March 12, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/258737-please-help-echoing-text/#findComment-1326422 Share on other sites More sharing options...
cpd Posted March 12, 2012 Share Posted March 12, 2012 PS, unless they picked the wrong sex in the profile page - the OP would be a she, not a he. 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. Quote Link to comment https://forums.phpfreaks.com/topic/258737-please-help-echoing-text/#findComment-1326425 Share on other sites More sharing options...
cpd Posted March 12, 2012 Share Posted March 12, 2012 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 Using the $_SERVER variable is not "not safe". Its perfectly safe provided you use it in a safe manner. Quote Link to comment https://forums.phpfreaks.com/topic/258737-please-help-echoing-text/#findComment-1326426 Share on other sites More sharing options...
Muddy_Funster Posted March 12, 2012 Share Posted March 12, 2012 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. 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. @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 Quote Link to comment https://forums.phpfreaks.com/topic/258737-please-help-echoing-text/#findComment-1326427 Share on other sites More sharing options...
cpd Posted March 12, 2012 Share Posted March 12, 2012 I love your signature btw Muddy. Completely true! Quote Link to comment https://forums.phpfreaks.com/topic/258737-please-help-echoing-text/#findComment-1326429 Share on other sites More sharing options...
scootstah Posted March 12, 2012 Share Posted March 12, 2012 I did't know using $_SERVER was not safe....thanks for pointing that out. It's not $_SERVER that is unsafe, it is certain indexes of $_SERVER, such as PHP_SELF which can be manipulated by the client to pose a potential XSS attack. Quote Link to comment https://forums.phpfreaks.com/topic/258737-please-help-echoing-text/#findComment-1326610 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.