php - 测试是否在某个页面上用php?

标签 php

使用PHP,有没有办法测试浏览器是否正在访问某个页面?

例如,我有一个名为 header.php 的头文件,它被拉入几个不同的页面。我想做的是当我转到另一个页面时,我想将某些变量附加到标题。

例子。

header.php 内部:

<?php 

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

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

编辑:如果您认为有更简单的方法可以做到这一点,请告诉我!

最佳答案

您可以使用 $_SERVER['REQUEST_URI']$_SERVER['PHP_SELF']__FILE__,具体取决于您的版本PHP 以及您如何设置代码。如果您在一个框架中,它可能有一个对开发人员更友好的功能可用。例如,CodeIgniter 有一个名为 current_url()

的函数

根据 PHP 文档:

$_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 - 测试是否在某个页面上用php?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6931101/

相关文章:

用于创建动态行并插入数据库的php代码

php - 基于数据库条目的下拉选择

javascript - Node 中的动态函数调用(无框架)

php - 如何确认浏览器是否安装了Firebug?

php - Magento - 在顶级类别菜单名称旁边放置图像

JavaScript 检查值是否存在然后提交表单

PHP 文件中的 Javascript 输出 JSON

php - 如何在图片下方添加说明?

phpmyadmin http 500 error.. No such file or directory in/usr/share/php/Symfony/Component/Cache/autoload.php 第 6 行

php - 如何使 Laravel DB 查询结果缓存到变量以处理同时相同的 SQL 查询以减少相同 SQL 查询的数量