Jump to content

PHP die function


scferg

Recommended Posts

I have a website setup using the simple navigation function, like: http://website.com/index.php?page=blah

 

The only problem is that if they typed in a page that does not exist, it returns with PHP not found errors. So, how do I make so that if the page is not found, another file is loaded, like error404.html ?

 

I've tried some other codes, but either don't work at all, or don't work right. I'm a novice with PHP, so I could really use some help with this.

Link to comment
Share on other sites

Well it depends on how you're going about showing the actual page.  Are you using a switch statement? etc.

 

you could try

 

<?php
if(file_exists("$_GET['page'].php")){
    include("$_GET['page'].php");
}
else{
    include("error.php");
}
?>

 

edit: fixed an error

Link to comment
Share on other sites

Well it depends on how you're going about showing the actual page.  Are you using a switch statement? etc.

 

you could try

 

<?php
if(file_exists($_GET['page'])){
    include("$_GET['page'].php");
}
else{
    include("error.php");
}
?>

 

Yeah...this is the normal way...but it should be

if(file_exists($_GET['page'].'.php')){

 

 

Link to comment
Share on other sites

Another thought, you could use a switch statement for more control:

 

switch($_GET['page']){
    case 'home':
        include("home.php");
        break;
    case 'about':
        include("about.php");
        break;
    default:
        include("error.php");
        break;
}

Link to comment
Share on other sites

How are you deciding on content to display? As in, can you paste the code where you work with the $_GET['page'] variable? There are lot's of solutions, and if you shows us what you have, it will be easier to provide you with the best solution.

Link to comment
Share on other sites

			<?php
				if($section && $page) {include("./$section/$page"); }
				elseif($page == "case1") {
				echo "case1 goes here no page was written for this statement.";}
				elseif($page) {include("./$page.html");}
				else {$number=10; $only_active=TRUE; include("./news/show_news.php"); }
			?>

 

Don't laugh about how pathetic it is :P

Link to comment
Share on other sites

<?php

  $error_page = "./404.php";

  if($section && $page) {

    if(file_exists("./$section/$page"))

      include("./$section/$page");

    else

      include($error_page);

  }elseif($page == "case1") {

    echo "case1 goes here no page was written for this statement.";

  }elseif($page) {

    if(file_exists("./$page"))

      include("./$page.html");

    else

      include($error_page);

  }else {

    $number=10;

    $only_active=TRUE;

    include("./news/show_news.php");

  }

?>

Link to comment
Share on other sites

Ok...let's start from the top...

 

<?php
  $error_page = "./404.php";
  $page = $_GET['page'];
  $section = $_GET['section'];

  if($page == "case1") { //Handle special cases first
    echo "case1 goes here no page was written for this statement.";
  } elseif($page) { //Page requested
    $path = "{$page}.html";
    if($section) //Add section
      $path = "{$section}/{$path}";
    if(file_exists($path))
      include($path);
    else
      include($error_page);
  } else { //Default
    $number=10;
    $only_active=TRUE;
    include("./news/show_news.php");
  }
?>

 

this code has a big security flaw though as it allows people to possibly see files on your computer that they shouldn't see. but, for now, let's just see if we can get this working....

Link to comment
Share on other sites

server...wherever the index.php script is

 

to be safe, you should only allow page keys to be letters, numbers, and underscores

 

  ...
  } elseif($page) { //Page requested
    $path = "{$page}.html";
    if($section) //Add section
      $path = "{$section}/{$path}";
    //Check keys and existance
    if(preg_match('/^\w+$/',$page) && preg_match('/^\w+$/',$section) && file_exists($path))
      include($path); //Good to go...include it
    else
      include($error_page); //Error page
  } else { //Default
  ...

Link to comment
Share on other sites

It was just a snippet of the lines I updated. Here it is in it's entirety:

 

<?php
  $error_page = "./404.php";
  $page = $_GET['page'];
  $section = $_GET['section'];

  if($page == "case1") { //Handle special cases first
    echo "case1 goes here no page was written for this statement.";
  } elseif($page) { //Page requested
    $path = "{$page}.html";
    if($section) //Add section
      $path = "{$section}/{$path}";
    //Check keys and existance
    if(preg_match('/^\w+$/',$page) && preg_match('/^\w+$/',$section) && file_exists($path))
      include($path); //Good to go...include it
    else
      include($error_page); //Error page
  } else { //Default
    $number=10;
    $only_active=TRUE;
    include("./news/show_news.php");
  }
?>

Link to comment
Share on other sites

try this out tomorrow:

 

<?php
  $error_page = "./404.php";
  $page = $_GET['page'];
  $section = $_GET['section'];

  if($page == "case1") { //Handle special cases first
    echo "case1 goes here no page was written for this statement.";
  } elseif($page) { //Page requested
    $path = "{$page}.html";
    if($section) //Add section
      $path = "{$section}/{$path}";
    //Check keys and existance
    if(preg_match('/^\w+$/',$page) && preg_match('/^\w*$/',$section) && file_exists($path))
      include($path); //Good to go...include it
    else
      include($error_page); //Error page
  } else { //Default
    $number=10;
    $only_active=TRUE;
    include("./news/show_news.php");
  }
?>

Link to comment
Share on other sites

grr....that has a security hole...this should be good though:

 

<?php
  $error_page = "./404.php";
  $page = $_GET['page'];
  $section = $_GET['section'];

  if($page == "case1") { //Handle special cases first
    echo "case1 goes here no page was written for this statement.";
  } elseif($page) { //Page requested
    $path = "{$page}.html";
    if($section) //Add section
      $path = "{$section}/{$path}";
    //Check keys and existance
    if( preg_match('/^\w+$/',$page) && 
        (!strlen($section) || preg_match('/^\w+$/',$section)) && 
        file_exists($path)
      )
      include($path); //Good to go...include it
    else
      include($error_page); //Error page
  } else { //Default
    $number=10;
    $only_active=TRUE;
    include("./news/show_news.php");
  }
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.