Jump to content

PHP Include. If not correct then 404 or selected page


ev5unleash

Recommended Posts

Hi I have this code.

 

<?php 

include('./top.html'); // header

switch(strtolower($_GET['id']))  // 'content'
{
	case "contact":
	    include("contact.html");
	break;
	case "smartboardsolution":
	    include("smartboardsolution.html");
	break;
	case "error=404":
	    include("ermes/404.html");
	break;
	case "error=403":
	    include("ermes/403.html");
	break;
	case "redirect":
	    include("redirect.html");
	break;
	default:
	    include("home.php");
	break;
}

include('./bottom.html'); // footer
?>

 

Instead of giving my users a weird php error page I would like to display my own message, page or 404.

Since you are ultimately trying to load a file, simply add the code to see if file exists.

 

<?php 

include('./top.html'); // header

switch(strtolower($_GET['id']))  // 'content'
{
	case "contact":
                  if(is_file('/path/to/contact.html'))
                  { 
	     include("contact.html");
                  }
                  else
                  {
                      // not a file, so show the custom message or 404 error.
                  }
	break;
	case "smartboardsolution":
	    include("smartboardsolution.html");
	break;
	case "error=404":
	    include("ermes/404.html");
	break;
	case "error=403":
	    include("ermes/403.html");
	break;
	case "redirect":
	    include("redirect.html");
	break;
	default:
	    include("home.php");
	break;
}

include('./bottom.html'); // footer
?>

 

I only added the code to the first case, you can even restructure it a bit so it only runs once instead of running in each case.

 

Nate

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.