Jump to content

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

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.