I get this error
" Notice: Undefined variable: page in /home1/codefund/public_html/includes/beginning.php on line 16
"
So my variable isn’t being set. Why?? I set the variable before the include file is taking place. Also ignore the <body> and <title> being “home”. It wasn’t echoing there so I moved it for whatever reason. That will be for the $page and $pageHeader
On my index page, I echo $page to make sure it works. However the beginning.php just doesn’t even recognize the variable exists. It has no value to it. I checked my PHP version and it’s 5.4. Is there any server setting that might be messed up? Just got this hosting account so everything should be default and working. I haven’t messed with anything. According to every forum post I’ve read researching this, my code should work and the variables should pass through.
Actually while I’m on the subject, can I get relative paths to work somehow? I originally had /includes/header.php that way no matter what folder I’m on, I can target the base and then go from there. Since that didn’t work, I used an absolute file path.
Unless the file you’re including is on a different server, I wouldn’t use http to include the file.
includes/requires work a little differently depending on the path you use to call it.
If you include(“http://somesite/file.php”) PHP will run through the DNS process and include the file over the network. In addition to taking longer to include the file, this can affect – needlessly – your bandwidth usage as the server is essentially “downloading” the file from itself. Some servers disable remote includes for security reasons as well, which can make the code less portable.
However, if you include(“/path/to/file”), PHP looks for the file locally and uses the filesystem to include it which is faster, and does’t affect your bandwidth.
There are many ways to make local includes easier in terms of relative/absolute paths. If you’re including classes, think about setting up an autoload function. You can also set up constants or variables to serve as the root path portion.
IE:
define('WWW_FOLDER', '/home/user/public_html');
include WWW_FOLDER . '/somefile.php';
Server variables like DOCUMENT_ROOT may be preferred though.
You can also use relative paths. IE: include ‘./file.php’, or ‘…/file.php’, or whatever.
I’ve seen this topic confuse some people coming from front end-development (like HTML/JS/CSS) to server-side because with front-end, everything is included using HTTP and everything is relative to the public_html folder and called with HTTP.
Yeah, in front-end stuff, /file translates to domain.com/file but with PHP, / refers to the root dir on the server, not the web-accessible dir which is commonly located somewhere like /home/username/public_html