PDA

View Full Version : [PHP] PHP includes instead of Iframes



мϊкэ
31-05-2005, 01:20 PM
PHP includes are sometimes used instead of iframes it makes another page appear as though it is actually on the page similar to iframes without all of the scrollbars right making a php include is pretty basic

<?php
include ('yourpage.php');
?>
rite to make the include change without having to make loads of the same page is a bit more complicated
were you want your include to appear put this code

<?php
$_GET['page'];
switch($page)
{
case "home":
include('home.php');
break;
case "news":
include('news.php');
break;
case "tutorials":
include('tutorials.php');
break;
case "contact":
include('contact.php');
break;
case "sitemap":
include('sitemap.php');
break;
case "admin":
include('admin.php');
break;
default:
include('home.php');
break;
}
?>
you can change all of the

case "admin":
include('admin.php');
break;
to the directory of the pages you want and you can also change the name (next to 'case' )
and what you want the default page to be

default:
include('home.php');
break;
now to make the links which will change the include use this:

<a href="index.php?page=home">Home</a>
this would change the include to the page with the 'case' called home
to see an example of this www.xenigma.co.uk

*** Updated ***

I have discovered a new way to use php includes were you do not have to add

case "admin":
include('admin.php');
break;
everytime to use a new page, the code:

<?php
if (!isset($page))
{
include("pages/home.php");
}
if(file_exists($_GET['page'].".php")){
include $_GET['page'].'.php';
}
if(file_exists($_GET['page'].".html")){
include $_GET['page'].'.html';
}
if(file_exists($_GET['page'].".txt")){
include $_GET['page'].'.txt';
}
elseif (isset($page) && !@include("$page"))
{
echo "Error Page not found!";
}
?>

now the code broken down and the meanings


<?php

starts the php script


if (!isset($page))
{
include("pages/home.php");
}

this part sets what the starting page will be example look at the www.xenigma.co.uk index page there is a description of what was used to develop the site and who it is owned by this is the starting page


if(file_exists($_GET['page'].".php")){
include $_GET['page'].'.php';
}
if(file_exists($_GET['page'].".html")){
include $_GET['page'].'.html';
}
if(file_exists($_GET['page'].".txt")){
include $_GET['page'].'.txt';
}

this sets what file extensions can be used when searching for them


elseif (isset($page) && !@include("$page"))
{
echo "Error Page not found!";
}

this is what message will be displayed if the page doesnt exist,


?>

this ends the php script. Now you can display any page were the include is, all as you need to do now is use the file name example in a link
index.php?page=pages/contact/contact that would open in the include either

pages/contact/contact.php
or
pages/contact/contact.html
or
pages/contact/contact.txt

Mentor
31-05-2005, 01:36 PM
I usealy prefer to use if statments.

PLus for a lot of links its far easyer just to use a dinamic include system aka



<?php
$page = basename($page);

if(!$page)
include("start.php");

else
if(file_exists("$page.php"))
{
include("$page.php");
}
else
{
echo "Error. Page not found";
}

?>

wich basicly just includes the value of the $page, with php on the end

so

index.php?page=cat

would include the page cat.php

and

index.php?page=dog

would include the page dog.php


And if the page varilble isnt there aka just

index.php?

it will include start.php or any other page you add in the same place


At the end of the script it aslo checks to see if the file exsists and if it doesnt it shows the error message.

i prefer this method as it saves on codeing and makes it easyer to add pages :D

мϊкэ
31-05-2005, 01:45 PM
i find that slightly more confusing mine was ment to be more basic :p

-JT-
31-05-2005, 03:13 PM
i prefere the if stament also, its alot easier and quicker

мϊкэ
31-05-2005, 03:52 PM
explain to me how it works, and how i can configure it im quite new to php coding :p

Mentor
31-05-2005, 04:11 PM
the if statment or the include system i did.

if works same way as yorus aka


<?php
$_GET['page'];
if($page == "home")
{
include('home.php');
}
elseif($page == "news")
{
include('news.php');
}
else
{
include('home.php');
}

?>

For a short eg..

My script works quite simply (info in comments / code hints)


<?php
$page = basename($page); // get page varibale form url

if(!$page) // if page varible doesnt exist
include("start.php"); // include this

else / if not then do this
if(file_exists("$page.php")) // does the file exists? if yes
{
include("$page.php"); // do this. this incude includes the varible $page with the php exsntion added on
// so the page included is the varible plus the exntion. aka if page = cat it would include cat.php in to the page... if it existed
}
else // if not
{
echo "Error. Page not found"; // show this error message
}

