简体   繁体   English

如何通过PHP获取Page body中的URL参数?

[英]How to get URL argument in Page body by PHP?

On a Drupal site, PHP code is enabled for Page body content. 在Drupal站点上,为页面正文内容启用了PHP代码。 How can I get the argument and its value in PHP code? 如何在PHP代码中获取参数及其值? For example, I'd like to get ref and 33002 from: 例如,我想得到ref33002来自:

http://example.com/node/1?ref=33002

In the following code: 在以下代码中:

<?php 
  print arg(0);
  print arg(1);
  print arg(2);
  print arg(3);
?>

I can get node and 1 , but nothing about ref or 33002 . 我可以得到node1 ,但没有关于ref33002

Thanks! 谢谢!

You can use drupal_get_query_parameters() as follows: 您可以使用drupal_get_query_parameters() ,如下所示:

$params = drupal_get_query_parameters();

if (isset($params['ref']) && is_numeric($params['ref'])) {
  var_dump(check_plain($params['ref']));
}

Use this: 用这个:

<?php
$a=$_REQUEST['ref'];
echo "The value of the ref parameter is ".$a;
?>

The solution by crowicked works, but "the Drupal way" is to pass the ref value as a url argument, ie: crowicked的解决方案有效,但“Drupal方式”是将ref值作为url参数传递,即:

http://example.com/node/1/33002

Now you can access the ref value using the arg() function: 现在您可以使用arg()函数访问ref值:

$ref = arg(2);

Of course an approach like this can only work if the ref value is always the third argument. 当然,只有当ref值始终是第三个参数时,这样的方法才有效。

Even though the code above works, it is not recommended to place php scripts in the node body. 即使上面的代码有效,也不建议将php脚本放在节点体中。 It makes your site harder to maintain and debug. 它使您的网站难以维护和调试。 The day will come when an editor deletes your php node by accident, thus breaking your site. 当编辑器意外删除您的php节点时,将会破坏您的网站。

If you have a php script that you want to run, the best way is to add a simple custom module to your site which implements hook_menu. 如果你有一个想要运行的php脚本,最好的方法是在你的站点上添加一个简单的自定义模块来实现hook_menu。 Have a look at the Menu Example module or this hello world module to learn more about hook_menu. 查看Menu Example模块或此hello world模块以了解有关hook_menu的更多信息。

Lastly, regardless of the method you choose (php nodes or a custom module), always make sure to sanitize url input, for instance with check_plain() . 最后,无论您选择哪种方法(php节点或自定义模块),都要确保清理url输入,例如使用check_plain()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM