Can you use $_GET variables when including a file in PHP? Is it possible, even from an AJAX call?

陌路散爱 提交于 2019-12-01 20:54:44

You can handle the variables within your included script, rather than appending them onto the include path itself:

$var = "foo";
include("script.php");

-- contents of script.php --

echo $var; // 'foo'

In my opinion this is much cleaner anyway. You may desire to check to make sure the values exist within script.php before echoing out any variables.

There are, at least, two alternatives to solve this:

1)


if($_GET['farm']) {
    $var = $_GET['farm'];
    $farm = $var;
    include("ajaxInclude.php");
}

Then, in ajaxInclude.php:


if($farm) {
    $var = $farm;
    echo $var;
}

2)


if($_GET['farm']) {
    $var = $_GET['farm'];
    header("Location: ajaxInclude.php?farm={$var}");
}

$_GET is a global variable, so can be used anywhere within your php script, including any included scripts

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!