PHP pass variable to include

2019-01-07 17:25发布

I'm trying to pass a variable into an include file. My host changed PHP version and now whatever solution I try doesn't work.

I think I've tried every option I could find. I'm sure it's the simplest thing!

The variable needs to be set and evaluated from the calling first file (it's actually $_SERVER['PHP_SELF'], and needs to return the path of that file, not the included second.php).

OPTION ONE

In the first file:

global $variable;
$variable = "apple";
include('second.php');

In the second file:

echo $variable;

OPTION TWO

In the first file:

function passvariable(){
    $variable = "apple";
    return $variable;
}
passvariable();

OPTION THREE

$variable = "apple";
include "myfile.php?var=$variable"; // and I tried with http: and full site address too.


$variable = $_GET["var"]
echo $variable

None of these work for me. PHP version is 5.2.16.

What am I missing?

Thanks!

11条回答
男人必须洒脱
2楼-- · 2019-01-07 18:08

Do this:

$checksum = "my value"; 
header("Location: recordupdated.php?checksum=$checksum");
查看更多
爷的心禁止访问
3楼-- · 2019-01-07 18:10

According to php docs (see $_SERVER) $_SERVER['PHP_SELF'] is the "filename of the currently executing script".

The INCLUDE statement "includes and evaluates the specified" file and "the code it contains inherits the variable scope of the line on which the include occurs" (see INCLUDE).

I believe $_SERVER['PHP_SELF'] will return the filename of the 1st file, even when used by code in the 'second.php'.

I tested this with the following code and it works as expected ($phpSelf is the name of the first file).

// In the first.php file
// get the value of $_SERVER['PHP_SELF'] for the 1st file
$phpSelf = $_SERVER['PHP_SELF'];

// include the second file
// This slurps in the contents of second.php
include_once('second.php');

// execute $phpSelf = $_SERVER['PHP_SELF']; in the secod.php file
// echo the value of $_SERVER['PHP_SELF'] of fist file

echo $phpSelf;  // This echos the name of the First.php file.
查看更多
We Are One
4楼-- · 2019-01-07 18:17

Option 3 is impossible - you'd get the rendered output of the .php file, exactly as you would if you hit that url in your browser. If you got raw PHP code instead (as you'd like), then ALL of your site's source code would be exposed, which is generally not a good thing.

Option 2 doesn't make much sense - you'd be hiding the variable in a function, and be subject to PHP's variable scope. You'ld also have to have $var = passvariable() somewhere to get that 'inside' variable to the 'outside', and you're back to square one.

option 1 is the most practical. include() will basically slurp in the specified file and execute it right there, as if the code in the file was literally part of the parent page. It does look like a global variable, which most people here frown on, but by PHP's parsing semantics, these two are identical:

$x = 'foo';
include('bar.php');

and

$x = 'foo';
// contents of bar.php pasted here
查看更多
贪生不怕死
5楼-- · 2019-01-07 18:18

PHP 5.2.0 adds the allow-url-include directive (default is 0, must be changed to 1 in php.ini), which means you can pass a full http: URL to the include statement.

By adding your variable as a query string to this URL, you can pass a variable into the included file -- albeit with some loss of performance:

include 'http://www.myservername.com/includes/second.php?variable=apple';

Of course, you also have to set up second.php to process $_GET['variable'] and apply it to the output.

查看更多
祖国的老花朵
6楼-- · 2019-01-07 18:19

This worked for me: To wrap the contents of the second file into a function, as follows:

firstFile.php

<?php
    include("secondFile.php");

    echoFunction("message");

secondFile.php

<?php
    function echoFunction($variable)
    {
        echo $variable;
    }
查看更多
登录 后发表回答