使用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/