php - Offering file download within a session -


i trying make file download dependent inside session. here code:

<?php>     session_name("my-download");      session_start();      $_session['download-authorized'] = 1;      echo "<a class='invlink' rel='nofollow' download target='_blank' href='download.php?download_file=file.to.download.pdf'>name of file</a><br /><br />";  ?> 

the download script ('download.php') comes next:

<?php     session_start();     if(!isset($_session['download-authorized'])) {         exit;     }     $path = $_server['document_root']."/downdir/";      $fullpath = $path.$_get['download_file'];      if ($fd = fopen ($fullpath, "r")) {         $fsize = filesize($fullpath);         $path_parts = pathinfo($fullpath);          header("pragma: public");         header("expires: 0");         header("cache-control: must-revalidate, post-check=0, pre-check=0");         header("cache-control: public");         header("content-description: file transfer");         header("content-type: application/pdf");         header("content-disposition: attachment; filename=\"".$path_parts["basename"]."\"");         header("content-transfer-encoding: binary");         header("content-length: " . $fsize);          while(!feof($fd)) {             $buffer = fread($fd, 2048);             print($buffer);             flush();         }         fclose ($fd);     } else {         die("file not exist. make sure specified correct file name.");     }     exit; ?> 

all works fine long verification of '$_session['download-authorized'] ist commented out. when check session-variable $_session['download-authorized'] set download fail.

what's wrong code?

any appreciated.

after adding session_start() beginning of download.php script still not work. appears case session-id session-name changes when "download.php" called. additionally $_session['downlad-autorized'] reset.

your initial script stores flag in session explicitely renamed (session_name("my-download");), download script uses default session name (no session_name()).

therefore download script starts (possibly empty) session.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -