Conditional loading of script

Hi,
Can someone please explain why it is that when I load the following page: [I]http://www.gordondesign.com.au/brochures.php[/I] which contains the php code block below, the script appears to operate in reverse? I understand the function $_SERVER[‘PHP_SELF’]; retrieves the current page URL, so why then, if the statement evaluates to false, assuming $_SERVER[‘PHP_SELF’]; is retrieving the current page URL does the script throw ‘true’. (Please see upper left corner of screen at [I]http://www.gordondesign.com.au/brochures.php[/I] for script generated string.)
Any help appreciated.
Colin

	<?php

                        $this_page = $_SERVER['PHP_SELF'];
						
                        if ($this_page != 'http://www.gordondesign.com.au/brochures.php')
						 {
                                		echo 'true';
                                }
								else {
									echo 'false';
								}
                         ?>

You can get the answer yourself by trying this:


 <?php
                        
                        $this_page = $_SERVER['PHP_SELF'];

                        echo "This page is: " . $this_page;

                        if ($this_page != 'http://www.gordondesign.com.au/brochures.php')
                         {
                                        echo 'true';
                                }
                                else {
                                    echo 'false';
                                }
                         ?>

Thanks so much for your quick reply. I figured out that because $_SERVER[‘PHP_SELF’] returned ‘/brochures.php’ the script evaluated to false because it didn’t match the absolute URL [B][I]http://www.gordondesign.com.au/brochures.php[/I][/B] I was using in the php block.
Cheers again - have a good day!

Hi,
I’ve just noticed while this script works on my live (non database) site it does’nt work on my local server for my wordpress site.
The script throws up This page is: /index.php - false on every page.
Any ideas how I can get $_SERVER[‘PHP_SELF’] to give me the correct rendered page URL?

Hi Argent,

You probably want to check against the value of $_SERVER[‘REQUEST_URI’]

Ah ha! thank you. That works I get the correct page ID now. I guess $_SERVER[‘REQUEST_URI’] is a specific request for the generated page ID as opposed to $_SERVER[‘PHP_SELF’] being a generic type of call?

PS: are you a guitarist by any chance fretburner?

You can find more info about the various $_SERVER variables available in the PHP docs, but basically $_SERVER['PHP_SELF'] gives you the filename of the currently running script. With apps such as WordPress that route all their URLs through a central script, this value won’t necessarily match the URL that you use to access that page, whereas $_SERVER['REQUEST_URI'] will give you the actual URL that was requested.

I am :slight_smile: although I don’t play as much these days as I used to.

Try


echo basename(__FILE__);