Results 1 to 7 of 7

Thread: include file to control elements in static php pages

  1. #1
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default include file to control elements in static php pages

    I don't work on many sites that use seperate pages like about.php contact.php and so on, but I have one question.

    I would like to see if that code used on the dynamic page could be changed to work in the same way so one include file can be inserted and for instance...keywords, descriptions, so on could be supplied based on domain.com/about.php

    I tried to mess around with the previous dynamic code to get the request of a static page like about.php but didn't have any luck.

    A general idea...
    $request = $_SERVER['REQUEST_URI'];

    somehow check that if $request equals domain.com/about.php
    echo "keyword, keyword 1, keyword 2"; (can be anything really...specific to that page..graphics etc.)

    Of course you can have http, https, w/ or w/o www. so domain.com/page.php would cover most. Thank you for any help, just a little curious.

  2. #2
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    Before you include the file that makes all the pages look similar, set some variables in the "host" file that the include will handle as arguments.

    about.php
    Code:
    $var = 'Hello, world!';
    require_once 'common.inc';
    common.inc
    Code:
    echo $var;
    You probably don't need to know what $var is unless your actions cannot be directly calculated from its value - in which case, use a switch-statement.
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  3. #3
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    Thank you for your reply. It's my fault for not clarifying. (more of how to get the static page requested)

    When pages are served using index.php?page=about the following can be used to check for that page and display something.
    PHP Code:
    <?php 
    $request 
    $_SERVER['REQUEST_URI']; 
    if (isset(
    $_GET['page']) && $_GET['page'] == 'about') { // see if  ?page is present and if it is equal to "about" 
    echo '<span style="color: red; font-weight: bold;">About Us</span>'
    }
    else { 
    echo 
    '<a href="?page=about" class="menu">About Us</a>'

    ?>
    I wondered how this could be changed to work if the page was a static page like... domain.com/about.php

    For instance, like a header graphic, in the area where the image would be I would include a file like header_graphic.php

    header_graphic.php would determine the actual page URL like domain.com/about.php domain.com/contact.php so on...once it determined the page it would display the correct info.

    The graphic can of course be manually changed in each static page, but if you had say 15 pages...you could make the changes in one file rather than each. Thank you.

  4. #4
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    I don't understand what your question/problem is... (Several $_SERVER variables are available to detect the page's location, but I chose SCRIPT_NAME because I don't think it includes GET variables.)
    Code:
    $page =& $_SERVER['SCRIPT_NAME'];
    if(isset($page)){
        switch($page){
            case '/about.php':// the URL relative to the document root
                //...
                break;
            case '/login.php':
                //...
                break;
            default:
                //...
        }
    }
    EDIT: And I don't think your PHP will be included from other domains, unless it's on an insecurely shared server.
    Last edited by Jesdisciple; 08-04-2008 at 03:18 AM.
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  5. #5
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    Thank you Jesdisciple, that's perfect.

    It looks like "SCRIPT_NAME" and "REQUEST_URI" will both work in my environment.

    My problem was trying to compare it to a value and the specific syntax that needed to be used, so your example made it really clear.

  6. #6
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    Note that I don't use anything like this for my own site. I just put the unique contents directly into each file so the switch-statement is unnecessary (not that this is a better way but that's how I do it).
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  7. #7
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    Yeah, on sites I have worked on with static pages I have always gone in and changed the content on each page (just as easy).

    I recently gave this some thought, because I recently worked with a client that was great to work with...but constantly wanted to add/change the keywords on each page.

    As an experiment in the future I might try this other method...not that it's easier, but it may save a little time on something like a 20 page site.

    Thank you 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
  •