?>

:Woof
30-07-2005, 01:19 PM
ok i have done all of what u said :) but the page loads in a new window not were i want any ideas?

iRoss
30-07-2005, 01:29 PM
Did you put:


<?php
$_GET['page'];
switch($page)
{
case "home":
include('home.php');
break;
case "news":
include('news.php');
break;
case "tutorials":
include('tutorials.php');
break;
case "contact":
include('contact.php');
break;
case "sitemap":
include('sitemap.php');
break;
case "admin":
include('admin.php');
break;
default:
include('home.php');
break;
}
?>

In the content box where you want it to appear?

:Woof
30-07-2005, 01:31 PM
lmao no i put that in the nav area :l

iRoss
30-07-2005, 01:33 PM
:l Why? :S

:Woof
30-07-2005, 01:37 PM
no idea :) still dont work :p page loads in new same window but as full screen

iRoss
30-07-2005, 01:39 PM
Did you use the PHP link?



<a href="index.php?page=home">Home</a>


?

:Woof
30-07-2005, 01:50 PM
yea :s gay php never liked it.

splintercell!
30-07-2005, 02:14 PM
Ok this is a basic overview of how to use it :) An include in php is like an iframe just different. But you place it (like an Iframe) where you want the text to show so as Mentor said the code is:

<?php
$_GET['page'];
if($page == "home")
{
include('home.php');
}
elseif($page == "news")
{
include('news.php');
}
else
{
include('home.php');
}

?>

So to set it out with a naVigation you could do it like so:


<table border="0" cellpadding="0" cellspacing="0" valign="top">
<tr>
<td width="200" bgcolor="99ccff">
LINKS<br>
<a href="index.php?page=home">Home</a></td>
<td width="50"></td>
<td width="450" bgcolor="99ccff" height="100">
<?php
$_GET['page'];
if($page == "home")
{
include('home.php');
}
elseif($page == "news")
{
include('news.php');
}
else
{
include('home.php');
}

?></td>
</tr>
</table>


Tha just give you a navigation box and also a box where the include appears :)

Anderman
30-07-2005, 02:24 PM
Yesss
finnaly a good tutoriul instead of these stupid copie and past ones
:D:D:D
If this dosent get stuck ima stab someone

James did try to explain to me the other day but i just got really confused
Oh well, I'll give it ago again ;D

:Woof
30-07-2005, 02:35 PM
omg u owe me anderman :p i searched for php and i wanted this and i got it posted this is a OLD thread :D

Anderman
30-07-2005, 03:35 PM
I know and it best get stuck pronto or im going to start gatting down moderators

T0X!C-uk
01-08-2005, 02:55 PM
Move to tutorials :)

мϊкэ
01-08-2005, 02:58 PM
wow this threads ancient i cant believe its still getting bumped :O:O:O

Anderman
01-08-2005, 03:49 PM
.................

мϊкэ
01-08-2005, 04:42 PM
Yea but its actually a good tutoriul for once ;)

love you too :s


:O:O:O:O 2 Tutorials pinned OMG!

:Woof
02-08-2005, 09:49 AM
No need to thank me :p i only wanted help and still didnt get it xD

мϊкэ
03-08-2005, 05:12 PM
The tutorial has been updated with an easier way of using include :D ive added it underneath the old tut

Anderman
05-08-2005, 05:29 PM
Yay!
I finnaly added includes to The-Pit
;D

Thanks for the tut man

мϊкэ
05-08-2005, 06:15 PM
That new way i put is so much easier isnt it, ross strangely finds that one harder?

Anderman
05-08-2005, 06:18 PM
I used the old one

<?php
$_GET['page'];
switch($page)
{
case "home":
include('home.php');
break;
case "news":
include('news.php');
break;
case "tutorials":
include('tutorials.php');
break;
case "contact":
include('contact.php');
break;
case "sitemap":
include('sitemap.php');
break;
case "admin":
include('admin.php');
break;
default:
include('home.php');
break;
}
?>
That one

мϊкэ
05-08-2005, 08:03 PM
:O the other ones easier >:~D

iRoss
05-08-2005, 08:05 PM
No it's not . . . Maybe it's just the way you explained it? I don't know. But it doesn't look easier.

