尝试将 url 的 preg_replace 部分用作 php 变量

发布于 2024-10-18 20:05:25 字数 1010 浏览 3 评论 0原文

我们的网站在开头有这个调用来查找页面的名称(例如product.html,product = name)并向其添加“-tabs”以生成变量$block_name:

    <? $current_url = $_SERVER['REQUEST_URI']; // this will return everything after the http://www.domain.xx including preceding "/"
$block_name = preg_replace ("/^(?:.*)\/(.*).html$/", "$1-tabs" , $current_url); ?>

然后该变量用于稍后调用静态块这个:

    <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId($block_name)->toHtml() ?> 

如果有人访问 http://example.com/product.html ,它就可以正常工作,但是如果我使用任何 URL 跟踪,例如 Analytics,并且 URL 末尾有以下内容:

http://example.com/product.html?utm_source=newsletter&utm_campaign=product_launch&utm_medium=email

然后变量未正确创建,静态块未加载。

有没有办法忽略 .html 之后的任何内容,以便任何跟踪附加都不会影响静态块加载?

谢谢!

Our site has this call at the beginning to find the page's name (e.g. product.html, product = name) and adds '-tabs' to it to produce the variable $block_name:

    <? $current_url = $_SERVER['REQUEST_URI']; // this will return everything after the http://www.domain.xx including preceding "/"
$block_name = preg_replace ("/^(?:.*)\/(.*).html$/", "$1-tabs" , $current_url); ?>

The variable is then used to call a static block later with this:

    <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId($block_name)->toHtml() ?> 

It works fine if someone goes to http://example.com/product.html, but if I use any url tracking, like Analytics, and the url has this at the end:

http://example.com/product.html?utm_source=newsletter&utm_campaign=product_launch&utm_medium=email

Then the variable isn't created properly and the static block doesn't load.

Is there a way to ignore anything after the .html so that any tracking appending doesn't impact the static block loading?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凹づ凸ル 2024-10-25 20:05:26

preg_replace("/^(?:.*)\/(.*).html\??.*$/", "$1-tabs" , $current_url)

preg_replace("/^(?:.*)\/(.*).html\??.*$/", "$1-tabs" , $current_url)

知足的幸福 2024-10-25 20:05:26

我建议首先在该位置上使用 parse_url ,这将剥离获取变量。从那里,您可以按照当前的方式操纵它。

$pathOnly = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$block_name = preg_replace(...);

I would recommend using parse_url on the location first, which will strip off the get variables. From there, you can manipulate it as you currently are.

$pathOnly = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$block_name = preg_replace(...);
山人契 2024-10-25 20:05:26

由于您使用的是 Magento 并且似乎是在模板内编写代码,因此您可以执行以下操作:

$_product = Mage::registry('product');
if ($_product) $block_name = $_product->getUrlKey();

产品页面上的多个模板已经为您定义了 $_product,这使得它变得更加容易。

当以非 SEO 方式请求页面(例如“http://www.example.com/catalog/product/view/id/123/”)或 URL 中包含类别名称时,此方法有效。 $_SERVER 无法处理这些情况。使用 $_SERVER 中未经验证的值允许攻击者将值插入到您的代码中,这绝对是不好的。

Since you're using Magento and appear to be writing code inside a template you can do this:

$_product = Mage::registry('product');
if ($_product) $block_name = $_product->getUrlKey();

Several templates on the product page already define $_product for you which makes it even easier.

This way works when a page is requested the non-SEO way, such as "http://www.example.com/catalog/product/view/id/123/", or when a category name is in the URL. $_SERVER could not handle these cases. Using an un-validated value from $_SERVER allows an attacker to insert values into your code which is definitely bad.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文