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
https://forums.phpfreaks.com/topic/89855-php-die-function/
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
https://forums.phpfreaks.com/topic/89855-php-die-function/#findComment-460508
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
https://forums.phpfreaks.com/topic/89855-php-die-function/#findComment-460510
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
https://forums.phpfreaks.com/topic/89855-php-die-function/#findComment-460518
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
https://forums.phpfreaks.com/topic/89855-php-die-function/#findComment-460530
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
https://forums.phpfreaks.com/topic/89855-php-die-function/#findComment-460533
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
https://forums.phpfreaks.com/topic/89855-php-die-function/#findComment-460546
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
https://forums.phpfreaks.com/topic/89855-php-die-function/#findComment-460551
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
https://forums.phpfreaks.com/topic/89855-php-die-function/#findComment-460554
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
https://forums.phpfreaks.com/topic/89855-php-die-function/#findComment-460560
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
https://forums.phpfreaks.com/topic/89855-php-die-function/#findComment-460566
Share on other sites

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.