2

Is it possible to use a POST variable on the buffered script? The code is the following

<?php
header('Content-type: application/json');

$someVar= $_POST["var"];

ob_start();
require "someScript.php?var=" . $someVar; // instead of passing through GET, is there a way to use the $someVar directly?
$output = ob_get_clean();
echo $output;
?>

Note: I've already tried to access $someVar directly from someScript but without success. For that is why I'm asking. Thanks

EDIT: Possible someScript.php

<?php
    header('Content-type: application/json');
    require_once("BD.php");
    require_once("json/json_header.php");
    require_once("tools.php");
    require_once('class.phpmailer.php');

    $BD = new BDInfo(); 


    function encodeIDs($id1,$id2){

        // doesn't matter
    }

    $response = array("errorCode" => 0, "errorDesc" => "No Error");


    if(isset($someVar)){
        try{
            $link = mysqli_connect($BD->BDServer, $BD->BDUsername, $BD->BDPassword);

            if( !$link )
                throw new Exception( "..." );

            if( !mysqli_select_db($link, $BD->BDDatabase) )
                throw new Exception( "..." );

            $SQL="SELECT (...) us.VAR='" . $someVar . "';";

            $RS = mysqli_query($link,$SQL);
            if($RS && $row = mysqli_fetch_array($RS)){
                // process query result
            }
            else
            {
                $response["errorCode"]=4;    
                $response["errorDesc"]="Error descr";   
            }
            mysqli_close($link);

        }catch (Exception $e) {
            $response["errorCode"]=1;
            $response["errorDesc"]="Database Error: " . $e->getMessage();
        }
    }else{
        $response["errorCode"]=2;
        $response["errorDesc"]="Invalid Parameters";
    }

    echo json_encode($response);
?> 

I get Invalid Parameters, showing that isset($var) failed

6
  • Have you tried it out? What have you come with so far? Commented Mar 14, 2012 at 11:24
  • possible duplicate of PHP - include() file not working when variables are put in url? Commented Mar 14, 2012 at 11:26
  • @sandeep, what i'm trying to do is to use a variable on an imported script. I thought I made it clear Commented Mar 14, 2012 at 11:36
  • I think @Yotaware guessed best what you are trying to do, so give that answer a try... Commented Mar 14, 2012 at 11:52
  • @jose Please post the code in someScript.php, too. Commented Mar 14, 2012 at 12:08

3 Answers 3

3

The variable $someVar is already available to you in someScript.php. You should not use a query string to do transfer it.

If you want to avoid modifying someScript.php for some reason then you can do this:

$_GET['var'] = $someVar; 

Or just this:

$_GET['var'] = $_POST['var'];
Sign up to request clarification or add additional context in comments.

2 Comments

I can't access $someVar on someScript.php. I've tried just now.
It should work. Try to setup a much simpler example and see if it works then. See here for an example.
1

When you include a script with include or require, that script has access to all variables in scope at the inclusion site. In your example, you can access $someVar directly from someScript.php (you could also directly access $_POST or anything else).

1 Comment

But not with that output buffer he uses.
1

Yes, you can.

Since require() works similar to include(), the included file inherits the variable scope.

From the manual:

"When a file is included, the code it contains inherits the variable scope of the 
line on which the include occurs. Any variables available at that line in the 
calling file will be available within the called file, from that point forward. 
However, all functions and classes defined in the included file have the global 
scope."

Imagine an include (or require) as that the included code replaces the line include ....

Comments

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.