php变量不带include()

时间:2018-01-14 21:53:52

标签: php

我试图将变量从userInfo.php传递到flips.php,但由于某种原因,变量不会通过。 当我在userInfo.php中echo "hello";时,我确实看到了#hello'在网站上,但当我这样做:

if (isset($variable)) {
 echo "it is set!";
} else {
 echo "nope!";
}

然后它返回'nope!'即使我在$variable中设置了userInfo.php

userInfo.php:

<?php
$variable = "hello everyone!";
echo "hello!";

flips.php:

<?php
include ('userInfo.php');

if (isset($variable)) {
 echo "it is set!";
} else {
 echo "nope!";
}

网站输出: hello!nope!

我做错了什么,flips.php没有看到$variable

编辑(我网站上的真实代码):

flips.php:

include ('../csgodonut/application/views/steamauth/userInfo.php');
if (isset($steamprofile["personaname"])) {
  echo "yes!";
} else {
  echo "nope..";
}

userInfo.php:

$steamprofile['personaname'] = "Smokey";
echo "test";

网站:www.csgodonut.com(您必须登录Steam以查看hello! nope..) flips.php位置:csgodonut/application/views/gebruiker/home/flips.php userInfo.php位置:csgodonut/application/views/steamauth/userInfo.php

2 个答案:

答案 0 :(得分:1)

文件系统

<path-to-views-dir>/steamauth/userInfo.php
<path-to-views-dir>/gebruiker/home/flips.php
<path-to-views-dir>/gebruiker/home/home.php

userInfo.php

<?php

$variable = "Hello everyone, I am the coolest variable!";

echo 'Hello from userInfo.php!<br/>';

flips.php

<?php

include __DIR__ . '/../../steamauth/userInfo.php';

if (isset($variable)) {
    echo 'Helo from flips.php. The variable is set.<br/>';
} else {
    echo 'Helo from flips.php. The variable is NOT set.<br/>';
}

home.php

<?php

include __DIR__ . '/flips.php';

error_reporting(E_ALL);
ini_set('display_errors', 1);

if (isset($variable)) {
    echo 'Hello from home.php. The variable is set.<br/>';
} else {
    echo 'Hello from home.php. The variable is NOT set.<br/>';
}

...或home.php(通过ajax获取flips.php内容)

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes" />
        <meta charset="UTF-8" />
        <!-- The above 3 meta tags must come first in the head -->

        <title>Demo</title>
        <script src="https://code.jquery.com/jquery-3.2.1.min.js" type="text/javascript"></script>

        <script type="text/javascript">
            $(document).ready(function () {
                $('#testButton').click(function (event) {
                    alert('testButton clicked!');

                    $.ajax({
                        method: 'post',
                        dataType: 'html',
                        url: 'flips.php',
                        data: {},
                        success: function (response, textStatus, jqXHR) {
                            $('#results').html(response);
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            $('#results').html(textStatus + '<br />' + errorThrown);
                        },
                        cmplete: function (jqXHR, textStatus) {
                            //...
                        }
                    });

                });
            });
        </script>

        <style type="text/css">
            body {
                padding: 30px;
            }

            button {
                padding: 5px 10px;
                background-color: #8daf15;
                color: #fff;
                border: none;
            }
        </style>
    </head>
    <body>

        <button type="button" id="testButton" name="testButton">
            Fetch some data
        </button>

        <br/><br/>

        <div id="results">
            Here comes the response data from the ajax request...
        </div>

    </body>
</html>

要做

在浏览器中,访问home.php页面。

答案 1 :(得分:0)

我只是通过将$ .get的url更改为我的控制器中的函数来实现它。 然后在该函数中我加载了flips.php并用它发送$ data(带有来自userInfo.php的所有变量的数组)。