Jump to content

Logging in when directly accessing page


Guest Ned Wilsher

Recommended Posts

Guest Ned Wilsher

I have a problem with sending multiple headers (I think). I am trying to send headers to download a file and, at the same time. open a new php file (e.g. index.php). Here is the scenario.At the beginning of each page in my site I test for user status. If the user is not logged in (i.e. no session) they are redirected to the login page using <?php header("location: $url"); ?>. I often parse the target page in the $url (e.g. $url='login.php?page=securepage.php'). Hence if someone tries to open 'securepage.php' (say, from favourites) then they are prompted to log in. Upon succesfully logging in the user is relocated by using the following sticky form script in 'login.php'. $url = (isset($_GET['page']) ? $_GET['page'] : 'index.php'); echo"<form action='login.php?page=" . $url . "' method='post'>.... etcIf the username and password are validated, the login form sets up a session and redirects to the target page (e.g. 'securepage.php'), or if no page was parsed in the url, the user is taken to the index page.That works fine, except when a logged out user tries to directly open my 'download_file.php' which I use to download files (Word; Excel; PDF etc). This file contains mainly script (html output is only used to report errors). I use the following code:if (file_exists ($the_file)) { // Send the file. ob_end_clean(); // Delete the buffer. header ("Content-Type: $ft\n"); header ("Content-disposition: attachment; filename=\"$fn\"\n"); header ("Content-Length: $fs\n"); readfile ($the_file); exit(); } else{<-- Display error message -->}If in this way the user is taken to the download_file.php via the login page, the Word etc. file succesfully downloads. However, the login page remains active such that when I close the downloaded file I am still at the login prompt. Clicking login (again) simply reopens the downloaded file (a sticky problem maybe). What is the best solution? How, for example, could I redirect the user to, say, 'index.php' after succesfully downloading the file?I have tried passing something back to the download page (e.g. download_file.php?page=index.php) and added another header immediately after the readfile function, viz code as above ....readfile ($the_file); header ("Location: $url"); //where $url is derived from $_GET['page']exit(); but the redirection does not execute, presumably because the script stops after the readfile function. I tried placing the new line of code after deleting the buffer: the redirection worked but the Word file did not open, I presume for very much the same reason. Any thoughts, anyone. Ned Wilsher (beginner)

Link to comment
Share on other sites

What is the best solution? How, for example, could I redirect the user to, say, 'index.php' after succesfully downloading the file?
You've got it backwards. The download page will be a special case for the login page. If they log in and are to be redirected to the download page, instead redirect them to index and put something on the querystring that will let index know that they want to download the file. The index page can then redirect them to the download page, and when the download is done they will still be on the index.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...