如何在其他php页面的字符串变量中获取一个php页面的输出

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.

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";

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();
?>