require_once()

I have the below code which basically goes to a file which connects to my mysql database.

require_once('Connections/conn_xxx.php');

How this code is within a template and I want folder destination to work regardless of the folder level of the connecting file.

So I though the below would work so starts at the root.

require_once('/Connections/conn_xxx.php');

but it failed and so did the below:

require_once('http://www.xxxx.com/Connections/conn_xxx.php');

Why is this? I would have thought an absolute path would be fine?
petewinterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MacAnthonyCommented:
Is that an absolute path on the server or for the url? I'm guessing there is more to the path before /Connections/ on the server. This needs to be included for it to be an absolute path on the server.
0
petewinterAuthor Commented:
That is the absolute path for the url

Does the require_once() command work differently to the include() command.

What's the difference?
0
MacAnthonyCommented:
The don't work differently with regard to the path. If you don't prepend it with some sort of protocol designation (like http or ftp), they will both assume it's local path on the server.

The difference between include and require is that include will continue with a warning if the file doesn't exist. Require will error out. require_once has the benefit that it will only attach the file to a running script once if for some reason it is included or required elsewhere in the same script. Including or requiring the same file in the same script will cause an error as well.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

MacAnthonyCommented:
Prepending your path with $_SERVER['DOCUMENT_ROOT'] may fix your issue depending on your server configuration.

require_once($_SERVER['DOCUMENT_ROOT'].'/Connections/conn_xxx.php');
0
themrrobertCommented:
Using an absolute path works, but the directory you are using is not there.

If you are on linux server, you will need to also include the home directory like:

require_once('/home/u/user/public_html/Connections/con_xxx.php');

also, if you try to include via http like your 2nd example, this causes the php script to be executed before its included so you won't be able to use the classes this way, you must use relative path.

To find relative path use realpath()
http://php.net/manual/en/function.realpath.php
0
Scott MadeiraCommented:
If conn_xxx.php is always going to be the same but just called from different directories then why don't you put the file somewhere in the Include path and then all the directory problems go away.  Just do:

require_once('conn_xxx.php');

For security purposes you could put it outside of the document root so it isn't accessible by a URL but is is accessible by the file path in the include statement.

0
petewinterAuthor Commented:
Many thanks for your help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.