Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Using require() or include() to enable use of CONSTANTS


shagymoe

Recommended Posts

Hi all,

 

I'm looking for help on accessing the constants in the configure.php file. I need to use the DIR_FS_CATALOG constant in a script, but it is not being seen. So here are some questions.

 

How do you get the constants to propagate down through the directory structure? Do you do it by using includes/requires? For instance, includes/application_top.php require()s configure.php and nearly every file require()s application_top.php, thus making the constants in configure.php propagate down to each file. Is that a correct statement?

 

On the other hand, I have found files that access constants which don't seem to have require()d or include()ed any other files. How is that possible? For instance includes/classes/message_stack.php accesses DIR_WS_ICONS, but doesn't have any require or include statements. However, application_top.php require()s message_stack.php so that might do the trick.

 

I have tried all kinds of things to access these constants, with no success.

 

I have some files set up like this:

catalog/mydir/my files

catalog/mydir/my more dirs/my more files

 

I want the "my files" and "my more files" to be able to access the constants.

 

I made a test file and put it in catalog/images and tried a couple of things. First I tried adding require('../includes/application_top.php'); to the test.php file which didn't work and then I tried adding require('images/test.php'); to the application_top.php file and that didn't work either.

 

I know this is going to be a simple answer, but I am relatively new to PHP and I've just been hacking my way through it. Thanks for the help in advance.

 

Shagy

Link to comment
Share on other sites

On the other hand, I have found files that access constants which don't seem to have require()d or include()ed any other files.  How is that possible?  For instance includes/classes/message_stack.php accesses DIR_WS_ICONS, but doesn't have any require or include statements.  However, application_top.php require()s message_stack.php so that might do the trick.
Yes, that is exactly why it works. Some file (e.g. index.php) includes application_top.php, which then includes configure.php and later (in the same context) includes message_stack.php.
I have some files set up like this:

catalog/mydir/my files

catalog/mydir/my more dirs/my more files

 

I want the "my files" and "my more files" to be able to access the constants.

 

I made a  test file and put it in catalog/images and tried a couple of things.  First I tried adding require('../includes/application_top.php'); to the test.php file which didn't work and then I tried adding require('images/test.php'); to the application_top.php file and that didn't work either.

For security reasons, web scripting usually requires you to include things in subdirs, rather than parent (..) directories.

 

What you may want to do is to put a file in your base catalog directory and have it include/require the other files. If you want things from application_top.php to be available, you need to include application_top.php before including the other file.

 

Good luck,

Matt

Link to comment
Share on other sites

As a rule of thumb, PHP does not work on the same playing field as HTTP ... you cannot include relative files (in terms of (../file), although include(/dir/file.php) works) ... i.e. stepping UP a directory or two won't work with include() or require().

 

Instead use $_SERVER["DOCUMENT_ROOT"] plus the path to the file(s).

 

Example:

 

I'm in subdir /whatever/ ... and I want to include a file that is one dir above that.. i'd use

 

 

 

<? include($_SERVER["DOCUMENT_ROOT"] . "filename.php"?>

 

or..

 

I'm in a dir /whatever/ and I want to include a file in /includes/

 

]<? include($_SERVER["DOCUMENT_ROOT"] . "includes/filename.php"?>

 

* note: you may need a / before the dir string.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...