?
Solved

require_once()

Posted on 2011-10-03
7
Medium Priority
?
282 Views
Last Modified: 2012-05-12
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?
0
Comment
Question by:petewinter
7 Comments
 
LVL 11

Expert Comment

by:MacAnthony
ID: 36904206
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
 

Author Comment

by:petewinter
ID: 36904231
That is the absolute path for the url

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

What's the difference?
0
 
LVL 11

Accepted Solution

by:
MacAnthony earned 2000 total points
ID: 36904256
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 11

Expert Comment

by:MacAnthony
ID: 36904279
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
 
LVL 13

Expert Comment

by:themrrobert
ID: 36904283
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
 
LVL 14

Expert Comment

by:Scott Madeira
ID: 36904296
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
 

Author Closing Comment

by:petewinter
ID: 36904302
Many thanks for your help.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month15 days, 9 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question