开发者

PHP File Get Contents getting PHP?

I'm using PHP's file_get_contents in a way that makes it an API without XML. I've done this several times before, but today, it's outputting the file's ACTUAL PHP as opposed to the output HTML which is what I'm trying to get!

Here's the code:

File I'm getting, udp.php

  <?php
session_start();
$user = $_SESSION['xxxxxx'];
require("connect.php");
$data = mysql_query("SELECT * FROM xxx WHERE xxx='$xx'");
$row = mysql_fetch_assoc($data);
/* Fetch Array */
$email = $row['email'];
$na开发者_开发百科me = $row['firstname'].' '.$row['lastname'];
$location = $row['location'];
$dob = $row['dob'];
$gender = $row['gender'];
$dp = $row['dp'];
$joindate = $row['joindate'];
$var = $email.'@@@@'.$name.'@@@@'.$location.'@@@@'.$dob.'@@@@'.$gender.'@@@@'.$dp.'@@@@'.$joindate;
echo $var;
?>

And I'm using this:

<?

    $getdata = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/udp.php');
    echo $getdata; 
?>

To get the file contents from udp.php, but the problem is, I'm not getting $var, I'm getting the ACTUAL PHP! The return data is the exact PHP file contents. The actual udp.php file renders $var the way I want it to, but when getting the file, it renders the exact PHP.

That is kind of confusing to me :S

Any Ideas?

Thanks! :)


$_SERVER['DOCUMENT_ROOT'] contains a local filesystem path. The PHP interpreter is never being invoked, so you just get the file contents.

You either need to file_get_contents() it via a URL, or capture the output from include() with some buffering and store the value that way.


Use include() to get the interpreted PHP file.


That is how it's supposed to work. If you did file_get_contents on an executable file, would you expect it to execute the file and return the output? Not really.

If you want to process the PHP file and get the resulting output, use include instead.


Honestly, I think you need to read up on programming in general, and PHP specifically. What you can do to fix what you posted is to create a function in udp.php by wrapping the code in a function named something like udp_getdata() {} and then return $var; instead of echo. Then in the other code, you require_once("udp.php"); and then change: $getdata=udp_getdata(); At this point, $get_data should be set to the contents of the return value of the function udp_getdata()

That is not to say that all your code is correct, and will work, mind you. I never got that far.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