Welcome to WebmasterWorld Guest from 54.145.166.247

Forum Moderators: brotherhood of lan & mack

Message Too Old, No Replies

Php includes?

   
6:09 pm on Feb 24, 2004 (gmt 0)

10+ Year Member



iam trying to use php includes so when i click on a menu link it will load the page in the middle between a header and footer.

<?
if ($link == page2){
$page="page2.html";
}
elseif($link == page3){
$page="page3.html";
}
else{
$page="page1.html";
}
?>
<html>
<head>

<title> index.php </title>
</head>
<body bgcolor="#999999">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td colspan="3"><h1>The Template (index.html)</h1></td>
</tr>
<tr>
<td align="center" valign="top"><a href="index.php?link=page1"> Page 1 </a></td>
<td align="center" valign="top"><a href="index.php?link=page2"> Page 2 </a></td>
<td align="center" valign="top"><a href="index.php?link=page3"> Page 3 </a></td>
</tr>
</table>
<? include($page);?>
</body>
</html>

When i use this i get errors:

Notice: Undefined variable: link in c:\inetpub\wwwroot\include\index.php on line 2

Notice: Use of undefined constant page2 - assumed 'page2' in c:\inetpub\wwwroot\include\index.php on line 2

Notice: Undefined variable: link in c:\inetpub\wwwroot\include\index.php on line 5

Notice: Use of undefined constant page3 - assumed 'page3' in c:\inetpub\wwwroot\include\index.php on line 5

How can i fix that!

7:04 pm on Feb 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is your problem..

$link == page2

..you don't have any quotes around page2 so PHP is assuming that page2 is a constant. Also $link doesn't exist, probably because your setup has register_globals turned off.

So instead use the $_GET super global try...

if ($_GET['link'] == 'page2'){

(and likewise for the other tests)

7:09 pm on Feb 24, 2004 (gmt 0)

10+ Year Member



Ive had a fiddle with requesting:

if ($_REQUEST['link'] == "page2"){
$page="page2.html";

Now iam getting this!

Notice: Undefined index: link in c:\inetpub\wwwroot\include\index.php on line 2

7:16 pm on Feb 24, 2004 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



another option to streamline it a little

Since the default is page1.html you could use the contents of the var directly.

if (!isset($_GET['link']) $_GET['link'] == "") $link = "page1";
else $link = $_GET['link'];
$page = $link . '.html';

or if you only want $link to equal only 1 of your 3 pages

if ($_GET['link']!= "page2" && $_GET['link']!= "page3") $link = "page1";
else $link = $_GET['link'];
$page = $link . '.html';

so there are a few ways to do it.

remember to change the broken pipe to a real pipe

7:18 pm on Feb 24, 2004 (gmt 0)

10+ Year Member



Will this sort out my undefined index problem?
7:19 pm on Feb 24, 2004 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



what is on line 2?
7:51 pm on Feb 24, 2004 (gmt 0)

10+ Year Member



if ($_REQUEST['link'] == "page2"){
8:19 pm on Feb 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about approaching the problem like this...

/* This array controls the mapping of link text to page.
* There should always be a value for 'default' */
$link2page = array
(
'default' => 'page1.html',
'page1' => 'page1.html',
'page2' => 'page2.html',
'contact' => 'contact_us.html'
);

if ( isset($_GET['link']) and isset( $link2page[$_GET['link']] ) )
$page = $link2page[$_GET['link']];
else
$page = $link2page['default'];

That way you have a nice clear array showing the mapping.

9:16 pm on Feb 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



By the way, it should be pointed out that this is a slightly strange way to do templating with php includes.

A more common approach is to create one or more files with all the common code in them and then let you other pages include them.

So you might have two files like this...

header.php:


<html>
<head>
<title> index.php </title>
</head>
<body bgcolor="#999999">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td colspan="3"><h1>The Template (index.html)</h1></td>
</tr>
<tr>
<td align="center" valign="top"><a href="index.php?link=page1"> Page 1 </a></td>
<td align="center" valign="top"><a href="index.php?link=page2"> Page 2 </a></td>
<td align="center" valign="top"><a href="index.php?link=page3"> Page 3 </a></td>
</tr>
</table>

footer.php:


</body>
</html>

..and then your remaining pages would look something like this..

page1.php:


<?php include 'header.php'?>

<h1>Page One</h1>
<p>
This is page one.
</p>

<?php include 'footer.php'?>

8:22 am on Feb 25, 2004 (gmt 0)

10+ Year Member



This is what I have done, i was tryin to get the menu to work first :) Iam going to try the array version now..!
9:00 am on Feb 25, 2004 (gmt 0)

10+ Year Member



That worked a treat! thank you all very much!
9:13 am on Feb 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jatar_k:

I wouldn't recommend your first method..

if (!isset($_GET['link'])  $_GET['link'] == "") $link = "page1";  
else $link = $_GET['link'];
$page = $link . '.html';

..because a clever user might figure out what you are doing and use this to read files off your system that should not be accessible to normal users. (e.g. admin or member only pages).

Its always better to check user input against what you expect, so your second method or the array method I suggested are a much safer.