2

I'm dyeing to know how to do this.

Actually I have two php page. Say page1.php and page2.php.

Now say in page1.php we have something like

<?php
  $id=$_GET['id'];
  // do some processing with mysql database
  // do some more processing
  $name="Kumar Ravi"; // this is the name generated using ID received.
  echo $name;
?>

and in page2.php, we have

<?php
  $var=get_the_output('page1.php?id=24');
  echo $var;
?>

How can make something like this, I mean I want to have all the data echoed by another PHP (which can only be called using a GET request) into a string on another PHP page.

I have tried many things but failed. Things I tried are:

  1. file_get_contents ---> failed as it was unable to maintain session present in page2.php to page1.php
  2. require ---> as I don't know how to pass parameter and maintain the session using this. Tried to search everywhere but.. :(

Please help.

3
  • Well if you wanted you could do it with javascript and do an Ajax call from page1 to page2 or the other way around Commented Nov 10, 2011 at 21:06
  • Option 1 should work. Why weren't you able to maintain the session? Are you using cookies to handle the session id or the query string? Commented Nov 10, 2011 at 21:08
  • 1
    @MikeB I'm using $_SESSION and not cookies.. I am a newbie actually.. can you provide an example? Commented Nov 10, 2011 at 21:11

2 Answers 2

4

if the same session exists when loading page1.php and page2.php you should be able to just set the superglobals manually, then if you need to capture the output of an included page, you could do so using output buffering:

$_GET['id'] = '24';
ob_start();
require("page1.php");
$out = ob_get_clean(); //$out = "Kumar Ravi";
Sign up to request clarification or add additional context in comments.

1 Comment

@Daveo I need to know why it smells? and what are the issues I can face when I use this? Daveo may be you can shed some light on this?
3

You could do something like the following, however be careful:

<?php
$_GET['id'] = 24; // set the id in $_GET so page2 can find it
ob_start();
include('page1.php');
$var = ob_get_clean();
?>

2 Comments

Thanks a lot for your solution, but may i know what do you mean by be careful? is there any security or other issues when using this approach?
No security issue. Just meant be careful passing parameters like that. Especially if you have register globals on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.