Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 198
  • Last Modified:

How to include/require a page in another page

I'm trying to include this page below:
http://gryphon.murdoch.edu.au/~group13/kalamunda/displayarticle.php?area=front

into this page here:
http://gryphon.murdoch.edu.au/~group13/kalamunda/test.php
.. so i used...
<?php
include ('http://gryphon.murdoch.edu.au/~group13/kalamunda/displayarticle.php?area=front');
?>

(1) Is there a way not to include the full path and let it auto detect?
(2) When i click on link which is in the include page. the url becomes test.php?id=1
instead of displayarticle.php?id=1

is there a way to fix it to link to displayarticle.php instead of test.php?
0
sivil
Asked:
sivil
  • 2
3 Solutions
 
DoppyNLCommented:
you can also include files relative to the current location.
Downside is that you won't be able to use parameters after ?
Upside is that you can use the global variables in the script.


regarding (2):
Make sure that the script that outputs those links, links to displayarticle.php, and not to something else.
0
 
ramonklownCommented:
Hey

There are 2 possibilities one is you can link to another page and send out a header
Example the link is
http://gryphon.murdoch.edu.au/~group13/kalamunda/displayarticle2.php
and inside that page there is a header going to
http://gryphon.murdoch.edu.au/~group13/kalamunda/displayarticle.php?area=front

instead of include you can user require... which is a little better when thinking about security risks.... (after all it's never secure anyways)

Post the "link code" so we can see what is your problem there.... of just post the hole page to see what is going on with the posting os linking...
0
 
ramonklownCommented:
forgot to post the link to header section http://www.php.net/header
0
 
shmertCommented:
There's an important thing to note when you include() a file with a full URL.   PHP is sending a new request out to the webserver to get that file.  This is most definitely not the most efficient way to do this.

I'd recommend creating a third php file for generating article HTML, which is never called directly from the webserver.  This third file is shared by both your test.php and displayarticle.php pages.  Then you can use include() with relative paths to get to this third page, and reuse the code.

Example contents of files:

# test.php
<html>
<head><title>this is a test page</title></head>
<body>
<?php
$areaToDisplay = 'front';
include ('_article.php');
?>
</body>
</html>

# displayarticle.php
<html>
<head><title>Showing exciting article stuff</title></head>
<body>
<?php
$areaToDisplay = $_REQUEST['area'];
include ('_article.php');
?>
</body>
</html>

# _article.php
# this does the fetching and generating of the article
if (!isset($areaToDisplay)) die ("You must specify an area to display");
// output article here...
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now