Forums

  • # September 17, 2011 at 2:09 pm

    example:
    Header.php


    < !DOCTYPE html>


    Head stuff here such as meta, and stylesheets etc.



    Website Title


    Subheading






    pages such as index.php and about.php



    This is a paragraph with body text and lists and such


    Footer.php






    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?

    Bob
    # September 17, 2011 at 2:22 pm

    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 and closing tags to your footer file though.

    # September 17, 2011 at 2:46 pm

    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.

    Bob
    # September 17, 2011 at 2:51 pm

    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.

    # September 17, 2011 at 3:23 pm

    For what, why do you need it to go through the loop?

    # September 17, 2011 at 4:10 pm

    So you’re asking how you can do something like:

    if home show content of home || if about show content of about || if contact show content of contact

    else show 404?

    # September 17, 2011 at 4:23 pm

    I’m not sure this would be beneficial but you could do something similar below.

    Get URL
    If home.php echo content
    else if page.php echo content

    else echo 404.php

    endif

    Sounds more simple this way

    # September 18, 2011 at 12:33 am

    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.

    # September 18, 2011 at 5:49 am

    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?

    # September 18, 2011 at 11:31 am

    never trust user input. something like

    include($_GET["page"].".php");

    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');
    }
    else
    {
    $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

    # September 18, 2011 at 11:36 am

    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 ^^

    # September 18, 2011 at 2:05 pm

    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 ^^

    # September 20, 2011 at 10:17 am

    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
    if(isset($title)) $title=$title;
    else $title="My Page Title";

    //start document
    print < << END
    < !DOCTYPE html>


    $title



    END;
    ?>










    < ?php // index.php
    session_start(); // start session if needed

    //set title
    $title="Different title from the default one in header.php";
    require('header.php');

    print < << END

    haha


    END;
    require('footer.html'); // or footer.php depending on the type of code in the file
    ?>

    # September 21, 2011 at 2:28 pm

    @ChristopherBurton

    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.

    # September 21, 2011 at 2:44 pm

    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.

    header.php



    < !doctype html>

    < ?php echo $thisTitle; ?>











    index.php:



    < ?php
    $thisTitle='This is the title you change for each page';
    $thisDescription='This is the desc you change for each page';
    include ('html/header.php');
    ?>




    < ?php include ('html/footer.php'); ?>

Viewing 15 posts - 1 through 15 (of 16 total)

You must be logged in to reply to this topic.

icon-anchoricon-closeicon-emailicon-linkicon-logo-staricon-menuicon-nav-guideicon-searchicon-staricon-tag