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;
}
}
}
?>
