Forum Moderators: mack
<?
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!
$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)
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
/* 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.
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'?>
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.