0

I am new to php programming. I need to create a webpage which get user name and password from user and after verifying it gives access to a particular text file. I have seen the basic on the documentation page and after running it works good for me (however I need to define user name and password):

<?php
if (!isset($_SERVER['PHP_AUTH_USER'])) {
    header('WWW-Authenticate: Basic realm="My Realm"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'Text to send if user hits Cancel button';
    exit;
} else {
    echo "<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>";
    echo "<p>You entered {$_SERVER['PHP_AUTH_PW']} as your password.</p>";
}
?>

My question here is, I need a bash/shell script which is sending username and password information to this page and then it can download a texfile. The text file is in htdocs directory and called "welcome.txt". The bash script command to download the file can be anything like this:

#!/bin/bash
clear
wget --user=admin --password=admin http://behzadgarekani.net16.net/connect.php -O -q my-old-bookmarks.txt

OR

using curl

I'd appreciate if you could help me. There is no force that I must do it in this way. If you have any other suggestion to share this text file will be appreciated.

Edit:

<?php

    destroy_foo();
    function destroy_foo()
    {
        unset($GLOBALS[_SERVER]);

        if (isset($_SERVER['PHP_AUTH_USER'])) {

            unset($_SERVER['PHP_AUTH_USER']);

        }
        if (isset($_SERVER['PHP_AUTH_PW'])) {

            unset($_SERVER['PHP_AUTH_PW']);

        }
    }

    //unset ($_SERVER['PHP_AUTH_USER'] );
    if (!isset($_SERVER['PHP_AUTH_USER'])) {
        header('WWW-Authenticate: Basic realm="My Realm"');
        header('HTTP/1.0 401 Unauthorized');
        echo 'Text to send if user hits Cancel button';
        exit;
    } else {
        echo "<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>";
        echo "<p>You entered {$_SERVER['PHP_AUTH_PW']} as your password.</p>";
        //header('Location: welcome.txt');

        $file = 'welcome.txt';

        if($_SERVER['PHP_AUTH_PW'] == "admin" && $_SERVER['PHP_AUTH_USER'] =="admin"){

            if (file_exists($file)) {
                header('Content-Description: File Transfer');
                header('Content-Type: application/octet-stream');
                header('Content-Disposition: attachment; filename='.basename($file));
                header('Expires: 0');
                header('Cache-Control: must-revalidate');
                header('Pragma: public');
                header('Content-Length: ' . filesize($file));
                readfile($file);
                destroy_foo();

                exit;
            }
        }
    }
    ?>

enter image description here

3
  • did you see the example with username and password php.net/manual/en/features.http-auth.php Commented Sep 12, 2014 at 18:56
  • @zod yes. My problem is the text file. How can I redirect it to text file if the username and password is correct? Commented Sep 12, 2014 at 19:01
  • php.net/manual/en/function.readfile.php Commented Sep 12, 2014 at 19:06

1 Answer 1

2

As per your comments , i think after login you want to force download a file

<?php
$file = 'monkey.gif';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    readfile($file);
    exit;
}
?>

for text file

header('Content-type: text/plain');

http://php.net/manual/en/function.readfile.php

http://webdesign.about.com/od/php/ht/force_download.htm

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for you reply. There is a problem with downloading. my bash script is downloading text file in format of html file.
Do I need to delete previous headers because it did not work with that even. The file is html format
Please have a look at image after download

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.