Error using require_once

2

My problem is the following one I have an index.php file which has a login that sends the information "controller / login.php" which has inside these two require_once

require_once 'conx/Db.php';
require_once '../libs/Security.php';

the root of the files is as follows:

index.php
dashboard.php
controller/
|---->conx/Db.php
|---->login.php
libs/
|---->Security.php

What happens, when I login in the system, it enters perfectly but when I get to dashboad.php , it sends me this error:

Warning: require_once(../libs/Security.php) [function.require-once]: failed to open stream: No such file or directory in C:\xampp\htdocs\internacionalPeritaciones\controller\login.php on line 4

What I do not understand is that before entering dashboard.php it works perfectly but when I login it sends me that error and both index and dashboard files are in the same folder. I tried using:

require_once(dirname(__FILE__) .  '/libs/Security.php';
------ por separado
$root = $_SERVER['DOCUMENT_ROOT'];
require_once $root . '/libs/Security.php';

None of those worked for me, I'm really a bit lost. They are grateful for the help.

    
asked by Jorge D. Jesus 09.09.2016 в 07:33
source

2 answers

2

The error this:

Warning: require_once(../libs/Security.php) [function.require-once]: failed to open stream: No such file or directory in C:\xampp\htdocs\internacionalPeritaciones\controller\login.php on line 4

It is because it does not find the file in the indicated route. If you have the following route:

index.php
dashboard.php
controller/
|---->conx/Db.php
|---->login.php
libs/
|---->Security.php

And you are in dashboard, to access libs , you can not put ../ before the route, since you would be going back one level in the folders, to access libs from dashboard , you would have to put it like this:

require_once 'libs/Security.php';

or so:

require_once '/libs/Security.php';

I hope it serves you

    
answered by 09.09.2016 в 08:12
2

In theory , to avoid problems, it is better to use absolute paths. They adapt better to any implementation.

Taking into account the absolute path of your system up to root of the application:

C:\xampp\htdocs\internacionalPeritaciones

You should try the following:

require_once __DIR__ . '/conx/Db.php';
require_once dirname( __DIR__ ) . '/libs/Security.php';

__DIR__ will always point (in this script and within this structure) to C:\xampp\htdocs\internacionalPeritaciones\controller

In Windows I do not remember how the routes with normal or inverted bars were armed.

So you only have to finish linking the script from that point:

/conx/Db.php

And dirname( __DIR__ ) points to a less directory, it's the same as doing __DIR__ . '/../libs/Security.php' .

So it would leave us in root C:\xampp\htdocs\internacionalPeritaciones .

Doing the same as in the previous point, just link the other script:

/libs/Security.php

This should not give you problems, even if you change the environment. But yes, logically, if you change the current structure of root .

  

As a note, both __DIR__ and dirname() end the routes without the / at the end. So both to advance in the directories /dir/... , and to leave them /../dir/... you have to arm the route starting with a bar / .

If I did not make a mistake in writing it, I should solve the problem.

Otherwise, give us more information about how you send the info to login.php and shows the critical parts of the code that intervene to see better what may be happening.

    
answered by 09.09.2016 в 10:34