Welcome to WebmasterWorld Guest from 54.224.113.125

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Are checking $ SERVER variables server intensive?

How inefficient is this code?

     
8:42 pm on Aug 8, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Jan 23, 2007
posts: 48
votes: 0


I have a sidebar on each page of my site that needs to be wider or narrower depending on where you are on my site. Currently, I have this line of code in the header of each page:

if ($_SERVER[SCRIPT_NAME] == "/index.php")
$width = $homepagewidth;
else if (strstr($_SERVER['SCRIPT_NAME'],"forum"))
$width = $forumwidth;
else
$width = $restofsitewidth;

Is it inefficient to check the SCRIPT_NAME variable on each and every page?

Thanks.
11:36 am on Aug 9, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 0


As far as I'm aware, checking the $_SERVER superglobal is just like checking any other variable so in that sense I don't think you can really get anymore efficient.
11:40 am on Aug 9, 2011 (gmt 0)

Senior Member from GB 

WebmasterWorld Senior Member brotherhood_of_lan is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 30, 2002
posts:4843
votes: 2


The variables are already present and as penders says, it's just like calling regular variables.

You might be able to optimize the if/else statement though. If you found that 'forum' was getting accessed more frequently than '/index.php' you could put 'forum' at the top of the if/else statement.
10:46 pm on Aug 9, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Jan 23, 2007
posts: 48
votes: 0


thank you both!