Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > PHP include question...

PHP include question...
Thread Tools
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
Jan 18, 2004, 02:33 PM
 
Hey guys,

I remember a while ago there was a tutorial on how to do this:

You have a php url like 'index.php?inc=contact'. The 'contact' is a html page called contact.php. In the index.php I would have a basic template, and a little bit of php code where I want the contact bit to go. In theory, I could replace '=contact' with '=about', and it would include about.html. Im not very good at explaining it, but I hope you can help .

Thanks,
Oliver
     
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Jan 18, 2004, 03:55 PM
 
Something like this would work:

[php]
<?php

if (isset($_REQUEST['inc']))
{
if ( $_REQUEST['inc'] == "contact" )
{
require_once('contact.php');
}
// do other includes here
}
?>
[/php]

I'd avoid any function that generically runs include on an whatever you pass, like this:

[php]
require_once($_REQUEST['inc'] . ".php");
[/php]

There could be security issues with something like that.
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
Jan 18, 2004, 04:17 PM
 
Thanks a lot :-) And I remember someone warning me about security issues.

Anywho, thanks
Oliver
     
Occasionally Useful
Join Date: Jun 2001
Location: Liverpool, UK
Status: Offline
Reply With Quote
Jan 18, 2004, 04:24 PM
 
here's 2 methods for ya, both with their own pros/cons: [php]<?php
switch ($_GET['id']) {
// first case
case "1": $inc = 'pages/about_me.inc.php';
break;
// second case
case "2": $inc = 'pages/contact.inc.php';
break;
// third case
case "3": $inc = 'pages/links.inc.php';
break;
// fourth case
case "4": $inc = 'pages/projects.inc.php';
break;
// if no id is specified
default: $inc = 'pages/about_me.inc.php';
break;
}
include ($inc);
?>[/php]...or...[php]<?
if (empty($_GET["id"])) {
$page_name = 'pages/about_me.inc.php';
} else {
$page_name = 'pages/' . $_GET["id"] . '.inc.php';
}
include($page_name);?>[/php]
"Have sharp knives. Be creative. Cook to music" ~ maxelson
     
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
Jan 19, 2004, 03:44 PM
 
Originally posted by Arkham_c:
Something like this would work:

[php]
<?php

if (isset($_REQUEST['inc']))
{
if ( $_REQUEST['inc'] == "contact" )
{
require_once('contact.php');
}
// do other includes here
}
?>
[/php]

I'd avoid any function that generically runs include on an whatever you pass, like this:

[php]
require_once($_REQUEST['inc'] . ".php");
[/php]

There could be security issues with something like that.
Thanks all for the code, I decided to use this:

[php]
<?php
if (isset($_REQUEST['inc']))
{
if ( $_REQUEST['inc'] == "contact" )
{
require_once('contact.php');
}
}
?>
[/php]

But I have a quick Q, how can I set which page will load on default? I notice on phillzilla's code:

[php]
default: $inc = 'pages/about_me.inc.php';
[/php]

That it has a default tag (I couldn't get this code to work for some reason), how would I do it for the other code?

Thanks,
Oliver
     
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Jan 19, 2004, 08:53 PM
 
[php]
$default_inc = "index.php";

if (isset($_REQUEST['inc']))
{
if ( $_REQUEST['inc'] == "contact" )
{
require_once('contact.php');
}
elseif ( $_REQUEST['inc'] == "about" )
{
require_once('about.php');
}
else
{
require_once($default_inc);
}
}
else
{
require_once($default_inc);
}
[/php]
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
Jan 20, 2004, 09:53 AM
 
Originally posted by Arkham_c:
[php]
$default_inc = "index.php";

if (isset($_REQUEST['inc']))
{
if ( $_REQUEST['inc'] == "contact" )
{
require_once('contact.php');
}
elseif ( $_REQUEST['inc'] == "about" )
{
require_once('about.php');
}
else
{
require_once($default_inc);
}
}
else
{
require_once($default_inc);
}
[/php]
Could I just ask why you need 2 of:

[php] else
{
require_once($default_inc);
}
[/php] these?
     
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Jan 20, 2004, 10:00 AM
 
Originally posted by iOliverC:
Could I just ask why you need 2 of:

[php] else
{
require_once($default_inc);
}
[/php] these?
The first is inside the block that says it has 'inc' in the request, the second is in the else block. In the first case, 'inc' was provided, but it's not a valid value. In the second, 'inc' was not provided. You could set a flag instead and display the default based on the flag.
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
Jan 20, 2004, 10:34 AM
 
Thanks for all the help .

Thanks,
Oliver
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:51 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2