User downloadable links in php

Hi all,

Am pretty much new to php. I hve a php site with user login, i hve maintained session. I hve pdf download in blog and articles page.

I hve to show the pdf for all, but it should be downloadable only for registered users, for others when they click it i want to show a alert “Please sign in to Download”.

Please help me out in this issue.

You can see the files here:
http://212.227.137.184/tim_04-02-10/index.php

user id: dhilipr

pass: dhilipr

i hve placed a downloadable pdf in Blog and Articles page. Now only the register user can see the pdf files. Bcoz i hve hide it initially and after logging in i hve revealed it.

What i want is, i hve to show the pdf for all users, but only registered users can able to download it. for others it should show a alert like “sign in to download”

Thanks & Regards,
Dhilip. R

Hi all,

Am pretty much new to php. I hve a php site with user login, i hve maintained session. I hve pdf download in blog and articles page.

I hve to show the pdf for all, but it should be downloadable only for registered users, for others when they click it i want to show a alert “Please sign in to Download”.

Please help me out in this issue.

You can see the files here:
http://212.227.137.184/tim_04-02-10/index.php

user id: dhilipr

pass: dhilipr

i hve placed a downloadable pdf in Blog and Articles page. Now only the register user can see the pdf files. Bcoz i hve hide it initially and after logging in i hve revealed it.

What i want is, i hve to show the pdf for all users, but only registered users can able to download it. for others it should show a alert like “sign in to download”

Thanks & Regards,
Dhilip. R

You should have a php page which downloads the file and pass the downloading file to that file in query string. For example save the following script in download.php file. (I assume you save download.php file in the root).


if($user_is_logged_in){
    $filetodownload = addslashes($_GET['filename']);
    if(file_exists($filetodownload)){
        header('Content-type: application/pdf');
        header('Content-Disposition: attachment; filename="' . $filetodownload . '"');
        readfile($filetodownload);
        die();
    }
}
else{
    echo 'You must login to download the file.';
}

And in your article or blog content add something below link
http://www.yoursite.com/download.php?filename=myfile.pdf instead of direct file url/path.

Use mod_rewrite to redirect all requests for .pdf files to a PHP script. That script looks at the session to determine whether to show the “sign in to download” message, or send the appropriate headers and pass the PDF data through.

Off Topic:

It seems there are two threads for the same problem posted by same user.
http://www.sitepoint.com/forums/showthread.php?t=659863

Threads merged

Hi Thanks for all your replies,

I hve done the same with the help of jquery.

here is my code for your view.

<?php
if(isset($_SESSION[‘id’]) != true ){
print “<script>”;
print “$(document).ready(function(){”;
print “$(‘#pdfdownloads a’).click(function(event) {”;
print “event.preventDefault();”;
print “alert(‘Sign in to download’);”;
print “});”;
print “});”;
print “</script>”;
}
?>

Thank you Very much for all of your efforts.

I’ve done a bunch of stuff like this, and I suggest using a slightly different approach that simplifies the process:
You can circumvent the need to create user registration by simply using a form where users enter a form and check off the documents they want to download. Then use PHPMailer to send the documents. You can log the users email or send it to yourself at the same time.

Of course, the documents have to be small enough.