Results 1 to 6 of 6

Thread: fread()/fopen()

  1. #1
    Join Date
    May 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default fread()/fopen()

    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 ?

  2. #2
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Why would you want to use fopen/fread to read querystring data? I wonder.

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    It is possible, but you need to access it via HTTP (requires url_fopen):
    Code:
    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:
    Code:
    $_GET['step'] = 3;
    ob_start();
    include 'index.php';
    $page = ob_get_contents();
    ob_end_clean();
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    May 2007
    Location
    Sweden
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post

    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 Code:
    <?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($url07) != "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($fsock300);
                        }

                        @
    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
    Last edited by mbrodin; 05-25-2007 at 05:24 PM.

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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.
    Code:
            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.
    Code:
        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?
    Code:
                $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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    May 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by codeexploiter View Post
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •