PHP - Using pathinfo variables makes relative paths not to work

I am hoping to achieve some kind of cms on my own. I see i can catch Apache enviromental variables, known as PATH_INFO, so this way i can make dynamic sections on my site (just like joomla does).

for example:

stackoverflow.com/index.php/section1/article22

Currently i'm developing a function to find out wich section and article was requested, by doing this:

$url_seccion = $_SERVER['PATH_INFO'];
$secciones_array =  array_values(array_filter(explode('/', $url_seccion)));

This part is working fine, the issue i'm having is that all relative paths i had now are broken. Can someone explain me why is this happening and what can i do to solve it? (Please don't tell me i have to use absolute paths...)

Upvotes: 4

Views: 279

Answers (1)

Júlio Jamil
Júlio Jamil

Reputation: 107

example:

$url = "stackoverflow.com/index.php/section1/article22";
$myArray = array_slice( explode('/', $url), 2 );
echo "section: ". $myArray[0] ."<br /> article: ". $myArray[1]."<br />";
if(isset($myArray[0]))  {
$section = $myArray[0];
} else {
$section = "";
}

if(isset($myArray[1]))  {
$article = $myArray[1];
} else {
$article = "";
}

switch(strtolower($section)) {
default:
echo "home";
break;
case 'section1':
echo "function for find and show my article: ". $article;
break;
}

to use section saved in db, you can use a select to find the id of it and then Article

To redirect all requests to index.php use:

.htaccess

Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteCond $0 !^(index\.php|css|js|img)
RewriteRule ^(.*)$ index.php [L]

index.php:

$url = addslashes($_SERVER['REQUEST_URI']);
$myArray = array_slice( explode('/', $url), 1 );

Upvotes: 1

Related Questions