Jseb
31-08-2005, 04:43 AM
PHP includes are sometimes used instead of iframes it makes another page appear as though it is actually on the page similar to iframes without all of the scrollbars right making a php include is pretty basic


Sorry for asking this really stupid question but acording to this quote if ever my english got better it is saying that the php navigation took out the '' Iframes '' and place is content where it is but without having any code, its just simply adding the text were u want it? So if i am right u place the code like a simply text like cutenews thing (were u want it to be appear) and then where u place ur navigation u put the link that will activate the php page?

But yeah it would be fun to clarifie this for me if u understand what i said, quite tired today, and yeah nice tutorial throught even if i dont get to much the difference between a php navigation and html navigation unless the coding is really different :P.

leaked
31-08-2005, 01:37 PM
Bad way to go about it... A user could simply request ?page=../../etc/passwd and they could view it (UNIX)!
Make sure you put
include('news.php'); as
include('./path/to/news.php'); otherwise expect your box to be exploited!

Heres the code I use on all of my websites, you should use it too (remember to change the array $pages).
If you have 50odd PHP pages you'll have a big *** PHP code, so heres a loop that will go through an array of pages and include them, also exploit safe.
'name' => './path/to/file', etc.
http://somesite.com/?i=name would load up path/to/file
You can change $GET['x'] accordingly ?x=name

<?php
$default = './i/news.php';
$pages = array('about' => './i/about.php','servers' => './i/servers.php','services' => './i/services.php');
if(array_key_exists($_GET['i'], $pages))
{
foreach($pages as $pageid => $pagename) {
if($_GET['i'] == $pageid && file_exists($pagename))
{
include $pagename;
}
}
} else {
include $default;
}
?>

Mentor
31-08-2005, 04:24 PM
Bad way to go about it... A user could simply request ?page=../../etc/passwd and they could view it (UNIX)!
Make sure you put
include('news.php'); as
include('./path/to/news.php'); otherwise expect your box to be exploited!



How would that help someone all u would need to go back up a diecty is the ../




Heres the code I use on all of my websites, you should use it too (remember to change the array $pages).
If you have 50odd PHP pages you'll have a big *** PHP code, so heres a loop that will go through an array of pages and include them, also exploit safe.
'name' => './path/to/file', etc.
http://somesite.com/?i=name would load up path/to/file
You can change $GET['x'] accordingly ?x=name

<?php
$default = './i/news.php';
$pages = array('about' => './i/about.php','servers' => './i/servers.php','services' => './i/services.php');
if(array_key_exists($_GET['i'], $pages))
{
foreach($pages as $pageid => $pagename) {
if($_GET['i'] == $pageid && file_exists($pagename))
{
include $pagename;
}
}
} else {
include $default;
}
?>

interesting script, as my navigation works by takeing the name then adding an exstention, and a folder on to it, that woulnt work, athogh i may borrow a bit of the script and get the array from the directy commands "/

Chippiewill
09-12-2007, 01:36 AM
I made an addition to the coding as I realised that to get the script to work well you would have to do redirecting junk...

Anyway I realised that the

<?php
if (!isset($page))
{
include("pages/home.php");
?>

Part of the script was a bit annoying since it showed up all the time, so I removed it to:


<?php
if(file_exists($_GET['page'].".php")){
include $_GET['page'].'.php';
}
if(file_exists($_GET['page'].".html")){
include $_GET['page'].'.html';
}
if(file_exists($_GET['page'].".txt")){
include $_GET['page'].'.txt';
}
elseif (isset($page) && !@include("$page"))
{
echo "Error Page not found!";
}
?>

Then create a file called .php (no file name just file type) and that is the default and it shows up when the
$_GET['page'
Variable isnt set

Invent
09-12-2007, 11:11 AM
Very insecure.

Beau
13-12-2007, 10:22 AM
Insecure if one actually uses the code examples above to include filetypes other than PHP. If you limit it to PHP, it's fine. Webservers don't store sensitive information in PHP files. Users might, in the form of database connection scripts, however simply including a file such as this isn't going to achieve a great deal. To improve security further, you could make sure the file exists in the actual directory you are working in only, as oppose to the entire server.

Invent
13-12-2007, 07:41 PM
as oppose to the entire server.

Or as oppose to any site on the internet really.

Beau
14-12-2007, 04:55 AM
Or as oppose to any site on the internet really.

I was under the assumption that you can't use direct URLs when including files (ie. http://www.example.com/example.php'), however I might be wrong. In any case, it would work in exactly the way file_get_contents does, ie it wouldn't retrieve the entire PHP source.

Black_Apalachi
16-12-2007, 10:09 PM
Omg my brain hurts.

You know, if one of you boffons created a website making programme that let you do *EVERYTHING* from the editor view rather than the HTML view, and it still worked fine, you'd be so rich that the Queen would be freiend requesting you and asking for free Thrones :rolleyes:.

Chippiewill
20-12-2007, 04:15 PM
I was under the assumption that you can't use direct URLs when including files (ie. http://www.example.com/example.php'), however I might be wrong. In any case, it would work in exactly the way file_get_contents does, ie it wouldn't retrieve the entire PHP source.

