Jump to content

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);

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.