Jump to content

page fetching problem


DenverR

Recommended Posts

my current code for getting a page is

	<?php 
	$page = $_GET['page'];  //Gets the (page=) from the URL
  		if($page){  
  		$site = file_exists($page.'.html') ? $page.'.html' : 'home.html';}
  		else{
  		$site = 'home.html'; // Else include the default home.php file.       
  		}
  		include($site);
?>

 

the problem with it is it only checks for html pages.

I would like to have it check for html then php pages and it pull up whichever is valid.

 

i tried to do it myself but im a php n00b. pls help

        	<?php 
	$page = $_GET['page'];  //Gets the (page=) from the URL
  		if($page){  
  		$site = file_exists($page.'.html') ? $page.'.html' : 'home.html';}
  		elseif
  		$site = file_exists($page.'.php') ? $page.'.php' : 'home.html';
	else{$site = 'home.html'; // Else include the default home.php file.       
  		}
  		include($site);
		?>

 

^^ is what i did. any help will be appreciated. Thanks

Link to comment
https://forums.phpfreaks.com/topic/211003-page-fetching-problem/
Share on other sites

I would never use the file name/path as a GET variable. You are opening yourself up to exploitation. But,this should do what you ask:

$pageExtensions = array('htm', 'html', 'php');
    
if(isset($_GET['page']))
{
    $page = $_GET['page'];  //Gets the (page=) from the URL
    //Check page against each extension until a match is found
    foreach($pageExtensions as $ext)
    {
        $filepath = "{$page}.{$ext}"
        if(file_exists($filepath)
        {
            $site = $filepath;
            break;
        }
    }
}
    
if(!isset($site))
{
    $site = "home.html";
}	
    	
include($site);

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.