How to fill user/pass field of a prompted form with php?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Zarich
    New Member
    • May 2010
    • 5

    How to fill user/pass field of a prompted form with php?

    Hello, i've been spending some time trying to figure out a solution for the hotlinking of the videos hosted at my site, which is imposible to do trough htaccess since firefox has a bug that can't read the referrals in this particular case like IE or chrome would. Well i figured out something, in my cpanel i have the option to protect folders with an username and password, and for what i have tested so far, it works to avoid the hotlinking. So here is my idea, since with that activated the browser will ask for an username and password, is there a way to put a script that fill this form with the correct password & user stored in variables in the php with out the need of the human hand to do it, so i could put this code in every web at my site so they can access the folders (people shouldn't be prompt it with the log in screen since the php should fill it underneath) but since the other webs don't have the password and the php script they are screwed. I hope i made myself clear, and please help me out with this, i just know enough php to edit it but not to create it yet and I'm not sure if this is possible though, if you help me out to create this code I'm sure a lot of people will be grateful since most of video streaming have this issue cuz of the firefox bug.

    Well thanks in advance to whoever gives me an idea of how to do this.
    And please don't say "use htaccess" just help me with a code that could fill the username and password when prompted by my own website.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    I don't think this is possible, no. The password protection is most likely at the HTTP server level, which happens before any PHP code is executed. The request needs to include the authentication info, which is not something PHP can circumvent.

    There is one idea I can offer you; something that might at least prevent people simply linking to things like images or videos on your server. This is by no means bulletproof, though.

    You could use a server-side session to keep track of whether the requester has actually accessed a page on your website before sending the request for the resource, and if he has not you can deny access. To do that, you would simply add two lines to the top of your scripts that would register a session variable.
    For example:
    [code=php]<?php
    session_start() ;
    $_SESSION['isEnabled'] = true;
    ?>
    <html>
    <head><title>Ho tlinking test</title></head>
    <body>
    <h1>Image!</h1>
    <img src="logo.png" alt="Logo!" title="A logo">
    </body>
    </html>[/code]

    Then you would pass all requested images through a .htaccess filter that would, rather than serve the requested image, redirect it to a PHP script:
    [code=htaccess]RewriteEngine On
    RewriteRule ^(.*\.(jpe?g|gi f|png))$ getContent.php? file=$1&nothing[/code]
    (Note, the "&nothing" is there just to prevent a loop, as the rule would also match the redirected URL without it.)

    The PHP script, "getContent.php ", would then verify that the session element has been register before serving the image. If it has not, it either just gives a 404 error or some other non-content.
    For example:
    [code=php]<?php
    session_start() ;
    define('CONTENT _ROOT', 'files/');

    if(isset($_SESS ION['isEnabled']) && isset($_GET['file']))
    {
    $filename = $_GET['file'];
    $filePath = CONTENT_ROOT . $filename;
    if(!file_exists ($filePath) || !is_file($fileP ath))
    {
    send_image(CONT ENT_ROOT . '404.png');
    }
    else
    {
    if(!send_image( $filePath))
    {
    send_image(CONT ENT_ROOT . '404.png');
    }
    }
    }
    else
    {
    send_image(CONT ENT_ROOT . 'denied.png');
    }

    /**
    * Sends an image to the output buffer.
    * Note, this can not be called more than once,
    * and can not be called after ANY other output.
    * @param $path The location of the image to send.
    */
    function send_image($pat h)
    {
    $data = getimagesize($p ath);
    if(!$data) {
    return false;
    }
    header('Content-type: '. $data['mime']);
    header('Content-length: ' . filesize($path) );
    readfile($path) ;

    return true;
    }
    ?>[/code]

    Comment

    • Zarich
      New Member
      • May 2010
      • 5

      #3
      Oh, i get what u mean, since its a server function it should be executed before any php could be loaded. Now your idea seems interesting, correct me if im wrong on this; ill add the first piece of code to all my php files so they all have active sessions, then the htaccces, then the last part is a script that would register the image based on the getimagesize parameter. Well now how would i be able to do that with mp4 videos since the getimagesize won't work there (at least i think so), After all im just a Newbie at php and thats why i ask for help. Btw since we will be using htaccess here im not sure if it will work, because the thing is firefox completly bypass the htaccess protecion when hotlinking a video on a embedded .swf.
      More info about this issue at http://www.longtailvideo.com/support...m-with-firefox

      Anyways at least i can try with the code you gave me, but help me out to modify it to work with .mp4 videos if it is capable to.

      Another question in case this won`t work, is it possible to protect folders with a password in php?, i have seen i can protect webpages but not the folders, but if is possible it would do the thing.

      Well i really appreciate your help and spending some time in your answer, you are the man =P.

      Cya

      Comment

      • Zarich
        New Member
        • May 2010
        • 5

        #4
        I was Able to do it with httaccess cookies =D, so if u have problemes with hotlinking and videos you gotta do that.

        Comment

        Working...