< !DOCTYPE html>
Head stuff here such as meta, and stylesheets etc.
pages such as index.php and about.php
This is a paragraph with body text and lists and such
thats the layout of my website and im just wondering if there’s a better way i could use php instead of including header and footer in every page?
Actually that is exactly the way WordPress does it. The index.php page of WordPress generally looks like this: first
< ?php get_header(); ?>, then it gets all the main content using the loop, after that it sometimes gets a sidebar and it ends with
< ?php get_footer(); ?>.
So I’d say your code above is pretty much the same, of course if you don’t want the footer or header in one of your pages you just don’t put for example
< ?php get_header(); ?>in your code.
Be sure to add the
closing tags to your footer file though.
Looks good to me other than a few minor things. Make sure to add meta charset in your header and close the body and html in the footer.
I think it would be a good idea to just use WordPress now if you plan to use a CMS in the future. WordPress is really simple if using the right blank theme.
Just out of curiosity – why don’t you just use WordPress? The way I see it, it fits your needs exactly, because as you say, it’s a hassle the way you get and include everything now. Using WordPress you can just use WordPress’ functions and that seems a lot easier than you’re doing it now.
I would steer clear of this method and just suck it up. Because you can not possibly have that many pages. If you do, this the way you are talking about it will both kill your SEO and the scalability of any site. These two reasons alone make the cost out-way the benefits. You will also learn a lot more by doing the project in a more scalable manner.
get the url
remove php extension so you have: home, about, contact
echo that to a body class
In your css target the body class with a current class to your nav item
I’m not sure I explained that well. I think you should stick to WordPress, man. Atleast until you get the hang of how to loop your content.
@seanroberts – I’m not aware of how a loop will “kill” your SEO, care to explain?
never trust user input. something like
is evil itself.
now, if you don’t want to “echo the whole page”, don’t use include(), just use file_get_contents(), and *echo that. that also means you can use an .html extension, which might help with text editor syntax highlighting, too.
(* what I personally do is build the whole output, then set the appropriate headers, then have a single echo $output; at the very end)
if you want to use a whitelist:
// do stuff
if (in_array($_GET, array('home', 'about', 'another_page', 'you_get_the_idea')))
$page = file_get_contents('path_to_pages/' . $_GET . '.html');
$page = '404 sadface.';
// do stuff
that is the one super secure way — nothing you haven’t predetermined to be a valid argument gets accepted.
I guess you could also remove all dots and slashes from $_GET, and then do a
if (file_exists('path_to_pages/' . $_GET))
instead of the in_array() check, that *should* be safe, too, but maybe there is more to watch out for than dots and slashes I’m not sure, I’m usually using the a database anyway, and when I don’t I usually use a white list because hey, it’s not *that* much hassle and it’s bulletproof.
as long as it’s your own little site, go for it… you can always “just use wordpress” later. yes, if you want good results fast, use wordpress :P and if you do it your own way, you will eat your foot so many ways! it’s amazing what one can screw up (there’s more to HTTP than just echoing stuff, if you want things properly cached, support 304 not modified and whatnot), and you’ll be fixing errors and wait months for google to forgive you haha… but I say, go for it. learn how this stuff actually works.
everybody should have built at least one crappy little CMS before using those of others, that’s my motto :P
one thing that’s really important, set the right headers! and generally learn about the awesome extra stuff you can do with them, starting with the basics of setting a content-length that matches the length of the string you’re going to output, working yourself up to caching, etags and 304 not modified :D
but one thing is sure, if you don’t set a 404 header when outputting a 404 page, search engines will hate you. that’s the one thing about headers you *have* to learn. been there, done that ^^
please try to understand what the code actually does :)
it is reading the file, and assigning the content to a variable ($page) — but unless you echo that variable, of course nothing will show up! I just like to put stuff in a variable first, because then you can do more with it than just echo it.. but in this case that’s unnecessary I guess. sorry for the confusion ^^
To be honest, the way you do it with the includes, that’s how I do it – except i use the require(); function.
< ?php //header.php
//if i'm using mysql:
require('mysqlstuff.php') // this requires functions i made for accessing mysql databases
//set title dynamically
else $title="My Page Title";
print < << END
< !DOCTYPE html>
< ?php // index.php
session_start(); // start session if needed
$title="Different title from the default one in header.php";
print < << END
require('footer.html'); // or footer.php depending on the type of code in the file
There is a way to make it SEO friendly and I was assuming how he was talking about doing the page is not good for SEO. If you create the header information (Meta data, Title, etc.), or have a function that adds information to the page based on the current page. However, I assumed he would not do that and just use the current page to build the actual page content. And here is where he confirms my assumptions. Doing the page this way will cause duplicate page titles, descriptions, etc.
Why are you so adamant on not using the includes or requires? This is what I do.
I use variables for things that need to be page specific, like title and desc tags. This is an example of a drummed down version of what I typically do for small static sites.
< !doctype html>
< ?php echo $thisTitle; ?>
$thisTitle='This is the title you change for each page';
$thisDescription='This is the desc you change for each page';
< ?php include ('html/footer.php'); ?>
You must be logged in to reply to this topic.