I'm just learning PHP, and I realize this is a very basic question, but some of the things I've found on the Internet aren't working! I want to be able to set a unique page title for every page at my site. So, I thought I could set a php variable at the top of my page and then include my header.php file which uses that variable. So, at the top of every page I have: <?php $title = "Unique Title"; include("http://www.manageyourdollars.com/common/header.php"); ?> And in header.php I have (among other things): <TITLE><?php echo $title; ?></TITLE> I've found a couple tutorials on the web that say this should work, unless I'm misreading them. What am I missing?
Include requires local file paths, not http: URLs. So it would be more like include("/path/to/your/local/files/header.php");
you can only include files on a local system (even then you can have loads of fun with the open_basedir error) any variable in the included file is accesible it will be a huge security flaw if php files could be opened across sites ok lets say heres the file u want to include (call it common.php and its in sam folder as rest of your site) <?php $commonTitle = 'title goes here'; ?> PHP: and heres every page of your site <?php include( 'common.php' ); print '<html><head><title>'.$commonTitle.'</title> and so on and so forth'; ?> PHP:
Variables that are defined in included files are accessible everywhere else, but are files that are defined somewhere else accessible in the included file?
Yes. Think of it as copying and pasting the code from the include file into the "parent" file. Once php parses the includes it's all one big happy file
Just note too, though, that once you're trying to access that variable from within a function, you need to add the line: global $variable_name; to the function: by default, a function cannot access a variable declared outside of that function.
For the whole story, I can't recommend highly enough the php.net site: http://us2.php.net/manual/en/function.include.php Some interesting quotes from that page: And
On the point of including via HTTP, please know this up front: if you include http://www.example.com/somefile.php, you are including the output of that script, not the file itself. That is to say that if you visit http://www.example.com/somefile.php with your browser, what you see in that browser has to be valid PHP otherwise including it via HTTP will not work. It doesn't magically get the code itself, if you get my drift...
Why does everyone insist on using parenthesis with include? It's not a function, so it doesn't need parenthesis. A good idea, along with what you are doing, is to have the headers or other file check if each of those variables are set (isset()) and, if they aren't, provide a default so you don't accidently end up with blank titles (or keywords, or whatever else you use variables across scripts for).
Yes. Think of it as copying and pasting the code from the include file into the "parent" file. Once php parses the includes it's all one big happy file