开发者

Test if on a certain page with php?

开发者 https://www.devze.com 2023-03-25 20:29 出处:网络
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

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 t开发者_JAVA百科itle.

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!


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.


<?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


You can use this variable to find out what page you're on:

$_SERVER['PHP_SELF']

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


read this: http://php.net/manual/en/language.constants.predefined.php


Any php page can include the following code, putting the applicable page title in the variable.

<?php

//Set Page Title for header template
$page_title = 'Welcome';

require_once("templates/header.php");

?>

In the header template:

<title><?php echo $pageTitle ?></title>

Any page called up will show your preferred title in the browser tab, and be usable as a variable on the specific page.

0

精彩评论

暂无评论...
验证码 换一张
取 消