homepage Welcome to WebmasterWorld Guest from 23.23.8.131
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / WebmasterWorld / New To Web Development
Forum Library, Charter, Moderators: brotherhood of lan & mack

New To Web Development Forum

    
Php includes?
infernofiasco

10+ Year Member



 
Msg#: 1231 posted 6:09 pm on Feb 24, 2004 (gmt 0)

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!

 

grahamstewart

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 1231 posted 7:04 pm on Feb 24, 2004 (gmt 0)

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)

infernofiasco

10+ Year Member



 
Msg#: 1231 posted 7:09 pm on Feb 24, 2004 (gmt 0)

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

jatar_k

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



 
Msg#: 1231 posted 7:16 pm on Feb 24, 2004 (gmt 0)

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

infernofiasco

10+ Year Member



 
Msg#: 1231 posted 7:18 pm on Feb 24, 2004 (gmt 0)

Will this sort out my undefined index problem?

jatar_k

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



 
Msg#: 1231 posted 7:19 pm on Feb 24, 2004 (gmt 0)

what is on line 2?

infernofiasco

10+ Year Member



 
Msg#: 1231 posted 7:51 pm on Feb 24, 2004 (gmt 0)

if ($_REQUEST['link'] == "page2"){

grahamstewart

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 1231 posted 8:19 pm on Feb 24, 2004 (gmt 0)

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.

grahamstewart

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 1231 posted 9:16 pm on Feb 24, 2004 (gmt 0)

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'?>


infernofiasco

10+ Year Member



 
Msg#: 1231 posted 8:22 am on Feb 25, 2004 (gmt 0)

This is what I have done, i was tryin to get the menu to work first :) Iam going to try the array version now..!

infernofiasco

10+ Year Member



 
Msg#: 1231 posted 9:00 am on Feb 25, 2004 (gmt 0)

That worked a treat! thank you all very much!

grahamstewart

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 1231 posted 9:13 am on Feb 25, 2004 (gmt 0)

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / WebmasterWorld / New To Web Development
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved