PDA

View Full Version : PHP Includes not working? +REP!



myke
19-05-2009, 04:49 PM
Hi guys...

Sorry for the rush, but I'm going just now, under Web Tutorials, there's a thread about php includes instead of iframes...

I am using this, and it seems that they dont change when I link... it stays at the default...

Links are:

index.php?page=web or w.e

Thankssss....

Source
19-05-2009, 04:59 PM
Helps to post the code in question. IE; the code you have, not what the tutorial gave y ou.

myke
19-05-2009, 08:40 PM
Well, I decided in the end to try this code:


<?php
if (!isset($page))
{
include("pages/main.php");
}
if(file_exists($_GET['page'].".php")){
include $_GET['page'].'.php';
}
if(file_exists($_GET['page'].".html")){
include $_GET['page'].'.html';
}
if(file_exists($_GET['page'].".txt")){
include $_GET['page'].'.txt';
}
elseif (isset($page) && !@include("$page"))
{
echo "Error Page not found!";
}
?>

Jam-ez
19-05-2009, 09:04 PM
All your includes are missing some kind of text string:


include "$_GET['page'].'.txt'"

Not sure if that's necessary but I usually use ' or ".

Trinity
19-05-2009, 09:16 PM
Well, I decided in the end to try this code:


<?php
if (!isset($page))
{
include("pages/main.php");
}
if(file_exists($_GET['page'].".php")){
include $_GET['page'].'.php';
}
if(file_exists($_GET['page'].".html")){
include $_GET['page'].'.html';
}
if(file_exists($_GET['page'].".txt")){
include $_GET['page'].'.txt';
}
elseif (isset($page) && !@include("$page"))
{
echo "Error Page not found!";
}
?>


Change all instances of $page to $_GET['page'].

myke
20-05-2009, 07:36 AM
Nope, that didn't work :(

Source
20-05-2009, 02:39 PM
<?php

//variables
$pageRequest = htmlentities( $_GET['page'] );
$pageDefault = 'home';
$pageExtension = '.html';
$pagesPath = 'includes/pages/';

//completed request
$pageInclude = $pagesPath . $pageRequest . $pageExtension;

//set default page if none is set
if( $requestedPage == '' ) $requestedPage = $defaultPage;

//check if selected page exists
if( file_exists( $pageInclude ) ) {
include $pageInclude;
} else {
echo "Page not found. Try again, or go back.";
}

?>


Wrote that really quickly, tried to make it simple to edit. Hope it works/helps.

Jam-ez
20-05-2009, 02:54 PM
Strange. Try this:

index.php

<body>

<?php

if ( $_GET['page'] )
{
if ( file_exists( "pages/" . $_GET['page'] . ".php" ) )
{
include 'pages/' . $_GET['page'] . '.php';
}
else if ( file_exists( "pages/" . $_GET['page'] . ".html" ) )
{
include 'pages/' . $_GET['page'] . '.html';
}
else if ( file_exists( "pages/" . $_GET['page'] . ".txt" ) )
{
include 'pages/' . $_GET['page'] . '.txt';
}
else
{
echo( "Page does not exist." );
}
}
else if ( isset( $_GET['page'] ) && !@include $_GET['page'] )
{
echo( "Error Page not found!" );
}
else
{
echo( "Index." );
}

?>

</body>

fail.php

<?php

echo( "Fail." );

?>

It works on my hosting:
http://habtown.net/test.php?page=fail
With


test.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php

if ( $_GET['page'] )
{
if ( file_exists( "pages/" . $_GET['page'] . ".php" ) )
{
include 'pages/' . $_GET['page'] . '.php';
}
else if ( file_exists( "pages/" . $_GET['page'] . ".html" ) )
{
include 'pages/' . $_GET['page'] . '.html';
}
else if ( file_exists( "pages/" . $_GET['page'] . ".txt" ) )
{
include 'pages/' . $_GET['page'] . '.txt';
}
else
{
echo( "Page does not exist." );
}
}
else if ( isset( $_GET['page'] ) && !@include $_GET['page'] )
{
echo( "Error Page not found!" );
}
else
{
echo( "Index." );
}

?>

</body>
</html>



fail.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php

echo( "Fail." );

?>
</body>
</html>


Edit: Fail is contained within the /pages/ folder.

Want to hide these adverts? Register an account for free!