Probably simple error

I am trying to do a current page script. On my contact.php I set $page and $pageHeader as such

<?php 
$pageHeader="Contact";
$page="contact";
include("http://www.codefundamentals.com/includes/beginning.php");?>

Then on my beginning.php it’s this

<?php
ini_set('display_errors',1); 
 error_reporting(E_ALL);
?>
<!DOCTYPE html>
<html>
<head>
<title>CodeFundamentals - Home</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
<link rel="icon" href="/favicon.ico" type="image/x-icon">
<link href="/scripts/styles.css" type="text/css" rel="stylesheet" />
<link href="/scripts/resets.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="/scripts/scripts.js"></script>
</head>
<body class="home"><?php echo $page; ?>

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

<DOCTYPE html>
<html>
<head>
<title>CodeFundamentals - <?php echo ucwords($page);?></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
<link rel="icon" href="/favicon.ico" type="image/x-icon">
<link href="/scripts/styles.css" type="text/css" rel="stylesheet" />
<link href="/scripts/resets.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="/scripts/scripts.js"></script>
</head>
<body class="<?php echo $page;?>">
<?php 
$page="home";
require("http://www.codefundamentals.com/includes/beginning.php");?>

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.

Do you have http:// wrapper enabled on your server?
Most of the time you can only include with a relative path, e.g.

<?php
$pageHeader="Contact";
$page="contact";
include "includes/beginning.php";
?>

Try changing the URL to a path.

Also set error_reporting(-1);

Ninjaad while reading other threads :frowning:

Also noticed that error_reporting is being set in the included file and should be set in the first file that is used.

Wow. That was it. Anyway to enable http:// wrapper? Didn’t know that it wasn’t set as default. Thanks so much!

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.

Try echo $_SERVER[“DOCUMENT_ROOT”] which is far better than relative paths because files can be moved and still be OK when called.

Tapped from a tablet

Perfect. Thanks a lot John.

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.

With CSS I can do /path/to/file/ and it’s fine, yet it was tripping up PHP for some reason. Thanks for the extra information.

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

Ah ok. That explains it. I coulda sworn when I dabbled in PHP stuff years ago that this wasn’t the case. Oh well now I know.

Another option is to use set_include_path() at the start of the first script