Hmm well my theory is:

When you put an includes for a URL like: /content/content.php

It then converts it to something like

\www\public_html\content\content.php

so you cant include out of the www folder because the server would need to know the server side code to parse so if you did request

http://whatisit.com/content/content.php

when it converts it goes

\www\http://whatisit.com\content\content.php

(roughly speaking of course)

Also i think the server works faster when you include it like

\www\public_html\content\content.php

anyway as it doesnt need to convert

nad
25-05-2009, 12:10 PM
Hi,i tried the 2 code bu it doesn´t work nad if it does it also appear the initial page.help

Invent
04-06-2009, 01:48 PM
Hmm well my theory is:

When you put an includes for a URL like: /content/content.php

It then converts it to something like

\www\public_html\content\content.php

so you cant include out of the www folder because the server would need to know the server side code to parse so if you did request

http://whatisit.com/content/content.php

when it converts it goes

\www\http://whatisit.com\content\content.php (http://whatisit.com%5Ccontent%5Ccontent.php)

(roughly speaking of course)

Also i think the server works faster when you include it like

\www\public_html\content\content.php

anyway as it doesnt need to convert
If allow_furl_open is enabled, you can call include(), include_once(), require() and require_once() on remote URLs.

And I'm pretty sure it doesn't really work faster if you provide the absolute file path - but I may be wrong.

Chippiewill
04-06-2009, 08:57 PM
That was a year (And a half) ago xD, I know ALOT more about php :P

Black_Apalachi
19-07-2009, 11:43 PM
No matter what code I use, all my links just stay at home.php rather than going to their repspective pages, (news.php, values.php, guides.php etc).

The latest thing I tried was this which I found the basis of earlier in the thread:


<?php
$_GET['page'];
if($page == "home")
{
include('home.php');
}
elseif($page == "news")
{
include('news.php');
}
elseif($page == "values")
{
include('values.php');
}
elseif($page == "guides")
{
include('guides.php');
}
elseif($page == "knowledge")
{
include('knowledge.php');
}
elseif($page == "links")
{
include('links.php');
}
else
{
include('home.php');
}

?>

does anyone know how I can make it work?? :eusa_wall

Mentor
19-07-2009, 11:58 PM
$_GET['page'];

should be

$page = $_GET['page'];

RastaLulz
20-07-2009, 12:41 AM
Here's a very basic one I just whipped up.


<?php
if ($_GET[p]) {
$url = $_GET[p];
include($url);
}else{
include ("main.html");
}
?>So the url would be index.php?p=(page).(extension).

If you don't put ?p=(page).(extension) after index.php, it'll include main.php.

Example:
http://yourwebsite.com/index.php?p=home.html (http://yourwebsite.com/index.php?p=home.htnl)

D-Man22
22-07-2009, 06:23 PM
I perfer .htaccess MOD rewrite ;]

L?KE
31-10-2010, 08:19 PM
I didn't read all pages, so I'm not sure that if this has been mentioned but when I used to play around with fansites and such, the reason for using an iframe was simply because you could load pages into the current one without disrupting the radio. With his method it would cause the radio to keep restarting, which is frankly quite irritating.

For a non-radio site though, this is clearly a good method to use.

Chippiewill
31-10-2010, 09:02 PM
I didn't read all pages, so I'm not sure that if this has been mentioned but when I used to play around with fansites and such, the reason for using an iframe was simply because you could load pages into the current one without disrupting the radio. With his method it would cause the radio to keep restarting, which is frankly quite irritating.

For a non-radio site though, this is clearly a good method to use.
Or you have the entire site contained within an iframe with the radio outside the iframe controlled by js from within. Or you could just use javascript to include the content which is safer anyway.

Want to hide these adverts? Register an account for free!