Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-11-2013, 04:02 PM   PM User | #1
low tech
Regular Coder

 
low tech's Avatar
 
Join Date: Dec 2009
Posts: 740
Thanks: 149
Thanked 67 Times in 67 Posts
low tech is on a distinguished road
is php_self still ok?

Hi guys

I'm having a go at building a basic php navigation for my site and basically it's based on this

PHP Code:
basename($_SERVER["PHP_SELF"]) 
It's working, but a thought came to my head --- is this code reliable?

ie is this code still used? Is there some other newer way I should be using?

I just need file name to determine navigation

LT
__________________
Ask not what can I do for myself, but what can I do for others

"The greatest revenge is to accomplish what others say you cannot do."
~ Unknown
low tech is offline   Reply With Quote
Old 02-11-2013, 04:05 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Depends on what you are using it for.
That will work for the accessed page, but not for inclusions. So the running script is PHP_SELF. You can probably save effort by using SCRIPT_NAME instead, but if you want to operate on a specific file regardless of inclusion levels, use the __FILE__ magic constant.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 02-11-2013, 04:22 PM   PM User | #3
low tech
Regular Coder

 
low tech's Avatar
 
Join Date: Dec 2009
Posts: 740
Thanks: 149
Thanked 67 Times in 67 Posts
low tech is on a distinguished road
Hi Fou-Lu

Thanks for reply

Yeh navigation is in an include.

so I should use this right

PHP Code:
basename($_SERVER["SCRIPT_NAME"]); 
I grab the filename and echo nav content based on that -- just if else stuff

It's working on localhost, but I don't want to do it all only to find it's not reliable on server.

My script hinges on that one line really.

I don't know how to use the __FILE__ magic constant.

atm my include looks like this at start

PHP Code:
$currentFile basename($_SERVER["PHP_SELF"]);
$parts Explode('/'$currentFile);
$show $parts[count($parts) - 1]; 
with rest of nav based on $show

LT
__________________
Ask not what can I do for myself, but what can I do for others

"The greatest revenge is to accomplish what others say you cannot do."
~ Unknown
low tech is offline   Reply With Quote
Old 02-11-2013, 04:58 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
SCRIPT_NAME should always be available in a web environment.
I'm not really sure what you are doing here. The $currentFile created using the basename will just be myscript.php, without any path, so the parts and show variables don't have any value.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
low tech (02-11-2013)
Old 02-11-2013, 11:19 PM   PM User | #5
low tech
Regular Coder

 
low tech's Avatar
 
Join Date: Dec 2009
Posts: 740
Thanks: 149
Thanked 67 Times in 67 Posts
low tech is on a distinguished road
Hi Fou-Lu

Quote:
The $currentFile created using the basename will just be myscript.php,
Yeh, I saw that eventually. I've changed it now and removed the code that was being used to parse php_self.

Just learning as I go:-)

Thanks for yr help

LT
__________________
Ask not what can I do for myself, but what can I do for others

"The greatest revenge is to accomplish what others say you cannot do."
~ Unknown
low tech is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:36 AM.


Advertisement
Log in to turn off these ads.