Jump to content

[SOLVED] If page doesnt exist


b1011

Recommended Posts

I did the mistake of writing my whole website on one index page :-\

 

The problem is, each page uses

if ($_GET['pg']==page){

#code

 

}

 

But if it is not a page, it doesn't execute anything. and the whole site is messed up. Is there anyway to make it so if $_GET['pg'] is not a valid page, then go to a 404 script?

Link to comment
https://forums.phpfreaks.com/topic/68617-solved-if-page-doesnt-exist/
Share on other sites

Alternatively, you could define an array of possible pages:

 

<?php
$pages = array('page1','page2','page3');//array of possible pages;
if(isset($_GET['pg']){
if(!in_array($_GET['pg'],$pages)){
	header("location:error.php");
	exit;
}else{
	//your nasty block of if statements here
}

}
?>

 

A better solution would be a re-write so you dont have such a block of if-elseif statements.

This is what I like to do...

<?

$page = $_GET['page'];

?>

 

 

 

 

 

 

<?

if ($page == "") {

include "home.php";

}

else {

if (file_exists($page . ".php")) {

include $page . ".php";

}

else {

echo "<center>The page <b>$page</b> does not exist.<br>Check back soon!</center>";

}

}

?>

 

Basically a URL with http://domain.com/index.php?page=modules

 

"modules.php" will be included if it exists. If not, it echos a does not exist... I like it cuz its simple...

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.