Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if on a certain page with php?

Tags:

php

Using PHP, is there a way to test if the browser is accessing a certain page?

For example, I have a header file called header.php which is being pulled into a couple different pages. What I want to do is when I go to a different page, I want to append certain variable to the title.

Example.

Inside header.php:

<?php 

$titleA = " Online Instruction";
$title B = "Offline";
?>

<h2>Copyright Info: <?php if ('onlineinstruction'.php) echo $titleA; ?> </h2> 

edit: also if you believe there is a simpler way to do this, let me know!

like image 771
tehman Avatar asked Dec 03 '22 07:12

tehman


2 Answers

You can use $_SERVER['REQUEST_URI'], $_SERVER['PHP_SELF'], or __FILE__ depending on your version of PHP and how you have your code setup. If you are in a framework it may have a much more developer-friendly function available. For example, CodeIgniter has a function called current_url()

Per PHP Docs:

$_SERVER['REQUEST_URI']: The URI which was given in order to access this page; for instance, '/index.html'.

$_SERVER['PHP_SELF']: The filename of the currently executing script, relative to the document root. For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/test.php/foo.bar would be /test.php/foo.bar. The __ FILE__ constant contains the full path and filename of the current (i.e. included) file. If PHP is running as a command-line processor this variable contains the script name since PHP 4.3.0. Previously it was not available.

like image 72
AlienWebguy Avatar answered Jan 11 '23 22:01

AlienWebguy


<?php

$url = $_SERVER["REQUEST_URI"]; 
$pos = strrpos($url, "hello.php"); 

if($pos != false) {
    echo "found it at " . $pos; 
}


?> 

http://www.php.net/manual/en/reserved.variables.server.php

http://php.net/manual/en/function.strrpos.php

like image 38
marko Avatar answered Jan 12 '23 00:01

marko