SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 28
  1. #1
    SitePoint Zealot Ethan-27's Avatar
    Join Date
    Jan 2011
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Includes and seo

    Includes seem great but what is their effect to optimising pages..?

    Meta tags and title tags are quite important for seo but if using includes how could you optimise each page individually...?

    Is it a case of declaring variables in say, a header include, then rewriting their values relevant to each page....?

  2. #2
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,410
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    I have a page header which I include on every page.

    I then have some switch statements that select the metadata title etc. to go with each page.

    The page name is found from the URL.

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,151
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    When using a MVC(P) framework most of those things that appear in the header can be set at any point within the request before the HTML is output to page. That said most meta tags are worthless. I'm far from a SEO snake, but that is what I have been told/heard.
    The only code I hate more than my own is everyone else's.

  4. #4
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Meta keywords are worthless. Meta Descriptions are useful. Titles are definitely useful.

    I use a function or method for the header rather than a static include for this reason. I can pass in this data which is supposed to be page specific.

  5. #5
    SitePoint Zealot Ethan-27's Avatar
    Join Date
    Jan 2011
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys

    I agree meta keywords are pretty much outlawed due to spam.

    I'm sure your ways are alot better. The switch statement might be something I could write.
    The function or method for the header sounds very interesting but to be honest I would have no idea what a method is....

    Could you perhaps post some example code to assist me in writing my own...?

  6. #6
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A method is just a function of a class.
    So you could have a page template class and have methods for header and footer.

    I also define paths to CSS/JS files. That way a given page can request the files it needs and avoid having unnecessary scripts loaded on every page. When those files change I can also update the file name (to force a fresh download and avoid cache issues) and it'll work across the site.

    Basic example
    PHP Code:
    <?php
    class PageElements
    {

        const 
    site_name 'My website';
        protected 
    $title;
        
        public function 
    __construct($title) {
            
    //append site name to title if not already in it
            
    if(false === strpos(strtolower($title), strtolower(self::site_name))) {
                
    $title .= ' - ' self::site_name;
            }
            
            
    $this->title htmlentities($titleENT_QUOTES'UTF-8');
        }
        
        public function 
    header($title$description null) {
        
            
    $head "<!DOCTYPE html>
            <head>
            <meta charset='utf-8'>
            <title>
    $this->title</title>\n";
            
            if(
    $description) {
                
    $head.= "<meta name='description' content='" htmlentities($descriptionENT_QUOTES'UTF-8') . "'>\n";
            }
            
            return 
    $head "</head>\n";
        }
        
        
        public function 
    footer() {
            
    $year date('Y');
            return 
    "<footer>&copy; $year " self::site_name "</footer>\n";
        }
    }
    ?>
    PHP Code:
    <?php
    $page 
    = new PageElements('Lemon Scented widgets');
    echo 
    $page -> header('Our widgets have been dipped in quality lemon rind for that sumptous nostril flavour');
    ?>
    <body>
    <h1>Lemon Widgets</h1>

    <?php
    echo $page -> footer();
    ?>
    </body>
    </html>

  7. #7
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,033
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    The search engine crawler cannot *see* the PHP code or anything else that parses server side. Includes are parsed server side and delivered to the outside world. There is no way for the spider to know whether the file was dynamically generated or not.

  8. #8
    SitePoint Zealot Ethan-27's Avatar
    Join Date
    Jan 2011
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Cranial that code is really helpful. Bit above me at the moment but Im getting the idea.


    Quote Originally Posted by Michael Morris View Post
    The search engine crawler cannot *see* the PHP code or anything else that parses server side. Includes are parsed server side and delivered to the outside world. There is no way for the spider to know whether the file was dynamically generated or not.
    So how do you change the meta tag, titles etc for each page and make them seo friendly-spider readable...?

    Lol now Im so confused

  9. #9
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,410
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    You echo the variable to display the data:
    PHP Code:
    <?php
    // Get the name of the page for the switch statement
    $path_parts pathinfo($_SERVER['PHP_SELF']);
    $filename basename($path_parts['basename']);

    // Description and keywords meta tags
    switch ($filename) {
    case 
    'index.php':
    $title 'Index page title;
    $metaDesc = '
    Description';
    $metaKey = '
    Keywords';
    break;
    case '
    cont.php':
    $title = '
    Contact';
    $metaDesc = '
    Description';
    $metaKey = '
    Keywords';
    break;
    etc.
    }
    ?>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
        <title><?php echo $title; ?></title>
        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
        <meta name="description" content="<?= $metaDesc; ?>" >
        <meta name="keywords" content="<?= $metaKey; ?>" >
        <meta name="robots" content="index, follow" >
        <meta name="author" content="Rubblewebs">
        <link type="text/css" rel="stylesheet" href="layout.css" >
        <link rel="shortcut icon" type="image/x-icon" href="graphics/favicon.ico">

  10. #10
    SitePoint Zealot Ethan-27's Avatar
    Join Date
    Jan 2011
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Rubble View Post
    You echo the variable to display the data:
    PHP Code:
    <?php
    // Get the name of the page for the switch statement
    $path_parts pathinfo($_SERVER['PHP_SELF']);
    $filename basename($path_parts['basename']);

    // Description and keywords meta tags
    switch ($filename) {
    case 
    'index.php':
    $title 'Index page title;
    $metaDesc = '
    Description';
    $metaKey = '
    Keywords';
    break;
    case '
    cont.php':
    $title = '
    Contact';
    $metaDesc = '
    Description';
    $metaKey = '
    Keywords';
    break;
    etc.
    }
    ?>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
        <title><?php echo $title; ?></title>
        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
        <meta name="description" content="<?= $metaDesc; ?>" >
        <meta name="keywords" content="<?= $metaKey; ?>" >
        <meta name="robots" content="index, follow" >
        <meta name="author" content="Rubblewebs">
        <link type="text/css" rel="stylesheet" href="layout.css" >
        <link rel="shortcut icon" type="image/x-icon" href="graphics/favicon.ico">
    Thanks for your reply and code Rubble.

    This is gonna sound really amateur, the switch codes you have, one for index and for one contact do you add each case to each page individually or keep them in an include and call them...?

    I can see you echo title and the meta tags in the HTML but how does the page know which case to use....?

    If Im confusing anyone I apologise

  11. #11
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,410
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    The page name is obtained from the URL using:
    PHP Code:
    // Get the name of the page for the switch statement
    $path_parts pathinfo($_SERVER['PHP_SELF']);
    $filename basename($path_parts['basename']); 
    The current page name is now in $filename. That is used to select the correct section of switch and you add as many switch statements as you have pages.

    You can add a echo $filename to see what it contains before the switch comand to confirm you are doing it correctly and remove it when the code is working OK.
    Last edited by Rubble; Mar 18, 2011 at 02:02. Reason: Modified last paragraph.

  12. #12
    SitePoint Zealot Ethan-27's Avatar
    Join Date
    Jan 2011
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Rubble View Post
    The page name is obtained from the URL using:
    PHP Code:
    // Get the name of the page for the switch statement
    $path_parts pathinfo($_SERVER['PHP_SELF']);
    $filename basename($path_parts['basename']); 
    The current page name is now in $filename. That is used to select the correct section of switch and you add as many switch statements as you have pages.

    You can add a echo $filename to see what it contains before the switch comand to confirm you are doing it correctly and remove it when the code is working OK.
    Thanks for all your code and trying to explain it but I am still really confused as to how it chooses which switch....?

    With your help I have learnt something new, basename, but in the code I am finding it still a bit difficult I think.

    Am I right in thnking it activates which case to use due to the page being looked at e.g 'cont.php' will activate the code below it...?

    Once again thanks dude

  13. #13
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,410
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    Yes the first part of the code gets the page name from the URL e.g 'cont.php'

    The switch comand then looks for the case 'cont.php' section.

    The variables $title etc. are then populated with contents that relate to the cont.php page

    The contents of the variables are then displayed in the correct place on the page e.g. <title><?php echo $title; ?></title>

    NOTE: <?= $metaDesc; ?> is the same as <?php echo $metaDesc; ?>

  14. #14
    SitePoint Zealot Ethan-27's Avatar
    Join Date
    Jan 2011
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Rubble View Post
    Yes the first part of the code gets the page name from the URL e.g 'cont.php'

    The switch comand then looks for the case 'cont.php' section.

    The variables $title etc. are then populated with contents that relate to the cont.php page

    The contents of the variables are then displayed in the correct place on the page e.g. <title><?php echo $title; ?></title>

    NOTE: <?= $metaDesc; ?> is the same as <?php echo $metaDesc; ?>
    Thanks so much Rubble that has really cleared things up for me and at last I get it lol. Thanks again to cranial for all the code aswell.

    Im gonna implement this into the building of my blog aswell as eveything else everyone has helped me with so far.

    Cheers guys for all your help :-)

  15. #15
    SitePoint Evangelist
    Join Date
    Apr 2007
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also, rather than having a Switch statement with 'x' number of metatag descriptions on it which would be lots of lines of un-needed code, why not (if you have it) save the titles, desciptions, keywords data in MySQL and dynamically call each pages data out depending on what page you are on.

  16. #16
    SitePoint Zealot Ethan-27's Avatar
    Join Date
    Jan 2011
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chris_stfc View Post
    Also, rather than having a Switch statement with 'x' number of metatag descriptions on it which would be lots of lines of un-needed code, why not (if you have it) save the titles, desciptions, keywords data in MySQL and dynamically call each pages data out depending on what page you are on.
    That does sound really productive, I was just wondering does that work ok with how the spiders crawl and how Google indexes, the same point Michael Morris wrote about a few posts back....?

  17. #17
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't like the idea of having all the meta data for your whole site in one file, especially when tied to the base file name. What if one script can serve variable content? I'd rather pass the data in from each page, or if it's DB driven content store it there and pass to the page template.

    As for what how search engines index-- They don't see your PHP. They don't know how you structure any of this. That is SERVER side code. They see your URLs, and the HTML your script outputs. If you want to know what they see, view the source of the page in your browser.

  18. #18
    SitePoint Zealot Ethan-27's Avatar
    Join Date
    Jan 2011
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cranial-bore View Post
    I don't like the idea of having all the meta data for your whole site in one file, especially when tied to the base file name. What if one script can serve variable content? I'd rather pass the data in from each page, or if it's DB driven content store it there and pass to the page template.

    As for what how search engines index-- They don't see your PHP. They don't know how you structure any of this. That is SERVER side code. They see your URLs, and the HTML your script outputs. If you want to know what they see, view the source of the page in your browser.
    ahh ok so the php will output the relevant HTML therefore it can be indexed and swept through by the spiders.

    When you say from each page, do you mean write the php on each page or call it from an include...? As you can see Im still very far behind from figuring out your code lol

    Calling it from the database sounds like a good idea, is that as reliable though....?

  19. #19
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well in my first post the class{} file would get included, because it's common across all pages. It'd make no sense to copy and paste it everywhere.

    BUT the title, and meta description is unique to each page, so you'd define it there, and pass it in to that class to vary the output for each page.

    Are you familiar with functions and passing arguments?

    PHP Code:
    function get_title($title) {
       return 
    $title ' - website name';
    }

    echo 
    "<title>" get_title('Why ducks are wet') . "</title>"

  20. #20
    SitePoint Zealot Ethan-27's Avatar
    Join Date
    Jan 2011
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cranial-bore View Post
    Well in my first post the class{} file would get included, because it's common across all pages. It'd make no sense to copy and paste it everywhere.

    BUT the title, and meta description is unique to each page, so you'd define it there, and pass it in to that class to vary the output for each page.

    Are you familiar with functions and passing arguments?

    PHP Code:
    function get_title($title) {
       return 
    $title ' - website name';
    }

    echo 
    "<title>" get_title('Why ducks are wet') . "</title>"
    To a certain extent but very basic. Thanks for your patience by the way.

    If you were defining the title and meta tags for each page (writing them seperately on each relevant page), what would you be varying..?

  21. #21
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,151
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    In many cases the title will be representative of the main content for the page. Take a blog posting my example. Ideally the title of the page should include in some manor the title of the blog post. Just look at the title of this thread by example. The title includes the thread title alongside the name of the site. Its very rare unless working on a fairly small and insignificant site that the title for each separate piece of main content will be the same. The title should always in some manor include information about the main content displayed on the page. The main content will generally dictate the meta data.
    The only code I hate more than my own is everyone else's.

  22. #22
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you were defining the title and meta tags for each page (writing them seperately on each relevant page), what would you be varying..?
    Like oddz said, the title and meta description should relate to that particular page.
    So what's your reason for having a different page? It must have different content than some other page on your site, or it wouldn't exist.

    The <title> is the title of that page. Not your whole site. Same with meta description.

  23. #23
    SitePoint Zealot Ethan-27's Avatar
    Join Date
    Jan 2011
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cranial-bore View Post
    Like oddz said, the title and meta description should relate to that particular page.
    So what's your reason for having a different page? It must have different content than some other page on your site, or it wouldn't exist.

    The <title> is the title of that page. Not your whole site. Same with meta description.
    Thanks for your help

    Im only confused with this part though

    "BUT the title, and meta description is unique to each page, so you'd define it there, and pass it in to that class to vary the output for each page."

    LIGHTNING BOLT! I think Ive got it. I actually forgot the HTML is part of an include and the title and meta tags 'values' are variables which change according to the page.

    Each page might perhaps have a variable name of '$title' but you would write its values at the top of that page. Obviously this would change form page to page.

    If I am right then Ive just completely over-complicated myself and forgotten both methods use includes. If I am wrong however then testing the gravity between my window and the downstairs might be an option.

  24. #24
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Gravity is 9.8ms^2, so don't worry about that.

    And yes, the purpose of the include is to provide the structure of the common page elements, while allowing you to pass in the data that is page specific.

    So the Doctype is the same for all pages, it is hard-coded into my pageElements class. The title however is variable, so you pass in the value from the page. The page knows what it's title should be. The class will append the site name if it's not already part of the string.

    The meta description is variable, so again, you pass it in.

    Have a go at running the code in my early post (#6). You'll need PHP5.
    Save the first block of code as a file (an include). Then in another file, include that script and pass in the title and meta description like my second block demonstrates. View the resultant source code in your browser and you should see what it outputs.

    You can change the markup in PageElements to suite (e.g. change the HTML5 footer element into a DIV). By looking at the output source code you should see how it works though.

  25. #25
    SitePoint Zealot Ethan-27's Avatar
    Join Date
    Jan 2011
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Cranial

    I'm gonna try and give it a go today and Ill post and let you know what happens (stand back!)

    I havent got as far as classes yet, only the pure minimum so gonna crack on with those too

    Thanks again


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •