简体   繁体   English

如何通过`include`传递参数?

[英]How to pass parameters with `include`?

I have a PHP script called customers.php that is passed a parameter via the URL, eg: 我有一个名为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. 我想运行我的customer.php脚本,并将我铺好的参数传递给它。 I am sure this is really simple, but just cannot get it to work. 我相信这确实很简单,但是无法正常工作。 I have tried: 我试过了:

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

But that does not work. 但这不起作用。 Nor does anything else that I have tried. 我也没有尝试过其他任何方法。 Please help me. 请帮我。

You can't add a query string (Something like ?x=yyy&vv=www) onto an include like that. 您不能将查询字符串(诸如?x = yyy&vv = www之类的内容)添加到此类包含中。 Fortunately your includes have access to all the variables before them, so if $_GET['employeeId'] is defined before you call: 幸运的是,您的include可以访问它们之前的所有变量,因此,如果在调用之前定义了$_GET['employeeId']

include 'customers.php';

Then customers.php will also have access to $_GET['employeeId']; 然后customer.php也将有权访问$_GET['employeeId'];

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

PHP includes are really includes; PHP包含确实包含; they attach piece of code from that location to this location. 他们将代码从该位置附加到该位置。

PaulPRO's answer most likely tells what you want to achieve, but also other variables would be usable in the included file. PaulPRO的答案很可能会告诉您要实现的目标,但是在包含的文件中也可以使用其他变量。 So if you define $foo = "bar"; 因此,如果您定义$foo = "bar"; in index.php and include layout.php after that line, you could call this $foo variable in that layout.php file. 在index.php中并在该行之后包含layout.php,您可以在该layout.php文件中调用此$foo变量。

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数组混淆,最终您只会感到困惑,然后开始将头打成墙。

You can pass the value through 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中,启动会话并将所有会话值都放入变量中,然后取消设置会话。

You can use $GLOBALS to solve this issue as well. 您也可以使用$GLOBALS解决此问题。

 $tit = "Hello";

 $GLOBALS["docTitle"] =  $tit;

 include ("my.php");

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 NOT very clean... 再次,它不是很干净...

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

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