简体   繁体   English

如何通过http协议从根目录访问脚本?

[英]How can I access a script out of root by http protocol?

I have this folder-structure: 我有这个文件夹结构:

\out
    \script.php
\root
    \include
        \calculator.php

I need that script into \\calculator.php . 我需要将该脚本放入\\calculator.php How can I include it? 如何包含它?

// calculator.php
require( what path ? );
 ..

Note: I can include it by this path: ../out/script.php . 注意:我可以通过以下路径包括它: ../out/script.php But I also need to pass a argument to script.php . 但是我还需要将参数传递给script.php So I want something like this: 所以我想要这样的事情:

.. what path/script.php?arg=value

And as you know, because I need to pass a argument to that, so I have to use http protocol. 如您所知,因为我需要传递一个参数,所以我必须使用http协议。 All I want to know, How can I use both http and ../ ? 我只想知道,如何同时使用http../ Something like this: 像这样:

http://../out/script.php?arg=value

You don't need to pass parameters when you include the script because it's loaded into the same scope as the first script. 包含脚本时不需要传递参数,因为它被加载到与第一个脚本相同的作用域中。 This means that the script ../out/script.php has access to the query string arguments through the $_GET variable: 这意味着脚本../out/script.php可以通过$ _GET变量访问查询字符串参数:

$_GET['arg']

Also, any variables you define in calculator will be available in script.php 另外,您在计算器中定义的任何变量都将在script.php中提供

You could instantiate some variables inside the $_GET superglobal before you include your file, ie 您可以在包含文件之前实例化$_GET超全局变量中的一些变量,即

$_GET['arg'] = "value";
require('../../out/script.php');

But that isn't ideal, in my opinion. 但这在我看来并不理想。

If I were you I'd use a boring 'ol global variable (assuming you start in the global scope): 如果您是我,我会使用无聊的'ol全局变量(假设您从全局范围开始):

$arg = 'value';
require('../../out/script.php');

Then simply use it like any other variable (maybe with some sanity checking, though): 然后像其他变量一样简单地使用它(尽管可能需要进行一些检查):

if (!isset($arg)) {
    die('Oh my! $arg is not set! Error! Danger!');
}

do_something_with($arg);

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

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