在 PHP 中使用 include 函数 [英] Using the include function in PHP

查看:32
本文介绍了在 PHP 中使用 include 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我正在开发电子商务风格的网站,并且我拥有一个主机/域.根据我的理解,我应该将我的 PHP 文件放在 public_html 以外的另一个位置(出于安全目的),index.html 的位置在/home/user/public_html 中.我想把我的 PHP 文件放在 php 文件夹中.我试过使用:

include("home/user/php/file.php")或者include("../php/file.php").

我已经在 ini 文件中启用了这些命令的使用,并且我还尝试设置文件夹的权限(读/写/执行).正在工作的一件事是将带有根"文件夹的子域设置为php 文件夹,但我想这违背了将文件放在服务器上其他地方的目的,特别是因为我正在使用一个对象来放置我的 php,如下所示:

在我的java脚本下,我放了:

预先感谢您帮助我.

解决方案

通常使用带有 includerequire 的相对路径意味着使用从你第一次调用的目录开始的相对路径定位 URL 调用的脚本.

示例:

应用源文件数据库.php一些脚本.php公共(文档根)索引.php新闻.php

http://example.com/index.php

public/index.php

这应该很容易理解.

http://example.com/news.php

public/news.php

src/somescript.php

所以要注意 somescript.php 中的包含.它在 src 文件夹中并不重要.像 include 'database.php' 这样的包含会失败,因为 news.php 的路径很重要.

<小时>

编辑 (2018-04-04)

忘记说了.考虑使用 require()/require_once() 而不是 include()/include_once().>

require、include、require_once 和 include_once 之间的区别?

So, i am working on an eCommerce style of website and i own a host/domain. From my understanding, i should place my PHP files in another location than public_html(for security purposes), the location of index.html being in /home/user/public_html. I want to put my PHP files in the php folder. I have tried using:

include("home/user/php/file.php") or include("../php/file.php").

I have enabled the use of these commands in the ini file and i also tried to set permissions to the folders(read/write/execute).One thing that was working is to set a subdomain with its "root" folder to the php folder, but i guess that defeats the purpose of having the files somewhere else on the server, especially because i was using an object to place my php like so:

<object class="php-script" data="database.php"></object>

and under my java scripts i put :

<?php
    include("/home/user/php/database.php");
?>

Thanks in advance for helping me.

解决方案

Normally using relative paths with include or require means using relative paths starting from the directory your first called script of an URL call is located.

Example:

application
    src
        database.php
        somescript.php
    public (document root)
        index.php
        news.php

http://example.com/index.php

public/index.php

<?php
include '../src/database.php'

This should be easy to understand.

http://example.com/news.php

public/news.php

<?php
include '../src/somescript.php';

src/somescript.php

<?php
include '../src/database.php';

So take care of the include in somescript.php. It doesn't matter that it is in the src folder. An include like include 'database.php' will fail, because it's the path of the news.php which matters.


Edit (2018-04-04)

I forgot to mention. Consider to use require() / require_once() instead of include() / include_once().

Difference between require, include, require_once and include_once?

这篇关于在 PHP 中使用 include 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