dongzi0857 2018-04-03 13:24
浏览 456
已采纳

在PHP中使用include函数

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.

  • 写回答

2条回答 默认 最新

  • doujiao6507 2018-04-03 13:37
    关注

    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?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了