htaccess

Discussion in 'PHP' started by gigamike, May 29, 2007.

  1. #1
    hi Guys,

    i have a question, im not sure if this is possible in php. I have a site let say www.mysite.com and its htaccess password protected. i know the username and password info. Now is it possible that i can get the content of the page. i already did some file_get_contents and CURL exec to read the content of the page but no luck it only returns "Authorization Required
    This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required."

    But when i did in using this one, it works

    <cfhttp url="www.mysite.com" method="get" username="htaccess_username" password="htaccess_password">
    <cfset #site_content# = #CFhttp.Filecontent#>

    see what i mean...

    Thanks,

    Mike
     
    gigamike, May 29, 2007 IP
  2. gigamike

    gigamike Active Member

    Messages:
    165
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #2
    Guys,

    i think i got it.

    $url="http://username:password@yoursite.com";
    $handler=file_get_contents($url);

    Thanks,

    Mike


     
    gigamike, May 29, 2007 IP
  3. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #3
    It's not the best idea in the world, if you're have curl then use it, please

    
    <?php
    function auth_file_get_contents( $username, $password, $url )
    {
    	if( !( $curl = curl_init( ) ) )
    	{
    		die( 'Cannot initialize curl' );	
    	}
    	elseif( !curl_setopt( $curl, CURLOPT_URL, $url ) or
    			!curl_setopt( $curl, CURLOPT_HTTPHEADER, array( 
    				sprintf( 'Authorization: Basic %s', base64_encode( sprintf( '%s:%s', $username, $password ) ) )
    			)) or
    			!curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true ) or
    			!curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false ) or
    			!curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, false ) )
    	{
    		die( sprintf( 'Cannot setup curl %d : %s', curl_errno( $curl ), curl_error( $curl )  ) );	
    	}
    	else
    	{
    		return curl_exec( $curl );
    	}
    }
    echo auth_file_get_contents('username', 'password', 'https://hozter.info:2083/frontend/x/index.html' );
    ?>
    
    PHP:
    That will work over ssl aswell, the url there is a cpanel one just to test with, but that'll work on near on any authorization you need, some services / apis have thier own authentication type, for instance WHM uses it's own "Authorization: WHM accesshash" string to authorize users.
     
    krakjoe, May 29, 2007 IP
  4. gigamike

    gigamike Active Member

    Messages:
    165
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #4
    This is great. Thanks a lot.

    Mike

     
    gigamike, May 29, 2007 IP