PDA

View Full Version : basic php help needed




tominated
Jun 23, 2008, 08:46 PM
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:


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?



tominated
Jun 23, 2008, 09:48 PM
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
//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:


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!

Me1000
Jun 23, 2008, 09:54 PM
try this...

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


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

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

tominated
Jun 23, 2008, 10:09 PM
try this...

$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?

tominated
Jun 23, 2008, 10:18 PM
--ignore this, i fixed it

Me1000
Jun 23, 2008, 10:20 PM
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....


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


<?
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';
}

?>

Me1000
Jun 23, 2008, 10:23 PM
for if the variable is empty...



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

tominated
Jun 23, 2008, 10:56 PM
thanks. I have pretty much got it sorted now.