Fsockopen subfolders

If i have a file structure,

index.php - 127.0.0.1
examples - folder
example.php

how to refer example.php using fsockopen


fsockopen("127.0.0.1", 80, $errno, $errstr, 30); - i am getting index.php contents

Building on the example in the PHP manual:


<?php

$fp = fsockopen("127.0.0.1", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\
";
} else {
    $out = "GET /examples/example.php HTTP/1.1\\r\
";
    $out .= "Host: 127.0.0.1\\r\
";
    $out .= "Connection: Close\\r\
\\r\
";
    fwrite($fp, $out);
    while (!feof($fp)) {
        echo fgets($fp, 128);
    }
    fclose($fp);
}

?>

http://www.php.net/manual/en/function.fsockopen.php
http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html

Thanks.
That’s great. That’s what i was looking for.

one more thing i am struck here. i have downloaded a function which is used to post data, this is in,
post.php


<?php

function PostRequest($url, $referer, $_data) {
 
    // convert variables array to string:
    $data = array();    
    while(list($n,$v) = each($_data)){
        $data[] = "$n=$v";
    }    
    $data = implode('&', $data);
    // format --> test1=a&test2=b etc.
 
    // parse the given URL
    $url = parse_url($url);
    if ($url['scheme'] != 'http') { 
        die('Only HTTP request are supported !');
    }
 
    // extract host and path:
    $host = $url['host'];
    $path = $url['path'];
 
    // open a socket connection on port 80
    $fp = fsockopen($host, 80);
 
    // send the request headers:
    fputs($fp, "POST $path HTTP/1.1\\r\
");
    fputs($fp, "Host: $host\\r\
");
    fputs($fp, "Referer: $referer\\r\
");
    fputs($fp, "Content-type: application/x-www-form-urlencoded\\r\
");
    fputs($fp, "Content-length: ". strlen($data) ."\\r\
");
    fputs($fp, "Connection: close\\r\
\\r\
");
    fputs($fp, $data);
 
    $result = ''; 
    while(!feof($fp)) {
        // receive the results of the request
        $result .= fgets($fp, 128);
    }
 
    // close the socket connection:
    fclose($fp);
 
    // split the result header from the content
    $result = explode("\\r\
\\r\
", $result, 2);
 
    $header = isset($result[0]) ? $result[0] : '';
    $content = isset($result[1]) ? $result[1] : '';
 
    // return as array:
    return array($header, $content);
}
 
 
 
/*
** The example:
*/
 
// submit these variables to the server:
$data = array(
    'param1' => '1',
    'param2' => '2',
    'param3' => '3'
);
 
// send a request to example.com (referer = jonasjohn.de)
/*
list($header, $content) = PostRequest(
    "http://www.example.com/",
    "http://www.jonasjohn.de/",
    $data
);
*/ 

list($header, $content) = PostRequest(
    "http://localhost/examples/example.php",
    "http://localhost/examples/example.php",
    $data
);

 
// print the result of the whole request:
print $content;
 
print $header; //--> prints the headers

?>

example.php


print_r($_REQUEST);

when i open post.php, i am able to see the contents (parameter values of param1, param2, param3). However when i open http://localhost/examples/example.php iam getting a blank array,
array()

$_REQUEST contains data from $_GET, $_POST and $_COOKIE. It is normal that
you see nothing when you directly access example.php (because you sent no
query string params, no POST data, and presumably no cookies).

I don’t really understand what you’re trying to achieve by directly accessing that file.

$_REQUEST contains data from $_GET, $_POST and $_COOKIE. It is normal that
you see nothing when you directly access example.php (because you sent no
query string params, no POST data, and presumably no cookies).

I don’t really understand what you’re trying to achieve by directly accessing that file.

i should be getting the values of $param1, $param2, $param3 as i am sending those values when posting.

the example.php is the page i am using to display in a browser and the values of $param1, $param2, $param3 are values i have to display in it

You are getting those values when POST-ing. When you directly access example.php,
though, you are not POST-ing anything, hence the empty array.

I still don’t understand what you’re trying to achieve. Maybe you want to save that data in
some database?

Yes, i will be sending those data into database in example.php page. I also would like to display some data in that page and visitors should be able to see those data.