php 如何使用`include`传递参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6805963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:18:03  来源:igfitidea点击:

How to pass parameters with `include`?

php

提问by Philip Sheard

I have a PHP script called customers.php that is passed a parameter via the URL, e.g.:

我有一个名为customers.php 的PHP 脚本,它通过URL 传递一个参数,例如:

http://www.mysite,com/customers.php?employeeId=1

http://www.mysite,com/customers.php?employeeId=1

Inside my customers.php script, I pick up the parameter thus:

在我的customers.php 脚本中,我选择了参数:

$employeeId = $_GET['employeeId'];

That works just fine. I also have an HTML file with a form that inputs the parameter, and runs another php script, viz:

这工作得很好。我还有一个 HTML 文件,其中包含一个输入参数的表单,并运行另一个 php 脚本,即:

<form method="post" action="listcustomers.php">
  Employee ID:&nbsp; <input name="employeeId" type="text"><br>
  <input type="submit" value="Show">
</form>

Then in my listcustomers.php script, I pick up the parameter so:

然后在我的 listcustomers.php 脚本中,我选择了这样的参数:

$employeeId = $_POST['employeeId'];

So far so good. But his is where I am stuck. I want to run my customers.php script, and pass it the parameter that I pave picked up in the form. I am sure this is really simple, but just cannot get it to work. I have tried:

到现在为止还挺好。但他是我被困的地方。我想运行我的customers.php 脚本,并将我在表单中选取的参数传递给它。我相信这真的很简单,但就是无法让它发挥作用。我试过了:

include "customers.php?employeeId=$employeeId&password=password";

But that does not work. Nor does anything else that I have tried. Please help me.

但这不起作用。我尝试过的其他任何事情也没有。请帮我。

回答by Paul

You can't add a query string (Something like ?x=yyy&vv=www) onto an include like that. Fortunately your includes have access to all the variables before them, so if $_GET['employeeId']is defined before you call:

您不能将查询字符串(类似于 ?x=yyy&vv=www)添加到这样的包含中。幸运的是,您的包含可以访问它们之前的所有变量,因此如果$_GET['employeeId']在您调用之前定义:

include 'customers.php';

Then customers.php will also have access to $_GET['employeeId'];

那么customers.php也将可以访问 $_GET['employeeId'];

回答by Prashant Singh

Your include file will have access to the GET variables directly.

您的包含文件将可以直接访问 GET 变量。

回答by Smar

PHP includes are really includes; they attach piece of code from that location to this location.

PHP 包含是真正的包含;他们将来自该位置的一段代码附加到该位置。

PaulPRO's answer most likely tells what you want to achieve, but also other variables would be usable in the included file. So if you define $foo = "bar";in index.php and include layout.php after that line, you could call this $foovariable in that layout.php file.

PaulPRO 的回答很可能会说明您想要实现的目标,但在包含的文件中也可以使用其他变量。因此,如果您$foo = "bar";在 index.php 中定义并在该行之后包含 layout.php,则可以$foo在该 layout.php 文件中调用此变量。

If you seriously want to pass an URL to the file, you can insert the URL into variable and in included file parse it with parse_url

如果你真的想将一个 URL 传递给文件,你可以将 URL 插入到变量中,并在包含的文件中使用parse_url解析它

EDIT: and yes, I do not suggest adding query params to actual include url as it starts to mess up with GET array and eventually you'll just be confused what there should be and start smacking your head into wall.

编辑:是的,我不建议将查询参数添加到实际包含的 url 中,因为它开始与 GET 数组混淆,最终你只会对应该有什么感到困惑,并开始把头撞到墙上。

回答by SUGU

You can pass the value through Session

您可以通过 Session 传递值

Like

喜欢

<?php
    $_SESSION['name']='peter';
    $_SESSION['age']=28;
    $_SESSION['name']='Peter';
    include('person_info.php');
?>

In person_info.php, start the session and put all session value in the variable and unset the session.

在 person_info.php 中,启动会话并将所有会话值放入变量中并取消设置会话。

回答by Dulith De Costa

You can use $GLOBALSto solve this issue as well.

您也可以使用它$GLOBALS来解决此问题。

 $tit = "Hello";

 $GLOBALS["docTitle"] =  $tit;

 include ("my.php");

回答by Richard Hymanson

This is not so clean but this will work if you need to enter a static parameter:

这不是那么干净,但是如果您需要输入静态参数,这将起作用:

if (true){
    echo "<meta http-equiv='refresh' content='0;url=query_source.php?random=red%20bull' />";
}
else
{
    echo "<meta http-equiv='refresh' content='0;url=query_source.php?random=blue%20bull' />";
}

Just put it within your source code for the page which calls the query_source.php.

只需将它放在调用 query_source.php 的页面的源代码中。

Once again, it is NOTvery clean...

再一次,它不是很干净......