简体   繁体   中英

Passing PHP variables between scripts

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> 

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> 

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

Favorite color is .
Favorite animal is .

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.

Paul

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.

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.

From the documentation -

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.

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
?>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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