0

I have this code:

function WriteLog($offer,$values) {
    $Filename = "./".$offer.date('Ymd').".txt";
    $fh = fopen($Filename, 'a') or die("can't open file");
    $filecontent = $values;
    $filecontent .= PHP_EOL;
    fwrite($fh,$filecontent);
    fclose($fh);
}

$aff = "100";

if (isset($HTTP_RAW_POST_DATA)) {
    echo $aff;

    WriteLog('events',$aff);

}

Here's the problem: The $aff variable is not accessible. Echoing the $aff inside the if ISSET statement doesn't display the value of the $aff. What's probably causing this? How can I access the value of the $aff variable? Note that the WriteLog() function was executed but the $aff doesn't have a value. Please help me with this. Thanks!

maikelsabido
  • 1,253
  • 4
  • 18
  • 37
  • check this http://stackoverflow.com/a/12997460/1699833 – Harish Singh Nov 25 '13 at 08:19
  • Are you sure that `WriteLog` is executed in this part of the script? Because there seems nothing wrong with it if `HTTP_RAW_POST_DATA` is set. – Peon Nov 25 '13 at 08:22
  • Yes, the WriteLog was executed. It created a log file but it was empty. – maikelsabido Nov 25 '13 at 08:22
  • Plain and simple: this should work just fine. There's no issue whatsoever with the `$aff` variable. It's not possible for the code to behave as you decribe. You have some other problem in your actual code. – deceze Nov 25 '13 at 08:22
  • 1
    I suggest you debug it with `die` or something like that, to check, at what part, your code breaks, because your problem is clearly **not** at the piece of script you are showing. As I said, the `WriteLog` probably is executed at a different part then the one you are looking at. – Peon Nov 25 '13 at 08:24

3 Answers3

0

$HTTP_RAW_POST_DATA contains the raw POST data.

in your case $HTTP_RAW_POST_DATA doesn't contain any value, so your if condition failed and not print the echo $aff; value.

$aff = "100";

if (isset($HTTP_RAW_POST_DATA)) {
  echo $aff;

  WriteLog('events',$aff);
 }else{
   echo "HTTP_RAW_POST_DATA not set";
}
Krish R
  • 22,583
  • 7
  • 50
  • 59
  • OP wrote that WriteLog was executed, so `$HTTP_RAW_POST_DATA` should be set. – l-x Nov 25 '13 at 08:19
  • The $HTTP_RAW_POST_DATA contains a value. That's why the WriteLog() function was executed. But I'm wondering why the $aff did not contain a value. – maikelsabido Nov 25 '13 at 08:20
0

$HTTP_RAW_POST_DATA requires an ini value to be set, using the input stream should work without any special ini settings and is also the 'prefered' method.

you need to use an alternative method

$postData = file_get_contents('php://input')

the documentation is here

Harish Singh
  • 3,359
  • 5
  • 24
  • 39
0

Its just an idea to check the value of $aff wether it goes inside if check or not.

 $aff = "100";

 if (isset($HTTP_RAW_POST_DATA)) {
    var_dump($aff);
    WriteLog('events',$aff);
 }
 else{
    var_dump($aff); 
 }
Adil Abbasi
  • 3,161
  • 1
  • 40
  • 35