Problem: I want to load a custom header file for each content type ($categoryId
) on my CMS. For example, if the url is "/?action=archive&categoryId=1", I want it to include my "header_medicine.html" file.
I'm definitely a php noob, but I've tried to respect this forum and solve my question using the tips at this post about conditional code based on url, but the archive page still loads from my 'else' condition.
Here's the code:
<?php $archive_url = parse_url($_SERVER['REQUEST_URI']);
if ($archive_url['path'] == "/?action=archive&categoryId=1")
include "header_medicine.html";
elseif ($archive_url['path'] == "/?action=archive&categoryId=2")
include "header_science.html";
elseif ($archive_url['path'] == "/?action=archive&categoryId=3")
include "header_other.html";
else
include "header.html";
?>
Thanks for considering my question!
Update: Solution
For anyone who is interested, here is the working solution to the code problem I posted above (with simplified fileysystem syntax). I didn't use the isset function that @Michael recommended in his code below. Thanks to everyone who offered suggestions, I'm now one step closer to having a clue about php.
<?php switch ($_GET['categoryId']) {
case 1:
include "header_medicine.html";
break;
case 2:
include "header_science.html";
break;
case 3:
include "header_other.html";
break;
default:
include "header.html";
}
?>
You can access parameters in PHP via $_GET
, $_POST
and others. So $_GET['action']
will give the action type archive and $_GET['categoryId']
will give you 1.
So you can do something like:
<?php
switch ($_GET['categoryId']) {
case "1":
include "/header_science.html";
break;
case "2":
include "/header_other.html";
break;
default:
include "templates/include/header.html";
}
?>
http://php.net/manual/en/control-structures.switch.php
Your example code will not work because $archive_url['path']
will give you only the path /
. Have a look at this example from php manual:
http://php.net/manual/en/function.parse-url.php
<?php
$url = 'http://username:password@hostname/path?arg=value#anchor';
print_r(parse_url($url));
echo parse_url($url, PHP_URL_PATH);
?>
The above example will output:
Array
(
[scheme] => http
[host] => hostname
[user] => username
[pass] => password
[path] => /path
[query] => arg=value
[fragment] => anchor
)
You're taking the long way around something that is actually quite simple in PHP. You only need to check the contents of $_GET['categoryId']
via the $_GET[]
superglobal array. parse_url()
and its cousin parse_str()
will parse correctly parse out a URL, and in your case the part you would have wanted to look at was $archive_url['query']
, but this is all unnecessary -- the information you need is in $_GET
.
if (isset($_GET['categoryId']) {
// If this also depends on action=archive, use
// if (isset($_GET['categoryId']) && isset($_GET['action']) && $_GET['action'] == 'archive')
switch($_GET['categoryId']) {
case 1:
include('/header_medicine.html');
break;
case 2:
include('/header_science.html');
break;
case 3:
include('/header_other.html');
break;
default:
include('templates/include/header.html');
}
}
Now, I find it suspicious that you actually mean to include files like /header_science.html
. include()
calls filesystem paths, so unless your document root is also the server file root path /
, these are probably not the correct inclusion paths.
You may instead be looking for something like
include($_SERVER['DOCUMENT_ROOT'] . '/header_science.html');
Can you get the include path dynamically from your database?
It's not security safe to pass file locations through post/get.
Use the $_GET array (OR $_REQUEST), it contains all parameters passed in url:
ex:
page.php?aaa=1&bbb=hello
you have:
$_GET['aaa']
and
$_GET['bbb']
containing their relative value