For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > May 2004 > Re: php includes









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: php includes
Janwillem Borleffs

2004-05-20, 5:30 pm

Moziah wrote:
> Probably a simpe question with a simple answer. I have a page with a
> menu and an include which contains the content of the page. Is it
> possible to click a link on the menu and have the include bring up a
> different file?


You are cross-posting (sending the post seperately to multiple newsgroups).
Don't do that. Replied in alt.comp.lang.php.


JW



Janwillem Borleffs

2004-05-20, 5:30 pm

Janwillem Borleffs wrote:
> Moziah wrote:
>
> You are cross-posting (sending the post seperately to multiple
> newsgroups). Don't do that. Replied in alt.comp.lang.php.
>


Sorry, wasn't meant for you. To answer your question, yes it's possible:

When the menu contains a link like:

main.php?main=navigation

The code in main.php might look like this:

<?

$dir = "pages/";

if (isset($_GET['main'])) {
$filename = $dir.$_GET['main'].'php';
if (file_exists($filename)) {
include $filename;
exit;
}
}

include "{$dir}default.php";

?>


HTH,
JW



Berislav Lopac

2004-05-21, 3:30 am

Janwillem Borleffs wrote:
> Moziah wrote:
>
> You are cross-posting (sending the post seperately to multiple
> newsgroups). Don't do that. Replied in alt.comp.lang.php.


Jan, you answered that to two separate posts. Actually, crossposting is OK;
multiposting is not.

Crossposting means that one message has two or more groups at the header,
and it gets visible in both groups. Multiposting is posting separate
messages, with same contents, on several groups.

Berislav

--
If the Internet is a Marx Brothers movie, and Web, e-mail, and IRC are
Groucho, Chico, and Harpo, then Usenet is Zeppo.


Janwillem Borleffs

2004-05-21, 7:30 am

Berislav Lopac wrote:
> Jan, you answered that to two separate posts. Actually, crossposting
> is OK; multiposting is not.
>


I realized that as soon as I pressed the send button. Therefore, I
appologized to the OP of this thread.


JW



Janwillem Borleffs

2004-05-21, 7:30 am

Moziah wrote:
> Would I be right in saying that I would have to write that whoe batch
> of code for every link in the menu?


No, you only have to change the structure. You will need only 1 main.php,
which can be used to include all existing files.

And, of course, you will have to set up the navigation links.

Security wise, it's recommended to validate each include file to see if it
resides in a specific directory. Otherwise, people would be able to see
files which they shouldn't.


JW



Moziah

2004-05-21, 2:31 pm

> Moziah wrote:
>
> No, you only have to change the structure. You will need only 1
> main.php, which can be used to include all existing files.
>
> And, of course, you will have to set up the navigation links.
>
> Security wise, it's recommended to validate each include file to see
> if it resides in a specific directory. Otherwise, people would be able
> to see files which they shouldn't.


Here is the code for my page...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<head>
<title>Softek Designs - Helping your company reach the web</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link href="base.css" rel="stylesheet" type="text/css">
</head>

<body>

<div class="sectiont">
<?php include ('content/misc/title.html'); ?>
</div>

<div class="content">
<?php include ('content/central.html'); ?>
</div>

<div class="sectionl">
<a href="index.php">Central</a>
<a href="about.php">About</a>
</div>

</body>

</html>

What do I need to do to get the about section, stored in
content/about.html, to be showed when the about link is clicked on?
Janwillem Borleffs

2004-05-21, 4:31 pm

Moziah wrote:
> What do I need to do to get the about section, stored in
> content/about.html, to be showed when the about link is clicked on?


<div class="content">
<?
if (isset($_GET['page']) &&
file_exists("content/{$_GET['page']}.html")) {
include "content/{$_GET['page']}.html";
} else {
include 'content/central.html';
}
?>
</div>

<div class="sectionl">
<a href="index.php">Central</a>
<a href="index.php?page=about">About</a>
</div>


JW



Moziah

2004-05-21, 5:30 pm

> Moziah wrote:
>
> <div class="content">
> <?
> if (isset($_GET['page']) &&
> file_exists("content/{$_GET['page']}.html")) {
> include "content/{$_GET['page']}.html";
> } else {
> include 'content/central.html';
> }
> ?>
> </div>
>
> <div class="sectionl">
> <a href="index.php">Central</a>
> <a href="index.php?page=about">About</a>
> </div>
>
>
> JW
>
>
>
>


That's works great, much appreciated. Thank you!
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com