0

I am trying to set-up a structure for my application to properly include scripts across various pages and subdirectories

The issue I am having;

inside my index.php I have

define ('BASE_URL', 'http://localhost/cformv2/');
include BASE_URL . 'inc/functions.php';
require BASE_URL . 'inc/db.php';
include BASE_URL . 'inc/auth.php';

and inside my functions.php I have

$hello = 'hi';

When I call echo $hello; in the index page, it will give me a notice saying;Notice: Undefined variable: hello in If I echo $hello in functions page, the message appears in the index page.

After trying to fix, I noticed that, If I move functions.php to the same directory and use include ('function.php'); and echo $hello in the index page, it works. So I assume, I am using this BASE_URL incorrectly.

In summary, what am I trying to do is to be able to include files without manually entering the path each time, after reading a couple of posts, I started using this option using DEFINE ();. Currently, all of my works are in folder cformv2 and working on localhost. Once it finishes, it will be uploaded to my web host. I have multiple files in multiple folders under this base directory (cformv2).

How can define the path to this folder so that I can include my other files from other folders? Also, how it is that I can include the file and not able to call the variable or a function within it?

Thanks in advance.

Aram Grigoryan
  • 740
  • 1
  • 6
  • 24
eleven0
  • 263
  • 6
  • 13
  • 2
    Possible duplicate of [Full URL not working with php include](https://stackoverflow.com/questions/13369529/full-url-not-working-with-php-include) – Davidos Jul 15 '18 at 18:02

2 Answers2

0

You can't open http://localhost/cformv2/inc/functions.php through URL
You can use BASE_DIR as

define ('BASE_URL', '/cformv2/');

Include is meant to include and evaluate a specified PHP file. If you fetch it locally, it can be processed like PHP - if you fetch it through a full URL, you will get the resulting HTML (in theory ... you may get only an error). Full URL not working with php include

Davidos
  • 419
  • 5
  • 17
0

If you want to include PHP files, you shouldn't have used URL. There must be full path to file on server, for example:

define ('BASE_DIR', __DIR__);
include BASE_DIR . '/inc/functions.php';
MrSmile
  • 1,217
  • 2
  • 12
  • 20