如何在网站的某些URL上显示PHP代码


How to Display a PHP Code on Certain URL of a Website

我想在某个URL上显示PHP代码,以在该页面上触发一些CSS代码。

我试过这段代码,但它不起作用

<?php 
if($url == 'test.com/local/memebership') 
{
echo $this->__('<style>label[for=product_id---1] {display: none;}</style>');
}
    ?>    

谢谢

你需要输入以下代码:

if ($_SERVER['REQUEST_URI'] == '/local/memebership') {
    // Your code goes here. 
    echo '<style>label[for=product_id---1] {display: none;}</style>';
    // If you need to echo content from any file
    // passthru('/path/to/file.css'); 
    // Exit right after echoing
    die(); 
}

此外,如果它在带有标记的页面上包含代码<link>则需要添加内容类型的标题,如下所示:

header ('Content-Type: text/css');

在这种情况下,您需要更改代码并删除标记<style>

简单地说,你必须验证当前的URI。

if (preg_match("/^('/)?local'/membership/i", $_SERVER["REQUEST_URI"])) // or $url
{ 
    echo "We are somewhere below local/membership"; 
}

要显示代码,您可以使用如下代码:

function displayCode($str)
{
 return "<pre>".htmlentities($str)."</pre>"; //use <code> if needed
}

要检测服务器,请使用 $_SERVER @Nathan,@Daniel 为您提供了一个检测一个 URL 的示例,如果要检测服务器,请使用 $_SERVER['SERVER_NAME']

签出数组,$_SERVER

要获取请求的页面,请使用 $_SERVER['REQUEST_URI']

所以你的脚本可以像

<?php
$url = '/local/memebership';
if($_SERVER['REQUEST_URI'] == $url)
{
  echo 'something';
}
?>