Unoffical empeg BBS

Quick Links: Empeg FAQ | RioCar.Org | Hijack | BigDisk Builder | jEmplode | emphatic
Repairs: Repairs

Topic Options
#163315 - 28/05/2003 18:00 Help with PHP switch construct for web page
hybrid8
carpal tunnel

Registered: 12/11/2001
Posts: 7738
Loc: Toronto, CANADA
Ok, I need this done rather quickly and simply and I thought I'd be able to get the info I needed with a few simple searches. Unfortunately I'm finding I need a little help. Here's a description of what I want and why (just in case someone has a better suggestion):

I have a piece of software that will have a few links to web pages on my server. I'd like to be able to modify the web pages and their locatoins without having to re-write the software or redistribute it.

My proposed solution: Call a single URL with a passed variable like this:
http://www.mypocket.com/subfolder/index.php?model=ford

Then my index.php would contain a SWITCH with various cases to correspond to the values being passed. Each case would redirect the client browser to a complete HTML page. Redirecting, from what I've read, should be possible with the following command: header("Location: http://www.mysite.com/");

Can anyone help me construct a simple page to accept a variable "name" for three values (let's say "name1" "name2" and "name3")

While writing this I believe I have managed to get this working, but I'd really like others input on this. Thanks.

Bruno
_________________________
Bruno
Twisted Melon : Fine Mac OS Software

Top
#163316 - 28/05/2003 18:17 Re: Help with PHP switch construct for web page [Re: hybrid8]
mcomb
pooh-bah

Registered: 31/08/1999
Posts: 1649
Loc: San Carlos, CA
I haven't done PHP in so long that I don't remember exactly how to do this, but I would use an include instead of a redirect. The redirect requires the user's browser to go out and request another page (takes longer) while the include just grabs the included file on the server site and outputs it to the client without an additional request.

-Mike
_________________________
EmpMenuX - ext3 filesystem - Empeg iTunes integration

Top
#163317 - 28/05/2003 19:11 Re: Help with PHP switch construct for web page [Re: hybrid8]
David
addict

Registered: 05/05/2000
Posts: 623
Loc: Cambridge
If you've got access to a database, then it would be easier to have a table with a primary key of the short name and the URL. You can then use the following code:

header('Location: '.query("SELECT url FROM table WHERE name=$model"));
With query() being whatever function is necessary to get a result from your database and return a single value.

To do a switch, use the following:

switch ($model) {
case 'ford':
header('Location: http://www.ford.com');
break;
case 'empeg':
header('Location: http://www.empeg.com');
break;
}

Top
#163318 - 28/05/2003 19:13 Re: Help with PHP switch construct for web page [Re: mcomb]
David
addict

Registered: 05/05/2000
Posts: 623
Loc: Cambridge
> use an include instead of a redirect.

Wouldn't that cause problems with things like cookies, browser detection scripts and so on?

Top
#163319 - 28/05/2003 19:20 Re: Help with PHP switch construct for web page [Re: mcomb]
hybrid8
carpal tunnel

Registered: 12/11/2001
Posts: 7738
Loc: Toronto, CANADA
Doesn't the include require that the included document contain only a portion of HTML? ie. it should be the contents of the body, but not actually include header tags.

This whole mechanism has to work with pages that already exist and that are usable from other standard links on a web site. So while I can do anything I want in the PHP, I can't modify the existing web pages that are to be displayed.

Here is what I came up with - which is working right now. The file is called "index.php" and it sits in a subfolder on my site. So you can call the page like this: http://www.mypocket.com/auto/?make=acura (it's not actually on my site right now)


<html>

<head>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
</head>

<body bgcolor="white" text="black" leftmargin="5" marginheight="3" marginwidth="5" topmargin="3">

<?php

$prod = strtolower ($prod);

switch( $make )
{
case "mazda":
header("Location: http://www.mysite.com/cars/mazda.html");
break;

case "acura":
header("Location: http://www.mysite.com/cars/acura.html");
break;

case "nissan":
header("Location: http://www.mysite.com/cars/nissan.html");
break;

default:
break;
}


?>

</body>

</html>


The default case just causes the page to sit there, I know. Alternately I can just make that point to a main page. DOesn't matter since access to this php would be fr the sole purpose of my app.

Bruno
_________________________
Bruno
Twisted Melon : Fine Mac OS Software

Top
#163320 - 28/05/2003 19:23 Re: Help with PHP switch construct for web page [Re: hybrid8]
ricin
veteran

Registered: 19/06/2000
Posts: 1495
Loc: US: CA

<?php
# index.php


// Directory containing .html/php/etc files to be displayed
//relative to the current directory

$HTML_DIRECTORY = "html";


switch($_REQUEST["q"]){


case 'name1':
include($HTML_DIRECTORY."/name1.html");
break;

case 'name2':
include($HTML_DIRECTORY."/name2.html");
break;

case 'name3':
include($HTML_DIRECTORY."/name3.html");
break;

default:
include($HTML_DIRECTORY."/default.html");
break;


}
?>


http://sinicco.org/hybrid8/?q=name1
http://sinicco.org/hybrid8/?q=name2
http://sinicco.org/hybrid8/?q=name3

OR



<?php
# test.php

// Directory containing .html/php/etc files to be displayed
// relative to the current directory

$HTML_DIRECTORY = "html";

include($HTML_DIRECTORY."/".$_REQUEST["q"]);
?>


http://sinicco.org/hybrid8/test.php?q=name1.html
http://sinicco.org/hybrid8/test.php?q=name2.html
http://sinicco.org/hybrid8/test.php?q=name3.html

OR


<?php
# test2.php

// Directory containing .html/php/etc files to be displayed
// relative to the current directory

$HTML_DIRECTORY = "html";

$EXT = "html";

include($HTML_DIRECTORY."/".$_REQUEST["q"].".".$EXT);
?>


http://sinicco.org/hybrid8/test2.php?q=name1
http://sinicco.org/hybrid8/test2.php?q=name2
http://sinicco.org/hybrid8/test2.php?q=name3


Of course, the files "name*.html" are in /hybrid8/html/.

How many examples do you want?
_________________________
Donato
MkII/080000565
MkIIa/010101253
ricin.us

Top
#163321 - 28/05/2003 19:39 Re: Help with PHP switch construct for web page [Re: David]
mcomb
pooh-bah

Registered: 31/08/1999
Posts: 1649
Loc: San Carlos, CA
Wouldn't that cause problems with things like cookies, browser detection scripts and so on?


It shouldn't if you are designing for it. That is a common piece of the MVC (Model, View, Controller) design model. You have a central controller that takes user input and displays the appropriate content based on input and state. All cookies are set with the path of your controller or just "/". Browser detection, etc isn't affected in anyway that I can think off.

It is not like you are creating a proxy, you are simply passing control to another php page temporarily and then you get control back so you can include a additional pages if necessary. My company has a standard jsp template (we are a java shop, but the premise is the same) that we use which dynamically includes headers, navigation elements and the body of the webpage based on database entries.

-Mike
_________________________
EmpMenuX - ext3 filesystem - Empeg iTunes integration

Top
#163322 - 28/05/2003 19:45 Re: Help with PHP switch construct for web page [Re: ricin]
mcomb
pooh-bah

Registered: 31/08/1999
Posts: 1649
Loc: San Carlos, CA
How many examples do you want?

Thanks for properly illustrating what I was trying to say! The OP may also want to look at the require() command that acts similar to include, but fails in a different way (hard error) than include().

-Mike
_________________________
EmpMenuX - ext3 filesystem - Empeg iTunes integration

Top
#163323 - 28/05/2003 20:10 Re: Help with PHP switch construct for web page [Re: ricin]
hybrid8
carpal tunnel

Registered: 12/11/2001
Posts: 7738
Loc: Toronto, CANADA
Thanks for the replies...

The trouble I see with the includes is that the URL isn't reformatted and continues to show the called contents, variable and all.

And the last two examples aren't quite appropriate to use with content that may already exist without a structured path. They also require the name of the HTML document to be passed or constructed from the passed variable. Good for when redeploying that content in a new structure, but I have to make this work with a bunch of pages that have been created by different people over the course of a few years - their naming conventions aren't the same.

Example 1 seems to be the most apropriate and is very similar to what I posted above. Allowing for the fact that I don't know next to anything about PHP except for its loose similarities to so many other languages.

What is th reason behind the $_REQUEST instead of just using "q" within the switch?

Bruno
_________________________
Bruno
Twisted Melon : Fine Mac OS Software

Top
#163324 - 28/05/2003 20:13 Re: Help with PHP switch construct for web page [Re: David]
hybrid8
carpal tunnel

Registered: 12/11/2001
Posts: 7738
Loc: Toronto, CANADA
Funny how I didn't see this reply even when entering my second one. Doh.

I'm actually doing this because I have relatve control over the application doing the calling, but I don't have control over the structure of the web site (it won't be used on my personal site).

The db idea is a good one too.

Bruno
_________________________
Bruno
Twisted Melon : Fine Mac OS Software

Top
#163325 - 28/05/2003 20:20 Re: Help with PHP switch construct for web page [Re: hybrid8]
ricin
veteran

Registered: 19/06/2000
Posts: 1495
Loc: US: CA
What is th reason behind the $_REQUEST instead of just using "q" within the switch?


All of my servers have register_globals = off, which is good PHP practice. See here.
_________________________
Donato
MkII/080000565
MkIIa/010101253
ricin.us

Top
#163326 - 29/05/2003 02:11 Re: Help with PHP switch construct for web page [Re: mcomb]
David
addict

Registered: 05/05/2000
Posts: 623
Loc: Cambridge
Sorry, misunderstanding. I was thinking of external sites, since that appeared to be the example used in Bruno's first post. As far as that site sees, it is being called by a system that can't remember cookies and doesn't return any browser ID string (for sever-based browser detection). Relative links on that site wouldn't work either, as the browser isn't aware of the true URL of the site.

Top