johnsmith153 Posted December 2, 2008 Share Posted December 2, 2008 Does anyone have a script that allows a user to download a file but have some sort of session control? Ie if($_SESSION['a']==1) { //download } else { //do not download } etc. must work with sessions - which seems to be the problem. Quote Link to comment Share on other sites More sharing options...
jjacquay712 Posted December 2, 2008 Share Posted December 2, 2008 have a script set a session, then do this <?php session_start(); if ($_SESSION['download']) { echo '<script type="text/javascript">document.location="download link.exe"</script>'; } ?> Quote Link to comment Share on other sites More sharing options...
johnsmith153 Posted December 2, 2008 Author Share Posted December 2, 2008 I need it without javascript. Is that possible? Quote Link to comment Share on other sites More sharing options...
waynew Posted December 2, 2008 Share Posted December 2, 2008 <?php session_start(); if ($_SESSION['download']) { header('Location: filename.exe'); } ?> Quote Link to comment Share on other sites More sharing options...
jjacquay712 Posted December 2, 2008 Share Posted December 2, 2008 sure, you can use this: <?php session_start(); if ($_SESSION['download']) { header("location: download_link.exe"); } ?> Quote Link to comment Share on other sites More sharing options...
jjacquay712 Posted December 2, 2008 Share Posted December 2, 2008 beat me to it Quote Link to comment Share on other sites More sharing options...
johnsmith153 Posted December 2, 2008 Author Share Posted December 2, 2008 <?php session_start(); if ($_SESSION['download']) { header("location: jpeg-image.jpg"); } ?> jpeg-image.jpg Hardly going to work though, is it? Quote Link to comment Share on other sites More sharing options...
johnsmith153 Posted December 2, 2008 Author Share Posted December 2, 2008 Any ideas anyone? Need to download a file, NOT just link to it. But must be able to use $_SESSION on the page - it seems to mess it up if you do. Quote Link to comment Share on other sites More sharing options...
revraz Posted December 2, 2008 Share Posted December 2, 2008 Sessions have nothing to do with opening files, so not sure what you mean by "mess up". Quote Link to comment Share on other sites More sharing options...
unkwntech Posted December 2, 2008 Share Posted December 2, 2008 <?php session_start(); if ($_SESSION['download']) { header('Content-Disposition: attachment; filename="file.ext"'); readfile('path/to/file.ext'); } ?> Quote Link to comment Share on other sites More sharing options...
johnsmith153 Posted December 2, 2008 Author Share Posted December 2, 2008 I agree there should not be any relation between file download and $_SESSION, but: The below scripts are the full code that is on the page. This WORKS great: //session_start(); $fileurl = "/home/account/folder1/folder2/file.xlsx"; $file= "file.xlsx"; header("Content-Disposition: attachment; filename=$file"); readfile($fileurl); This DOES NOT: session_start(); $fileurl = "/home/account/folder1/folder2/file.xlsx"; $file= "file.xlsx"; header("Content-Disposition: attachment; filename=$file"); readfile($fileurl); Quote Link to comment Share on other sites More sharing options...
revraz Posted December 2, 2008 Share Posted December 2, 2008 Define DOES NOT. What does or does it not do? Quote Link to comment Share on other sites More sharing options...
johnsmith153 Posted December 2, 2008 Author Share Posted December 2, 2008 It does not work. It does not do the file download. Quote Link to comment Share on other sites More sharing options...
johnsmith153 Posted December 2, 2008 Author Share Posted December 2, 2008 Surely either I'm the idiot, or PHP is. Which one? I would appreciate if someone can try this, I just can not understand why the below script will not download the file just because I set session_start() Works fine, unless you set session_start() //session_start(); $fileurl = "/home/account/folder1/folder2/file.xlsx"; $file= "file.xlsx"; header("Content-Disposition: attachment; filename=$file"); readfile($fileurl); Quote Link to comment Share on other sites More sharing options...
johnsmith153 Posted December 2, 2008 Author Share Posted December 2, 2008 Thanks to www.sitepoint.com, the answer is: Place: session_cache_limiter('public'); before the session_start() call Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.