Working with directories to make a require in PHP

Asked

Viewed 725 times

2

I have a page where I submit some information, stored in the Database. This page is inside 5 folders: pasta1/pasta2/pasta3/pasta4/pasta5.

To the SELECT function, I need a require to make the connection to the bank. This file is in a directory: /conexao/conexao.php.

How can I use the require (or other secure command) to perform this query?

  • seek to use absolute paths. One way to do this is to use $_SERVER['DOCUMENT_ROOT']

  • Thanks for the prompt reply. Have some reference, so I can read?

  • Try to define some directories - http://answall.com/questions/97525/get-direct%C3%B3rio-general-do-project-php/97545#97545

2 answers

2

If you’re using PHP on a webserver, surely there’s a variable that tells the root of the application (or the site) and it’s from that information that you should use your paths.

$_SERVER contains the relevant information from Webserver and one of them is DOCUMENT_ROOT.

DOCUMENT_ROOT points to the root of the application. Assuming your application is in the directory x/y/z (here does not matter the operating system), $_SERVER['DOCUMENT_ROOT'] point to x/y/z correctly no matter what level your PHP script is.

Eventually, for security reasons, it is customary to place files that should not have direct access through Webserver on a path outside this tree. For example: Many Frameworks use a directory structure like this

-./
  -> web
  -> src
  -> test

Where web will be the root of the application. In this case, to access the other directories should be used

ROOT . '/../src';

The PHP manual is the best language documentation I know. Always start your research there.

  • Thanks for the comment, @Marcos. I came to ask for help because in some Google searches they revealed that require was no longer used for such a situation. I will look at your references to try to resolve this issue. As soon as I can, put the result here. Great Hug!

  • I have to disagree. The best documentation is that of Python :)

  • 1

    @Olimonf. I don’t know the Python documentation :( . I compare the PHP with the ones I use, like Java, C#, C++ and Delphi. The best official in my understanding is PHP, but I promise to take a look at Python. :)

  • @Hebertrichard require, include, require_once, include_once are still widely used. Currently, for applications that use autoload, use the standards of PSR-4 (http://www.php-fig.org/psr/psr-4/) but at the end of the day, you end up using require (https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md)

  • The documentation of php in Portuguese is quite flawed :p - http://answall.com/q/66277/3635 - but the in English is well organized and updated :D

1

An alternative way to another answer would be simply in each file you set the relative path.

Assuming the folder structure is:

+-- ./
    +-- conexao
        +-- conexao.php
    +-- pasta1
        +-- exemplo1.php
            +-- pasta2
                +-- exemplo2.php
                    +-- pasta3
                        +-- exemplo3.php

In the archive pasta1/exemplo1.php:

<?php
require '../conexao/conexao.php';

In the archive pasta1/pasta2/exemplo2.php:

<?php
require '../../conexao/conexao.php';

In the archive pasta1/pasta2/pasta3/exemplo3.php:

<?php
require '../../../conexao/conexao.php';

You can also create an index.php file along with . htaccess and it would access any file.

The file should look something like . htaccess:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteRule (.*) index.php/$1 [QSA,L]
</IfModule>

And the index.php should look like this:

<?php
require 'conexao/conexao.php';

$pathInfo = isset($_SERVER['PATH_INFO']) $_SERVER['PATH_INFO'] : '';
$pathInfo = ltrim($pathInfo, '/');

if (empty($pathInfo)) {
   echo 'Index normal';
} else if (is_file($pathInfo)) {
   require $pathInfo;
} else {
   header('X-PHP-Response-Code: 404', true, 404);
   echo 'Página não encontrada';
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.