 |
 |
PHP include question...
|
 |
|
 |
|
Grizzled Veteran
Join Date: Jun 2002
Status:
Offline
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
[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
|
|
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
|
|
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
|
|
Thanks for all the help  .
Thanks,
Oliver
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|