0

I expected the followingcode would create a file, test.txt, in the same directory of the loading script when the page is accessed. But it doesn't. Nothing happens. Could somebody tell what is wrong with this code? Does it work fine in your environment?

<?php

if (isset($_POST['cache']) && $_POST['cache'] === true) {
    $file = dirname(__FILE__) . '/test.txt';
    $current = time() . ": John Smith\r\n";
    file_put_contents($file, $current,FILE_APPEND);
    return;
} 

curl_post_async(selfurl(), array('cache' => true));
echo 'writing a log in the background.<br />';
return;


function curl_post_async($url, $params) {
    //http://stackoverflow.com/questions/124462/asynchronous-php-calls
    foreach ($params as $key => &$val) {
      if (is_array($val)) $val = implode(',', $val);
        $post_params[] = $key.'='.urlencode($val);
    }
    $post_string = implode('&', $post_params);

    $parts=parse_url($url);

    $fp = fsockopen($parts['host'],
        isset($parts['port'])?$parts['port']:80,
        $errno, $errstr, 30);

    $out = "POST ".$parts['path']." HTTP/1.1\r\n";
    $out.= "Host: ".$parts['host']."\r\n";
    $out.= "Content-Type: application/x-www-form-urlencoded\r\n";
    $out.= "Content-Length: ".strlen($post_string)."\r\n";
    $out.= "Connection: Close\r\n\r\n";
    if (isset($post_string)) $out.= $post_string;

    fwrite($fp, $out);
    fclose($fp);
}
function selfurl() {
    // http://www.weberdev.com/get_example.php3?ExampleID=4291
    $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
    $protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
    $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
    return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
}
function strleft($s1, $s2) {
    return substr($s1, 0, strpos($s1, $s2));
} 
?>
Teno
  • 2,582
  • 4
  • 35
  • 57
  • 1
    Your `curl_post_async()` function is a serious misnomer. It has nothing to do with cURL, neither is it asynchronous. – DaveRandom Sep 12 '12 at 10:55
  • Also your `selfurl()` will produce invalid URLs for HTTPS requests and goes all round the houses to accomplish something much simpler than your making it. `function selfurl() { return 'http'.(!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ? 's' : '').'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; }` will suffice (assuming Apache) – DaveRandom Sep 12 '12 at 11:05
  • 1
    Thanks Dave for your insight. Regarding `curl_post_async()` maybe you should comment it in http://stackoverflow.com/questions/124462/asynchronous-php-calls as well. – Teno Sep 12 '12 at 19:36

1 Answers1

1

Problem

The error in your script is in the following

if (isset($_POST['cache']) && $_POST['cache'] === true) {
    $file = dirname(__FILE__) . '/test.txt';
    $current = time() . ": John Smith\r\n";
    file_put_contents($file, $current,FILE_APPEND);
    return;
}

$_POST['cache'] === true would try to validated of cache with same type as boolean but $_POST['cache'] would actually output 1 when posted over http using current method

Solution

if (isset($_POST['cache'])) {
    if ($_POST['cache'] == true) {
        $file = dirname(__FILE__) . '/test.txt';
        $current = time() . ": John Smith\r\n";
        file_put_contents($file, $current, FILE_APPEND);
    }
    return ;
}
Baba
  • 94,024
  • 28
  • 166
  • 217
  • Still nothing happens. I'd like to know if the log file is created in your environment. – Teno Sep 12 '12 at 19:35