如何在包含文件的同时在 php 中声明变量?

发布于 2024-11-08 19:39:54 字数 545 浏览 0 评论 0原文

所以我有三个文件。

文件 1 包括文件 2,文件 2 包括文件 3

文件 1 需要打印在文件 3 中定义的 VAR 1

我该怎么做? 它没有对我回显

文件 1

<?php
if ($_GET["pg"]==false)
echo "<title>Socal Mods</title>";
else
echo $title_name;
?>

文件 2

<?php 
if ($_GET["pg"]==false)
include("home.php");
else
include("".$_GET["pg"].".php");
?>

文件 3

<?php $title_name="<title>Socal Mods</title>" ?>

文件 3 正在被解析为文件 1(即显示容器),但 %title_name 没有回显

So i have three files.

FILE 1 includes FILE 2 which includes FILE 3

FILE 1 needs to print VAR 1 which is defined in FILE 3

how would I do that?
Its not echoing out for me

file 1

<?php
if ($_GET["pg"]==false)
echo "<title>Socal Mods</title>";
else
echo $title_name;
?>

file 2

<?php 
if ($_GET["pg"]==false)
include("home.php");
else
include("".$_GET["pg"].".php");
?>

file 3

<?php $title_name="<title>Socal Mods</title>" ?>

file 3 is being parsed into file 1 which is the display container, but the %title_name is not echoing

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

残花月 2024-11-15 19:39:55

首先,关于如何访问 GET 变量。您可以使用 $_GET["pg"] == false 检查该值。请注意,在很多情况下,此表达式将无法达到您的预期。事实上,该值永远不会直接为 false。如果它为空或未设置,则它等于 false 的唯一方法(在这种情况下,您还会收到编译器警告,您应该避免这种情况)。通常,检查该值是否已设置的更安全方法是使用 isset( $_GET["pg"] ) 。

我要解决的下一件事是您引入的安全问题。您可以直接使用 GET 值来包含具有该名称的文件。如果我是一个有恶意的用户,我可以轻松地将 pg 值设置为您通常不会想到的值,这会以某种方式破坏您的网站。您通常应该避免使用用户以某种方式输入的数据(请求参数是用户数据),并确保首先清理它们。当您计划使用该值作为要包含的页面的基础时,一个好方法是拥有某种可接受/允许值的白名单。然后您可以检查输入的数据是否在该白名单中,如果是,请包含正确的页面。另一种简单的方法是使用 switch 语句来简单地遍历所有接受的案例。

现在最后,解决你的问题:我不确定这是否只是你发布代码时的一个错误,但文件 1 缺少文件 2 的包含。因此,你永远不会包含文件 3,当然变量将永远不会被设置。

另一个问题可能是 GET 值的使用。如果该值不包含准确文件名(如大小写且没有额外的空格),则将找不到该文件。最好回显您要包含在文件 2 中的文件名,只是为了检查是否犯了任何错误。如上所述的白名单是确保您尝试包含正确文件的另一种方法。

最后,您应该在服务器上启用错误报告,您可以在服务器配置中执行此操作,或者通过将以下行添加到第一个文件(即文件 1)的顶部:

error_reporting( E_ALL );

这样您将收到错误和警告告诉您运行时是否发生了意外情况,您可能会更容易发现错误。

旧答案

一般来说,它的工作原理如下:

文件 1:

 <?php
 include 'file2.php';
 echo $myVariable; // prints 'Hello World!'
 ?>

文件 2:

 <?php
 include 'file3.php';
 ?>

文件 3:

 <?php
 $myVariable = 'Hello World!';
 ?>

One thing first about how you access the GET variables. You check the value using $_GET["pg"] == false. Note that this expression will fail to do what you expect in a lot situations. In fact, the value will never be directly false. The only way it will equal to false if it is empty or unset (in which case you will also get a compiler warning, which you should avoid). Usually a safer way to check if the value was set is using isset( $_GET["pg"] ).

The next thing I want to address is a security problem you introduce. You use the GET value directly to include a file with that name. If I was a user with malicious intent, I could easily set the pg value to something, you usually wouldn't expect and which break your website in some way. You generally should avoid using data the user entered somehow (request parameters are user data), and make sure to sanitize them first. A good way to do this, when you plan to use the value as a base to which page you want to include, would be to have some kind of whitelist of acceptable/allowed values. Then you can check if the entered data is in that whitelist and if that is the case, include the correct page. Another simple way would be using a switch statement to simply go through all accepted cases.

Now finally, onto your problem: I'm not sure if this was just a mistake when you posted the code, but file 1 is missing the include of file 2. As such you will never include file 3, and of course the variable will never be set.

Another problem might be the usage of the GET value. If the value does not contain the exact filename (as in casing and no extra whitespace), then the file won't be found. It is a good idea to echo out the filename you want to include in file 2, just to check if you are making any mistakes. The whitelist as explained above would be another way to make sure that you are trying to include the correct file.

Finally, you should enable error reporting on your server, you can do that either in your server configuration, or by adding the following line to the top of your first file (i.e. file 1):

error_reporting( E_ALL );

That way you will get errors and warnings that will tell you if something unexpected happened at runtime, and you might see your mistake easier.

Old answer

In general it works like this:

File 1:

 <?php
 include 'file2.php';
 echo $myVariable; // prints 'Hello World!'
 ?>

File 2:

 <?php
 include 'file3.php';
 ?>

File 3:

 <?php
 $myVariable = 'Hello World!';
 ?>
中性美 2024-11-15 19:39:55
echo    $title_name = "<title>Socal Mods</title>";

这意味着它的回显 html,因此“Socal Mods”将出现在标题栏中而不是正文中。我希望您正在查看标题栏。要在文件 1 中使用 $title_name(在文件 3 中可用),您必须将 file3 包含在 file1 中。

echo    $title_name = "<title>Socal Mods</title>";

It means its echoing html, So "Socal Mods" will be seen in title bar instead of the body. I hope you are looking in the titlebar. And to use $title_name in file 1 which is available in file 3 you hv to include file3 in file1.

暗恋未遂 2024-11-15 19:39:55

文件3包含在脚本中吗?您是否确实验证并确认了该文件已包含在内?如果将脚本中的 include 语句更改为 require 语句,如果您要查找的文件不存在,PHP 将停止并出现错误。

找不到文件的原因有很多。如果文件名包含大写字母而请求仅包含小写字母,并且服务器使用区分大小写的文件系统,则会遇到问题。

此外,在使用用户输入包含文件之前,您应该清理用户输入。您不希望我能够在您的服务器上的任何位置包含任何文件,是吗?

Is file 3 included in the script? And have you actually validated and confirmed that the file is included? If you change the include statements in your script into require statements, PHP will stop with an error if the file you are looking for does not exist.

There are many reasons why a file might not be found. If the file name contains uppercase letters and the request only contains lowercase letters, you will run into problems if the server uses a case sensitive file system.

Also you should sanitize user input before you use that to include files. You don't want me to be able to include any file anywhere on your server, do you?

坠似风落 2024-11-15 19:39:55

必须先创建变量、设置变量并为其分配值,然后才能回显变量。

最有可能(请向我们展示一些代码),您的问题不是文件包含的问题,而是您试图在为变量赋值之前回显变量。

Variabes have to be created, set and assigned a value before they can be echoed.

Most probably ( please show us some code ), your problem is not the file includes, but you are trying to echo your variable before it is assigned a value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文