Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tominated

macrumors 68000
Original poster
Jul 7, 2006
1,723
0
Queensland, Australia
Hey guys. I'm trying to make a basic php include setup going with the scotmac site i posted yesterday, but I have run into a small problem. Here's the snippet of code i need help with:

PHP:
if(file_exists("includes/pages/$page.html")){
	require_once ("includes/pages/$page.html");
} else {
	require_once ("includes/pages/home.html");
}

When i type the url to the php file and add "?page=about" or something other than home, it just goes to the home page. I can't get it to include the file i want. Is there anything I'm doing wrong?
 
ok, with a little help of this - http://www.alistapart.com/articles/succeed/ - i have further exploded my php. here is the whole php file after destroying it by accident:

PHP:
<?php
//1. check to see if a "real" file exists..

if(file_exists($DOCUMENT_ROOT.$REQUEST_URI)
and ($SCRIPT_FILENAME!=$DOCUMENT_ROOT.$REQUEST_URI)
and ($REQUEST_URI!="/")){
$url=$REQUEST_URI;
include($DOCUMENT_ROOT.$url);
exit();
}

//2. if not, go ahead and check for dynamic content.
$url=strip_tags($REQUEST_URI);
$url_array=explode("/",$url);
array_shift($url_array); //the first one is empty anyway


?>
<?php require_once ("includes/header.php"); ?>	

		<?php require_once ("includes/pages/latestproj.html"); ?>	
		
		<div id="content">
			<div id="border">
				
				<?php
					if(empty($url_array)) { 
						include("includes/pages/home.html"); 
						exit();
					} elseif(file_exists("includes/pages/$url_array.html") {
						include("includes/pages/$url_array.html");
					} else {
						include("includes/pages/404.html");
					}
				?>	
				
			</div>
		</div>
		
<?php require_once ("includes/footer.php"); ?>


and my .htaccess file is this:

Code:
RewriteEngine on
RewriteRule !\.(gif|jpg|png|css)$ /scotmac2/index.php


I am using MAMP to do this currently, using the OS ports (whatever the hell that means). It just comes up with an internal server error when i load any page in the scotmac2 directory (where the files are stored (including the htaccess)

HELP!
 
try this...

Code:
$page = $_REQUEST['page']; //though you might want to run some type of strip slashes on this or something

if(file_exists("includes/pages/".$page.".html")){ 
    require_once ("includes/pages/".$page.".html"); 
} else { 
    require_once ("includes/pages/home.html"); 
}

If I were you I would just change the .html to a .php just because it will give you more flexibility later on and less server configuration now. Because as I understand it by default you can only include .php pages...


that said if I were to write this script it would looks something like this...

Code:
$page = stripslashes($_REQUEST['page']); 

if(file_exists("includes/pages/".$page.".php")){ 
    include ("includes/pages/".$page.".php"); 
} else { 
    include ("includes/pages/home.php"); 
}
 
try this...

Code:
$page = $_REQUEST['page']; //though you might want to run some type of strip slashes on this or something

if(file_exists("includes/pages/".$page.".html")){ 
    require_once ("includes/pages/".$page.".html"); 
} else { 
    require_once ("includes/pages/home.html"); 
}

Thanks, that worked. Is there any way I can get friendly URL's (urls like a static website's) without much config?
 
Thanks, that worked. Is there any way I can get friendly URL's (urls like a static website's) without much config?

the way I do it....

Code:
<?
include('template.php');
$pageName = 'Page Name';

templateHeader($pageName);
?>


<p>This is the static page html</p>

<?
templateFooter($pageName);
?>

then the template.php file looks like this...

Code:
<?
function templateHeader($pageName){
    echo 'This is the template content before the actual page begins (up to where you would have used the require_once() functions';

/* if you wanted to call the page name you just refer to the variable $pageName  just make sure that when you do that you end your quote from the echo then use the concatenation */
//for example....
// echo '<title>Website Name: '.$pageName.'</title>'; //make sure you uncomment the line though... 

}

function templateFooter($pageName){
    echo 'this is all the template code after the page content';
}

?>
 
for if the variable is empty...

Code:
$page = (isset($_REQUEST['page'])) ? $_REQUEST['page'] : 'home' ;

that is basically a shorthand if statement.

but again for security reasons you should really stripslashes() on the $_REQUEST['page'] part, or just do it on the $page variable after you set it.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.