在脚本之间传递 PHP 变量 [英] Passing PHP variables between scripts

查看:44
本文介绍了在脚本之间传递 PHP 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此有困难,所以我在谷歌上搜索并在 SO 上阅读.最后,我尝试了 W3 网站的 2 个小摘录,在 2 个文件 Z1.php 和 Z2.php 中,如下所示:

I'm having difficulty with this, So I Googled and also read here at SO. In the end, I tried 2 small excerpts from a W3 website, in 2 files Z1.php and Z2.php like this:

Z1

<?php
// Start the session
session_start();
include ('z2.php'); (I added this)

?>
<html>
<body>

<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
?>

</body>
</html> 

我在行中加入 Z2 - Z2 然后是:

I put in the line to include Z2 - Z2 is then:

<?php
session_start(); (note: I tried with and without this)
?>
<html>
<body>

<?php
// Echo session variables that were set on previous page
echo "Favorite color is " . $_SESSION["favcolor"] . ".<br>";
echo "Favorite animal is " . $_SESSION["favanimal"] . ".";
?>

</body>
</html> 

我把所有这些都放到 Zend 中,并得到了作为输出

I put all that into Zend, and got, as output,

最喜欢的颜色是.
最喜欢的动物是.

Favorite color is .
Favorite animal is .

我需要它来使我能够使用 phpqrcode,它说的是调用 QR 码:警告!它应该是脚本生成的第一个也是唯一的输出,否则其余的输出将落在 PNG 二进制文件中,肯定会破坏它"而且,可以肯定的是,确实如此.所以我认为 2 个脚本是可行的方法.

I need this to enable me to use phpqrcode, which says about calling the QR code:"WARNING! it should be FIRST and ONLY output generated by script, otherwise rest of output will land inside PNG binary, breaking it for sure" and, for sure, it does. So I thought 2 scripts would be the way to go.

我以前没有尝试过在脚本之间传递变量,所以欢迎在我的尝试中有一两个批判的眼光来理解这种方法并使我走上正确的道路.

I've not tried to pass variables between scripts before, so would welcome a critical eye or two on my trial attempts to understand this method and to set me on the right path.

保罗

推荐答案

您在这里面临的问题是您将 z2.php 脚本包含在第一个文件中.现在将 include 想象成复制并粘贴在第二个脚本中的相同 php 代码.你启动了你应该的 session 来掌握变量,但是你尝试在它们被创建之前 echo 它们.尝试在定义 Session 变量后包含 z2.php 并从那里查看它是如何工作的.

The problem you're facing here is that you're including the z2.php script in the first file. Now imagine include as just the same php code in the second script copied and pasted there. You start the session which you should, to get a hang of the variables, but you try to echo them before they're created. Try including the z2.php after you define your Session variables and see how it works from there.

简单来说,就是尽量去掉任何include.您从 W3Schools 获取了这个示例.只需像您一样创建两个单独的文件.调用 session_start() 并创建 session 变量并尝试显式访问另一个文件,看看您是否可以访问它们.

To put it simply, try to do away with any include. You took this example from W3Schools. Just create two separate files like you do. Call session_start() and create session variables and try to access the other file explicitly and see if you can access them.

来自文档 -

当一个文件被包含时,它包含的代码继承了包含发生所在行的变量范围.从那时起,调用文件中该行可用的任何变量都将在被调用文件中可用.但是,包含文件中定义的所有函数和类都具有全局作用域.

When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.

当您include一个脚本到另一个脚本中时,您并没有真正传递会话变量.事实上,你所做的一切都可以在不使用 Sessions 的情况下实现.

When you include a script within another script like you did, you aren't really passing session variables. As a matter of fact what you did can be achieved without using Sessions at all.

//foo.php
<?php
$color = 'green'; 
$fruit = 'apple';
?>

//bar.php
<?php
echo "A $color $fruit"; // A
include 'foo.php';
echo "A $color $fruit"; // A green apple
?>

这样的东西是完全合法的,在 PHP 中可以完美运行.如果您确实想检查当前设置中的 Session 变量,您可以在最后重定向到 z2.php 而不是使用 includez1.php 在文件末尾添加以下几行:

Something like this is totally legit and would work perfectly fine in PHP. If you actually want to check Session variables in your current setting, you could instead of using include do a redirect to z2.php at the end of z1.php by adding the following lines at the end of the file:

header("Location: z2.php");
die();

希望这能让您朝着正确的方向开始.

Hope this gets you started in the right direction.

这篇关于在脚本之间传递 PHP 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