简体   繁体   English

在PHP中使用include函数

[英]Using the include function in PHP

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. 据我了解,我应该将PHP文件放置在public_html以外的其他位置(出于安全目的),index.html的位置在/ home / user / public_html中。 I want to put my PHP files in the php folder. 我想将我的PHP文件放在php文件夹中。 I have tried using: 我试过使用:

include("home/user/php/file.php") or include("../php/file.php") . include("home/user/php/file.php")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: 我已经在ini文件中启用了这些命令的使用,并且我还尝试设置对文件夹的权限(读/写/执行)。有效的一件事是将带有“根”文件夹的子域设置为php文件夹。 ,但是我想这违背了将文件放在服务器上其他位置的目的,尤其是因为我正在使用一个对象来放置我的php,如下所示:

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

and under my java scripts i put : 在我的Java脚本下:

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

Thanks in advance for helping me. 在此先感谢您的帮助。

Just try the following code. 只需尝试以下代码。 see what it outputs. 看看它的输出。 basically u need to use: $_SERVER['DOCUMENT_ROOT'] to navigate to file instead typing path yourself. 基本上,您需要使用:$ _SERVER ['DOCUMENT_ROOT']导航到文件,而不要自己输入path。

//to get site url 
echo $site_url = "http://" . $_SERVER['SERVER_NAME'] . dirname($_SERVER['PHP_SELF']); 

//to the folder
echo $dir = $_SERVER['DOCUMENT_ROOT'].'/user/php/';

if (file_exists($dir) == false) {
echo  "Directory $dir not found!";
} else {
echo "directory found and we are ready to proceed.";
}

let me know your response, i ll help u further. 让我知道您的回应,我会进一步帮助您。

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. 通常,将相对路径与includerequire使用,即使用从您的URL调用的第一个被调用脚本所在的目录开始的相对路径。

Example: 例:

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

http://example.com/index.php http://example.com/index.php

public/index.php public / index.php

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

This should be easy to understand. 这应该很容易理解。

http://example.com/news.php http://example.com/news.php

public/news.php public / news.php

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

src/somescript.php src / somescript.php

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

So take care of the include in somescript.php . 因此,请注意somescript.php中的include。 It doesn't matter that it is in the src folder. 不在src文件夹中。 An include like include 'database.php' will fail, because it's the path of the news.php which matters. include 'database.php'这样的include 'database.php'将会失败,因为这是news.php的路径。


Edit (2018-04-04) 编辑(2018-04-04)

I forgot to mention. 我忘了提。 Consider to use require() / require_once() instead of include() / include_once() . 考虑使用require() / require_once()而不是include() / include_once()

Difference between require, include, require_once and include_once? require,include,require_once和include_once之间的区别?

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

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