Home arrow Apache arrow Page 4 - Using ForceType For Nicer Page URLs
APACHE

Using ForceType For Nicer Page URLs


Apache has features that allow us to setup easy to remember URL's for our web site's pages. In this article Joe shows us how easy it is to do with Apache and a little PHP.

Author Info:
By: Joe O'Donnell
Rating: 5 stars5 stars5 stars5 stars5 stars / 57
June 05, 2002
TABLE OF CONTENTS:
  1. · Using ForceType For Nicer Page URLs
  2. · Apache's ForceType directive
  3. · Implementing ForceType sensibly
  4. · The PHP script
  5. · Conclusion

print this article
SEARCH DEVARTICLES

Using ForceType For Nicer Page URLs - The PHP script
(Page 4 of 5 )

PHP's $_SERVER array contains a lot of important variables that we can use to determine the complete URL entered into the browser. REQUEST_URI contains the URI requested and SCRIPT_NAME contains the path of the script that was requested.

A sample REQUEST_URI variable looks like this: /soccer/Taipan
A sample SCRIPT_NAME variable looks like this: /soccer


Now, because we know we are already working with our soccer PHP script, all we're concerned about is the bit of the URL after /soccer, which will tell us the name of the team and optionally the name of the player that our visitor wants the specs of. It is logical to remove the script name from the request URI to leave us with just the team name and optional player name, separated by forward slashes. We can do this with the ereg_replace function, like this:

$nav = $_SERVER["REQUEST_URI"];
$script = $_SERVER["SCRIPT_NAME"];
$nav = ereg_replace("^$script", "", $nav);


So now the $nav variable would look something like /Taipan/John Smith. Because $nav now contains the variables that we want, we can separate it into an array delimited by the forward slash using PHP's explode function:

$vars = explode("/", $nav);
$team = "";
$player = "";


Depending on whether or not the visitor wants team or player information, the size of the $vars array will be different. A simple switch statement can take care of grabbing the team and player names into variables:

switch(sizeof($vars))
{
case 2:
$team = $vars[1];
break;
case 3:
$team = $vars[1];
$player = $vars[2];
break;
}


At this point we will have extracted the team and optionally player name from the URL. We want to display stats for the chosen team/player, so let's mock up a quick MySQL database called soccer:

create database soccer;
use soccer;

create table teams
(
teamId int auto_increment not null,
name varchar(50),
country varchar(50),
coach varchar(50),
inWorldCup bit,
primary key(teamId),
unique id(teamId)
);

create table players
(
playerId int auto_increment not null,
name varchar(50),
salary decimal(7,2),
age int,
teamId int,
position varchar(20),
primary key(playerId),
unique id(playerId)
);


Let's add a team and player to our soccer database:

insert into teams values(0, 'Bears', 'Korea', 'Ju Wan Do', 0);
insert into players values(0, 'Kuan Chi', 35000.00, 24, 1, 'Striker');


We now have a team called the Bears and a player called Kuan Chi who is a member of the bear's team in the player's table. Note that the teamId field in the player's table represents the teamId field of the team in which this player exists.

Using an if statement, we can determine whether to show team stats, player stats, or an error if no team or player were passed through the URL:

if($team == "")
{
die("You must at least choose a team!");
}
else if($team != "" && $player == "")
{
// Show the team stats
GetTeamStats($team);
}
else if($team != "" && $player != "")
{
// Show the player stats
GetPlayerStats($team, $player);
}


GetTeamStats contains some code to query the database and simply return each field from the database as well as the number of players from the player's table who play for that specific team. It's a simple function and looks like this:

function GetTeamStats($TeamName)
{
$result = mysql_query("select * from teams where name='$TeamName'");
if($row = mysql_fetch_array($result))
{
// Show team data
?>
<h2>Statistics For <?php echo $TeamName; ?></h2>
<b>Country:</b> <?php echo $row["country"]; ?><br>
<b>Coach:</b> <?php echo $row["coach"]; ?><br>
<b>In World Cup:</b> <?php echo ($row["country"] == 1 ? "Yes" : "No"); ?><br>
<?php

// Show number of players
$pResult = mysql_query("select count(playerId) from players where teamId = {$row["teamId"]}");
$pRow = mysql_result($pResult, 0, 0);
?>
<b>Number Of Players:</b> <?php echo $pRow[0]; ?>
<?php
}
else
{
// No record found
die("Team $TeamName doesn't exist in the database.");
}
}


GetPlayerStats grabs the details of a player from the database and outputs them in a similar fashion to GetTeamStats. It looks like this:

function GetPlayerStats($TeamName, $PlayerName)
{
$result = mysql_query("select teamId from teams where name='$TeamName'");
if($row = mysql_fetch_array($result))
{
$teamId = $row["teamId"];
$pResult = mysql_query("select * from players where name='$PlayerName' and teamId=$teamId");

if($pRow = mysql_fetch_array($pResult))
{
?>
<h2>Statistics For <?php echo $PlayerName; ?></h2>
<b>Player ID:</b> <?php echo $pRow["playerId"]; ?><br>
<b>Salary:</b> $<?php echo number_format($pRow["salary"], 2); ?><br>
<b>Age:</b> <?php echo $pRow["age"]; ?><br>
<b>Team:</b> <?php echo $TeamName; ?><br>
<b>Position:</b> <?php echo $pRow["position"]; ?><br>
<?php
}
else
{
// No record found
die("Player $PlayerName doesn't exist in the database.");
}
}
else
{
// No record found
die("Team $TeamName doesn't exist in the database.");
}
}


[Note] The entire source code for the soccer PHP script is available as the support material for this article from the last page, and I've omitted the database connection code in the code above to keep it as short as possible. [End Note]

Now that we've setup the ForceType directive in our .htaccess file and written the PHP script to work with the pages URI and grab the team/player names, we can hit the soccer script and it will output the team/player details from the database.

So, if we visited http://localhost/soccer/Bears, then we would see this page:

Viewing the details of a team

To view the details of a specific player from the Bears team, we would visit http://localhost/soccer/Bears/Kuan Chi. Here’s the resultant page:

Viewing the details of a player

If we visit the URL http://localhost/soccer and don't pass any team/player name, then the script will spit an error message, like this:

Not passing any team/player name to the script
blog comments powered by Disqus
APACHE ARTICLES

- Programmatically Manipulating Microsoft Exce...
- Installing PHP under Windows
- Compressing Web Content with mod_gzip and mo...
- Compressing Web Output Using mod_deflate and...
- Setting Up Apache 2.0.45 to Parse PHP Pages
- Custom Error 404 Documents with PHP
- Using Apache and PHP on Mac OS X
- ASP: Active Sessions, Active Logins and Tota...
- Working With Oracle on Windows: Part 1
- The Quick-n-Dirty Guide to Setting Up Apache...
- Installing Apache With SSL: The Complete Gui...
- 7 Powerful .htaccess Customization Tips
- Trap And Get Notified: A Practical Solution ...
- One Way To Use Server Side Includes
- Using ForceType For Nicer Page URLs

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials