PDA

View Full Version : include file to control elements in static php pages



?foru
08-01-2008, 10:47 PM
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.

Jesdisciple
08-02-2008, 05:22 AM
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
$var = 'Hello, world!';
require_once 'common.inc';

common.inc
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.

?foru
08-04-2008, 02:53 AM
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
$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.

Jesdisciple
08-04-2008, 03:13 AM
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.)

$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.

?foru
08-06-2008, 02:23 AM
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.

Jesdisciple
08-06-2008, 03:08 AM
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).

?foru
08-07-2008, 02:48 AM
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