Results 1 to 14 of 14
  1. #1
    Join Date
    Mar 2009
    Location
    Lawrence, KS
    Posts
    108

    Question MySQL / PHP header includes

    Hello,

    I am setting up a new website using php includes. I guess my question is can I identify the meta description, keywords, and title of every url strictly with one file (header.php) and a MySQL database?

    I realize I can use variables with the addition of a third file, say index.php. Although I really want to keep my code clean and I thought using a database may be the answer. However I am still in the same predicament, or so I think.

    How will header.php know which page (db details) to display? Whether it is index.php or hosting.php or support.php, etc? Or is some extra coded needed in each of the php page files (index/hosting/support/etc) to determine just that?

    Does that make sense?

    Thanks guys!
    Seth Cole (Google+) (@rockhost)
    [ROCKHOST.COM - "Be Heard"]

  2. #2
    Join Date
    Feb 2003
    Location
    AR
    Posts
    2,381
    I'm not entirely sure what you're asking, but....

    Say you've got

    index.php
    PHP Code:
    <?php
    include 'header.php';
    ?>
    CONTENT GOES HERE
    <?php
    include 'footer.php';
    ?>
    Then in header.php
    PHP Code:
    <php
    if($_SERVER['REQUEST_URI'] == '/home.php') {
      $title='Home Page';
    }
    else if($_SERVER['REQUEST_URI'] == '/contact.php') {
      $title='Contact Page';
    }

    ?>
    <html>
    <head><title><?php echo $title?></title></head>

    Not the most elegant solution by a long shot, but it does do what you were asking (I think...)

  3. #3
    Join Date
    Mar 2009
    Location
    Lawrence, KS
    Posts
    108
    Hey Kevin, cool, thanks for the reply. So if I wanted I could just include a meta.php file in index.php which supplies all of the page urls and titles. I think that makes more sense than the angle I was taking. Nice.
    Seth Cole (Google+) (@rockhost)
    [ROCKHOST.COM - "Be Heard"]

  4. #4
    Join Date
    Apr 2002
    Location
    Philadelphia
    Posts
    2,277
    Yes, that is correct. That wouldn't be an issue at all

  5. #5
    Join Date
    May 2004
    Location
    Pflugerville, TX
    Posts
    11,222
    Here's an idea - this is roughly how we handle includes.

    Create a set of whatever variables you want present within the common areas of your site, and make sure they're all in your individual page files, like this:

    Code:
    <?php 
    $title = 'My page title';
    $desc = 'My page description, which shows up in search engine results.';
    $key = 'key, words, for, meta, tags, not, like, search, engines, care, anymore';
    $class = ''; // a spot to declare a page's class, if needed
    $id = ''; // a spot to declare a page's ID, if needed
    include $_SERVER[DOCUMENT_ROOT].'header.php'; 
    ?> 
    CONTENT GOES HERE 
    <?php 
    include $_SERVER[DOCUMENT_ROOT].'footer.php'; 
    ?>
    Then, in your header file, you can set a bunch of defaults, and then call in individual page variable where needed, like this:

    Code:
    <php 
    //DEFAULT VARIABLES
    if ($title == '') { $title = 'My Default Page Title'; }
    if ($desc == '') { $desc = 'This title will show up in search engines by default if I don't set a description within an individual page.'; }
    if ($key == '') { $key = 'some, default, key, words'; }
    if ($class == '') { $class = ''; //probably best to keep this as-is }
    if ($id == '') { $id = ''; //probably best to keep this as-is }
    ?>
    <!DOCTYPE html>
    <html dir="ltr" lang="en-US">
    <head>
    	<meta charset="UTF-8" />
    	<title><?php echo $title; ?></title>
    	<meta name="description" content="<?php echo $desc; ?>">
    	<meta name="keywords" content="<?php echo $key; ?>">
    </head>
    <body<?php if($id != '') { echo ' id="'.$id.'"'; } if($class != '') { echo ' class="'.$class.'"'; } ?>>
    The class and id slots are there in case you want to declare different styles or even a completely different layout for different pages. For instance, you could set $class='home' for your home page, and then create an if-statement that displays a big banner and different content on your home page than from other pages on your site. In theory, you could use IDs and classes to create an infinite number of variations to a single template, yet control them from a single document.

    Hopefully you get the idea. This basis can lead to some pretty clever coding solutions to interesting problems, such as how to keep a single code base but extend it over multiple off-the-shelf applications. Using a system like this, we've managed to create universal header and footer files that can plug into WordPress, PHPList, Kayako, WHMCS and more, all at the same time, all installed on the same site.

    Imagine being able to update multiple application templates and static pages from a single set of files. Believe me, it's nice!

    Disclaimer: the code above is untested. I just whipped it up in my browser. It might have a syntax error or two, but I just wanted to give you the right idea.
    Last edited by the_pm; 03-08-2011 at 03:42 PM.
    Studio1337___̴ı̴̴̡̡̡ ̡͌l̡̡̡ ̡͌l̡*̡̡ ̴̡ı̴̴̡ ̡̡͡|̲̲̲͡͡͡ ̲▫̲͡ ̲̲̲͡͡π̲̲͡͡ ̲̲͡▫̲̲͡͡ ̲|̡̡̡ ̡ ̴̡ı̴̡̡ ̡͌l̡̡̡̡.__Web Design

  6. #6
    Join Date
    Mar 2009
    Location
    Lawrence, KS
    Posts
    108
    I like this:

    $key = 'key, words, for, meta, tags, not, like, search, engines, care, anymore';

    Funny. Thanks everybody. I think I am going to create meta.inc.php and use this as in include similar to header.inc.php.
    Seth Cole (Google+) (@rockhost)
    [ROCKHOST.COM - "Be Heard"]

  7. #7
    Which one is easier PHP or .Net? Please suggest. your reply will be appreciated.

  8. #8
    Join Date
    Mar 2009
    Location
    Lawrence, KS
    Posts
    108
    This has been working great by the way (PHP). One last question...I'm finally to the index file as I have been building from the ground up on my migration. Anyway I have this:

    if($_SERVER['REQUEST_URI'] == '/index.php') {
    $title='[ROCKHOST.COM]';
    $description='Welcome to ROCKHOST.COM';
    $heading='blah blah blah';
    }

    I'm not sure how to add the syntax for '/' to the top line of code. Can someone help me out? Essentially I need this to cover both rockhost.com/ and rockhost.com/index.php. I'm hoping I can do this without having to add another "if" statement. Can I have two urls in the first line of code?

    Thanks!
    Seth Cole (Google+) (@rockhost)
    [ROCKHOST.COM - "Be Heard"]

  9. #9
    Join Date
    Sep 2010
    Location
    Romania
    Posts
    34
    if( ($_SERVER['REQUEST_URI'] == '/index.php') OR ($_SERVER['REQUEST_URI'] == '/') ) {
    // or '', or 'whatever'
    // [...]

  10. #10
    Join Date
    Mar 2009
    Location
    Lawrence, KS
    Posts
    108
    Hmm...I must have the syntax wrong still:

    Parse error: syntax error, unexpected T_LOGICAL_OR in /home/rockhost/public_html/includes/meta.inc.php on line 2

    I tried this:

    if($_SERVER['REQUEST_URI'] == '/') OR ($_SERVER['REQUEST_URI'] == '/index.php') {
    $title='Domain Names | Band Websites | Streaming Media Servers [ROCKHOST.COM - "Be Heard"]';
    $description='Welcome to ROCKHOST.COM! Home of the legendary streaming media server and refuge to owner operator Seth Cole.';
    $keywords='domain names, band websites, shoutcast hosting, icecast hosting, flash servers, quicktime servers, windows streaming';
    $heading='Small Business Owner';
    }

    Any ideas? Thanks!
    Last edited by rockhost; 07-20-2011 at 03:02 PM. Reason: typo
    Seth Cole (Google+) (@rockhost)
    [ROCKHOST.COM - "Be Heard"]

  11. #11
    Join Date
    Feb 2003
    Location
    AR
    Posts
    2,381
    if($_SERVER['REQUEST_URI'] == '/' || $_SERVER['REQUEST_URI'] == '/index.php') {.....

    You had closed your if statement before you were finished defining your conditions...

  12. #12
    Join Date
    Mar 2009
    Location
    Lawrence, KS
    Posts
    108
    Cool, that did the trick. Thanks guys!
    Seth Cole (Google+) (@rockhost)
    [ROCKHOST.COM - "Be Heard"]

  13. #13
    Just wanted to add...

    Like the_pm suggested, defining the page name or page variables is a safer route than going off of page request uri.

    For example:
    $current_page = "index";
    include("header.php");

    and in your header.php
    if ($current_page == "index") {
    // stuff
    } else if ( etc... ) {
    ...

    So, when you decide to mess around with stuff like file redirection, renaming, etc where that exact address isn't shown, it'll still work out.

  14. #14
    Join Date
    Jul 2011
    Location
    Seattle, WA
    Posts
    12
    On another note, if your page content is coming from a database, make sure you use mysql_real_escape_string($current_page); or you will be vulnerable to SQL injection:

    For example:

    Page1.php

    ...
    <body>
    <a href='blah.com?page=/Page2.php'>Go to page 2</a>
    </body>
    ...


    Page2.php
    <?php

    if(isset($_GET['page'])){
    $page = mysql_real_escape_string($_GET['page']));

    $q = @mysql_query("select page from tablename where page='$page'") or die(mysql_error());
    $a = @mysql_fetch_array($q);

    echo $page;

    }

    ?>

Similar Threads

  1. php 'includes' issue on linux server with apache 2.0.63 / php 5.2.6
    By scotch333 in forum Hosting Security and Technology
    Replies: 5
    Last Post: 06-19-2008, 07:18 AM
  2. Replies: 0
    Last Post: 10-24-2004, 11:34 AM
  3. Replies: 3
    Last Post: 10-17-2004, 06:27 AM
  4. Replies: 0
    Last Post: 10-09-2004, 10:36 AM
  5. Replies: 1
    Last Post: 07-08-2003, 06:00 PM

Posting Permissions

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