php basic page router

this php page router while pretty basic does the job for this site. it takes all the query string and splits it with the / into $p variables

you will also need the following nginx or similar page rewriting all to the index.php

php page router, php router

NGINX

server {
    root /var/www/html/kruxor.com;
    server_name kruxor.com;
    location / {
        index index.php;
        try_files $uri $uri/ @core;
        expires max;
    }
    location @core {
        rewrite ^/(.*)$ /index.php?p=$1;
    }
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
    location ~ /\.ht {
        deny all;
    }
}

PHP

<?php

// page router

$p = ""; if(isset($_GET['p'])) { $p = $_GET['p']; }

if(!$p > "") {
  $current_page = "home";
	$page_string = "home";
  include("pages/home.page.php");
	//$page_content .= "Home";
	return;
}

// explode all bits of p into page array
if($p > "") {
	$page_array = explode("/",$p); 
	$page_array = array_filter($page_array);
}

// if the site is in a sub directory need to delete the sub directory from the array.
if($base_directory > "") {
  //array_shift($page_array);
  //var_dump($page_array);
}

if(!is_array($page_array)) {
  $current_page = "page-not-found";
	$page_content .= "Not Found";
	return;
}

// set p variables to 10 as blank.
for($i=0; $i<=10; $i++) {
	${"p".$i} = "";
}

$pcount = 1;
foreach ($page_array as $key => $value) {
	if($value > "") {
		${"p".$pcount} = $value;
    $bugs .= "p$pcount : $value<br>";
	}
	$pcount++;
}

if(file_exists("pages/$p1.page.php")) {
  $current_page = $p1;
	include("pages/$p1.page.php");
} else {
	include("pages/404.page.php");
}

?>

Other Items in php

No Items Found.

Add Comment
Type in a Nick Name here
 
Page Views

This page has been viewed 147 times.

Search Code
Search Code by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.

❤👩‍💻🕹

Random Quote
Our brains are wired to find things we are looking for - if your always cynical or waiting for things to go wrong, then your life will reflect that. On the other hand, having a positive outlook on life will bring you joy and provide you with inspiration when you least expect it ☀️🍉🌴
Unknown