Log in

View Full Version : fread()/fopen()



CBTSlMob1us
05-24-2007, 10:49 PM
Does anyone know if fread()/fopen() are capable of reading/opening "files" such as http://www.sample.com/index.php?step=3 (or the product of any query string) or does it have to be an actual locatable file on the server such as http://www.sample.com/test.html ?

codeexploiter
05-25-2007, 03:10 AM
Why would you want to use fopen/fread to read querystring data? I wonder.

Twey
05-25-2007, 09:48 AM
It is possible, but you need to access it via HTTP (requires url_fopen):
fopen('http://www.sample.com/index.php?step=3');It's more efficient to use include and output buffers, and also allows you to use relative paths:
$_GET['step'] = 3;
ob_start();
include 'index.php';
$page = ob_get_contents();
ob_end_clean();

mbrodin
05-25-2007, 05:13 PM
Hi, CBTSlMob1us!

Here are a code I wrote for you, it require fsockopen and parse_url!

Some of the special of this script is, you can...
1. ... add username and password if it requires, such for memberpages and forums, etc.
2. ... get content from secured line, e.g. "https".
3. ... add your own user-agent name.
4. ... use it to fetch images and other files then for just HTML-documents.

Here it comes ...



<?php

function getContent( $url, $username = '', $password = '', $user_agent = '' )
{

if( $user_agent == '' )
{
$user_agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)';
}

@ini_set('user_agent', $user_agent);

if( substr($url, 0, 7) != "http://" )
{
$url = "http://" . $url;
}

$info = parse_url($url);

$path = $info['path'];
$host = $info['host'];
$query = $info['query'];

if( isset($info['port']) && ( $info['port'] != '' ) )
{
$port = $info['port'];
}
else
{

if( $info['scheme'] == 'https' )
{
$port = 443;
}
else
{
$port = 80;
}

}

$port2 = "";

if( $port == 443 )
{
$port2 = ":" . $port;
}

if($query != '')
{
$url = str_replace("?" . $query, '', $url);

$query = str_replace(array("&"), array("&amp;"), $query);
$url = $url . '?' . $query;
}

$fsocket_timeout = 300;

$errno = 0;
$errstr = "";

if ( !($fsock = @fsockopen($host, $port, $errno, $errstr, $fsocket_timeout)) )
{
die("Can't open <i>" . $url . "</i> with fsockopen!");
}

@fputs($fsock, "GET " . $path . " HTTP/1.1\r\n");
@fputs($fsock, "HOST: " . $host . "\r\n");

if( $username != '' )
{
@fputs($fsock, "Authorization: Basic " . base64_encode( $username . ":" . $password ) . "\r\n");
}

@fputs($fsock, "USER-AGENT: " . $user_agent . "\r\n");
@fputs($fsock, "Connection: close\r\n\r\n");

while( !@feof($fsock) )
{
$filedata .= @fread($fsock, 300);
}

@fclose($fsock);

return $filedata;
}

// Add a website to fetch HTML from ...
$url = 'http://www.phpbb.com/index.php';

//Add user details if needs ...
$username = '';
$password = '';

// Add your own user-agent if you want ...
$user_agent = '';

$content = getContent( $url, $username, $password, $user_agent );

echo "<p>HTML-content from <i>" . $url . "</i>.<BR /><BR />" . $content . "</p>";

?>


Best regards,
mbrodin

Twey
05-25-2007, 06:02 PM
I think the OP was only talking about pages on the same server. The second method I posted above does this, but more efficiently and with less effort.
1. ... add username and password if it requires, such for memberpages and forums, etc.Irrelevant, if my assumption is true.
2. ... get content from secured line, e.g. "https".No it can't.
if( substr($url, 0, 7) != "http://" )
{
$url = "http://" . $url;
} If you pass it an URL starting with https://, the script will mangle it into http://https:// and then fail to parse it.
4. ... use it to fetch images and other files then for just HTML-documents.As can either method above.
if( $user_agent == '' )
{
$user_agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)';
} You do understand the purpose of a default value for optional arguments, don't you?
$query = str_replace(array("&"), array("&amp;"), $query);
$url = $url . '?' . $query; Ampersands in HTML should be escaped as &amp;, ampersands in HTTP queries shouldn't. If they're meant for their intended special purpose, as they will be here, they should be left well alone; otherwise, they should be escaped as &#37;26 (for ASCII-compatible character sets).
if ( !($fsock = @fsockopen($host, $port, $errno, $errstr, $fsocket_timeout)) )
{
die("Can't open <i>" . $url . "</i> with fsockopen!");
}

@fputs($fsock, "GET " . $path . " HTTP/1.1\r\n");
@fputs($fsock, "HOST: " . $host . "\r\n");

if( $username != '' )
{
@fputs($fsock, "Authorization: Basic " . base64_encode( $username . ":" . $password ) . "\r\n");
}

@fputs($fsock, "USER-AGENT: " . $user_agent . "\r\n");
@fputs($fsock, "Connection: close\r\n\r\n"); Such frequent use of @ will make debugging a nightmare. If there's a warning, one should usually fix it. If one does have a genuine reason to suppress output (such as if a function isn't vital to the working of the script, and one doesn't want to display errors should it go wrong), one should use error_reporting() to disable then reenable error reporting.

CBTSlMob1us
05-25-2007, 10:00 PM
Why would you want to use fopen/fread to read querystring data? I wonder.
I said the PRODUCT of the query string (the HTML output, for example). I'm not looking for something to parse the query string.