| | |
PHP .htaccess form
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jul 2006
Posts: 6
Reputation:
Solved Threads: 0
I have a php login system that uses SQL to store users and passwords but users can still download .mp3 files or even pictures (jpeg, gif, etc). Is there anyways I can protect my web directories and files using .htaccess without that popup asking for user and password but using the php login system that I have.
•
•
•
•
Originally Posted by dcarrillo18
I have a php login system that uses SQL to store users and passwords but users can still download .mp3 files or even pictures (jpeg, gif, etc). Is there anyways I can protect my web directories and files using .htaccess without that popup asking for user and password but using the php login system that I have.
What you can do is put the files you want to protect under the web directory, so that it cannot be acccessed from the web, or if you prefer .htaccess, just limit access to that folder from the web (same thing really).
Lets say your web directory is something like say: /home/public_html/
You can put your file in /home/files/. That way those files cannot be accessed from the web (HTTP) but you can still access those files from PHP using the directory functions or stream functions.
Or if you used .htaccess to prevent users from accessing the files over the web, you can still access the files from php just the same.
What you have to do is create a php page that checks if the user is logged in. If they are then allow them to download the file they asked for by using the stream functions built into php (http://us2.php.net/manual/en/ref.stream.php) to get the file and echo it to the browser.
You will also need to send the right headers to let the browser know that this is a download, instead of a regular webpage.
Example:
[PHP]
$filename = $_GET['filename'];
$dir = '/home/files/';
if ( user_logged_in() ) {
download_file( $filename, $dir ); // send file to logged in user
} else {
header("HTTP/1.1 404 Not Found"); // dead end
}
/**
* Forces a file download by sending the Content-Type: application/force-download HTTP header to the client
* @param string filename
* @param string filebase directory including trailing slash
* @param int size stream buffer
* @auth download_file.func@fijiwebdesign.com | http://www.fijiwebdesign.com/
*/
function download_file( $fname, $path, $buffer = 2000 ) {
$fpath = $path.$fname; // absolute path to file
$fsize = filesize( $fpath ); // size of file
// send the headers forcing the file download
header("HTTP/1.1 200 OK");
header("Content-Length: $fsize");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=$fname");
header("Content-Transfer-Encoding: binary");
// open a file stream and read file contents in binary mode (r = read, b = binary)
if($fh = @fopen($fpath, "rb")){
while( ($buf = fread($fh, $buffer)) !== false) {
echo $buf; // echo the file contents to the browser in chunks
}
fclose($fh);
} else {
header("HTTP/1.1 404 Not Found");
}
}
[/PHP]
Note: user_logged_in() should check if the user is logged in. It isnt a real function.
Hope that helps..
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
![]() |
Similar Threads
- html/php form for .htaccess validation (PHP)
- PHP Quote form (PHP)
- PHP .htaccess form (PHP)
- php login form (PHP)
- php mail form - need to redirect to new page (PHP)
Other Threads in the PHP Forum
- Previous Thread: Query optimization
- Next Thread: dynamic combo load problems
| Thread Tools | Search this Thread |
.htaccess ajax apache api array beginner binary broken buttons cakephp checkbox class cms code cron curl database date directory display download dynamic ebooks echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla limit link login loop mail mediawiki menu mlm mod_rewrite multiple mysql number oop paypal pdf php phpincludeissue phpmyadmin problem query radio random recursion regex remote script search server sessions sms soap source sp space speed sql subdomain syntax system table tag tutorial update upload url validation validator variable vbulletin video web websphere white xml youtube






