Jump to content

Recommended Posts

OK I'm 98% new to PHP. I'm trying to do my own website and my idea will work something like this:

 

index.php

____________________

Header + Nav

{PHP INCLUDE}

Footer

____________________

 

The content will reside in another *.php page

 

If you look at www.avlscotland.com/test 'temporary'

 

It loads default.php when you go to the page which is perfect.

 

I have it working in basic form although only the hyperlinks

HOME - ABOUT AVL - DEAD LINK are active at this moment.

 

So it works this way with everything in the same directory,

but I want it to load the content from a subdirectory /test/pages/content.php

 

This is the PHP code I am using

<?php

// Check if page has been requested
if (!isset($_GET['content'])) { // Page has not been requested, show default page
$content = 'default.php';

} else { // Page has been requested, validate page exists, show page
$content = $_GET['content'].'.php';

} // End if page has been requested

// Check to see if page exists
if (file_exists($content)) { // Page exists

// Show page
include("$content");

} else { // Page doesn't exist

echo '<br>';
echo '<br>';
echo '<br>';
echo 'Sorry, the page that you are trying to access does not exist.<br>';
echo 'Please return to <a href="http://www.avlscotland.com/test/index.php">AVL Scotland</a><br>';
echo '<br>';
echo 'Thank you.<br>';
} // End if page exists
?>

 

I'm accessing the pages using links like

<a class="menuactive" href="test/index.php?content=avl">Home</a>

 

What I'm struggling to do is make the PHP look for the content files in a sub-directory.

 

Look forward to any help you can offer.

 

Thanks,

 

Ross

Link to comment
https://forums.phpfreaks.com/topic/223546-includes-changed-by-_variable/
Share on other sites

Sorry my bad with the above post.. Looking at it further you can do this:

 

$content = "/pages/";

if (!isset($_GET['content'])) { // Page has not been requested, show default page
$content .= 'default.php';

} else { // Page has been requested, validate page exists, show page
$content .= $_GET['content'].'.php';

}

 

I've then apprended the filename to the content variable. So for the default link, $content will be equal to '/pages/default.php'

 

Does that help?

 

Denno

@denno

 

Thanks for your input, though that didn't seem to help.

 

I have started again with a very basic page to test the script, which is working better now.

The default page is loading fine from sub-dir.

 

The problem I'm having now is prefixing my variable with the sub-dir...

<!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=ISO-8859-1" />
<title>PHP Include Test</title>
</head>

<body>
<table width="700" border="1" align="center">
  <tr>
    <td><div align="center">
<a href="index.php?content=default">Home</a>
<a href="index.php?content=one">One</a>
<a href="index.php?content=two">Two</a>
</div></td>
  </tr>
  <tr>
    <td><div align="center">
<?php

// Check if page has been requested


if (!isset($_GET['content'])) { // Page has not been requested, show default page
$content .= 'pages/default.php';

} else { // Page has been requested, validate page exists, show page
$content .= $_GET['$content'].'.php';

} // End if page has been requested

// Check to see if page exists
if (file_exists($content)) { // Page exists

// Show page
include("$content");

} else { // Page doesn't exist

echo '<br>';
echo '<br>';
echo '<br>';
echo 'Sorry, the page that you are trying to access does not exist.<br>';
echo 'Please return to <a href="http://www.avlscotland.com/phptest/">AVL Scotland</a><br>';
echo '<br>';
echo 'Thank you.<br>';
} // End if page exists
?>
</div></td>
  </tr>
  <tr>
    <td><div align="center">FOOTER</div></td>
  </tr>
</table>
</body>
</html>

 

At this line

$content .= $_GET['$content'].'.php';

it loads fine in the same directory.

I'm trying to get something like

$content .= $_GET['pages/$content'].'.php';

or

$content .= ['.pages/']$_GET['$content'].'.php';

 

So close, but seems so far.

Looking forward to some help.

 

Ross

 

Wait sorry, I've just realized what's happening there. I probably should have tried to comprehend the code before I suggested anything lol.

 

Try this:

$content .= "/pages/";
$content .= $_GET['$content'];
$content .= ".php";

 

That should hopefully work a bit better :). You can put them all on the same line if you want, but it's easier to put them on the 3 lines, at least for me :).

 

Denno

Ok can you give me a short brief as to what you want to achieve here?

 

Do you want the index page to link to another php page that is in the sub folder pages? Or is the content pulled from a php page and display in the index page?

 

Denno

The page

avlscotland.com/phptest/index.php

contains the Header, Navigation, Space for Include, Footer

 

When you load the page, the include is

avlscotland.com/phptest/pages/default.php

 

All other content will reside in

avlscotland.com/phptest/pages

 

When I click the links in the Navigation of

avlscotland.com/phptest/index.php

They put a variable in the URL which I'm trying to use the PHP to load the /pages/ with the corresponding name

 

Does that all make sense?

 

Thank you again for your continued help.

Ross

 

Alright, so it will depend on where you put the line of code that says include (blah blah)..

 

If you put that line in the php file that is in /phptest then you will need to specify the path to the file which is /pages/page.php.

 

If the include code line is in the default.php file, then you don't say /pages/page.php, as this will look for another folder called pages. You just say page.php (so $_GET["content"] etc etc)

 

I think it's only a path issue that you're having.. I'll continue to try and help :).

 

Denno

 

Assuming the other scripts are in the pages/ directory as well . . .

} else { // Page has been requested, validate page exists, show page
if( ctype_alnum($_GET['content']) ) {  // do some basic validation to prevent non-alphanumeric chars (like ../..)
	$content = "pages/{$_GET['content']}.php";
}
}

@denno

Yes that is how the files are named. default.php is in there also.

 

There is some good news though...

Pikachu2000 has just solved the problem.

 

@Pikachu2000 - Thank you

 

I always thought it was a syntax thing in

"pages/{$_GET['content']}.php";

Over the past 2 days 12-15 developers have tried to tell me it was all wrong and try other ways.

 

Thank you both, a lot.

 

Ross

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.