I am passing a GUID as a header and a photo as a body to a php file. The GUID is used to authenticate. If it's not valid, I want to end the call and I do this using
die("GUID was expired");
This works fine, but my issue is that the whole photo is uploading before that gets called. This is bad for the user. I want to read the header and have the upload wait until I do the validity check. Is this possible in php? So rather than uploading the photo, then getting a failed response, just upload the headers from objective c, if the Guid is valid, then upload the photo.
Thanks!
I don't believe that's possible in PHP; the browser will POST the image content body before PHP ever gets a say in the matter.
One (slightly complicated) solution to this problem would be to do a HEAD request with an XMLHttpRequest just before the form is submitted using JavaScript. If the request failed, you could display an error message to the user or redirect to a login page. Otherwise, let the form submit as usual. Of course, the GUID could expire between the HEAD request and the subsequent POST, so it's not completely fool-proof.
Related
I am trying to serve up a CSV file as the response to a get request.
If I go onto Dev tools -> Network I can see the CSV values in the response preview, and if I double click that it will download the file. However I can't seem to get it to download automatically.
I have messed around with every type of header I can think of.
Any ideas?
Yes, It seems you can't get file downloads from ajax requests (Thanks to Quentin for the linked answer in the comments), at least not easily anyway.
This is because there is a subtle separation between the user and the browser. Ajax is triggered by the browser so the file is returned to the browser not the user.
A form submit on the other hand is triggered by the user and so the file is returned to the user (automatically downloading).
So in order to fix this I changed to a form with a post method with hidden inputs and it now works a treat.
Thanks all.
I have a php file which generate a file to be downloaded using POST parameters. I already have everything working to resume file downloading (using Range header and everything related).
However, if I pause the download and then try to resume it, the browser does not send POST data in the request. This works fine using GET data instead but I'd like to keep using POST. How could I make this work ?
Notes:
The file is generated on the fly and sent to the browser (simply print'ed by php) using the right headers.
I cannot save the file somewhere and serve it. It has to be served on the fly.
*sorry for my bad english.
well, lets say that you have database to save the POST params.
then you need to create unique url for the download based on the params.
lets say that the download link is dowload.php <- you send the POST here.
now you need to save the params there, and lets says that you get the unique_id.
after that you redirect the page to the new page(with the unique_id param) that process the download, for example resume_download.php
example download url will be resume_download.php?req=[your_unique_id]
or you can use .htaccess to made the url more friendly
this is not guarantee the download will continue, but at least user don't need to re-enter the form.
PDF Generation Dependent on $_POST
I generate a PDF in my browser, using a PHP script, where the PDF-generating script depends on variables sent to it through POST. When POST is empty, PDF cannot be generated and it issues a warning saying "PDF Must be Regenerated".
Certain situations triggered POST array being empty, namely, if I hit browser's Refresh button while being on the page with PDF, for whatever reason, POST data does not get resent and there is no prompt asking me to resend it, it just becomes empty and the script detects it and the warning is issued. The warning is PDF Must be Regenerated, which is what I put in when POST array is empty in my code.
PDF Generation Workaround Dependent on $_SESSION
So a work-around I did is to save POST data to SESSION and then use SESSION when POST is empty. Like below:
if (empty($_POST))
{
// try restore from session first
$_POST['product_count'] = $_SESSION['product_count'];
if (empty($_POST['product_count'])) // if we failed.. session is empty
{
print 'PDF Must be Regenerated';
exit();
}
}
else
{
//save POST into SESSION
$_SESSION['product_count'] = $_POST['product_count'];
}
Browser's Save feature tries to generate PDF after SESSION expires
And all was well and fine until users started noticing a certain behavior:
They generate PDF using script
Leave the browser idle for a while doing other business, until SESSION expires behind the scenes
Then they use browser's Save-As feature to save the PDF.
The browser, instead of saving PDF from memory (PDF is still visible inside the browser), browser our company uses (Mozilla), makes a request to the server, asking for a fresh new copy of the PDF. But ... while doing so, POST is empty because POST is not being resent, and SESSION is empty because SESSION has expired during the idling stage.
This results in a corrupted PDF, where PDF is actually an ASCII text file containing words PDF Must be Regenerated. Users don't notice it until it is too late.
But, Mozilla apparently makes a call to the server, PHP scripts pass the user login credentials test, but all the while having empty POST and SESSION, resulting in corrupt PDF.
Login credentials check does not depend on SESSION and to be honest I can't yet tell what it depends on because I can't easily reproduce the issue so I can't readily test.
The work-around is obviously to save PDF soon after it has been generated, before SESSION expires, but user is "always right" and I want to let the user to be able to save the PDF even when SESSION expired. My question is "how".
How? What can I do?
The pdf thing seems surely a bug on the browser. but regardless, you want to use GET.
post is to execute an action and the browser correctly do not re-issue that request. From your description it does not seem viewing the pdf is executing an action.
if you use GET, it means get me this data with those parameters. and the browser will handle it accordingly.
if it is the result of an action, then break the action and the report into two requests. for example, make viewing the resulting PDF report a link in the response for the action. you can see this pattern when you buy someone online for example. the payment POST request gives you a link via GET to the invoice.
I'm a beginner in HTML and PHP. I made an HTML file include a form that sends variables with POST format to a PHP file for process and validation. In addition this form sends a picture file.
But really I confused that this PHP file how send back acknowledge for show to user.
For more description I want design an HTML form that send values to PHP and finally show user "thank you message" if the image size and format is valid or show "unable to upload your image" in first html form. Is it possible??
Regards...
HTML is static stuff. It sends nor validates anything. The HTML describes what a page should look like and if there are special elements, like your form. It is the blueprint that a browser uses to render a page.
The browser can communicate with a server. The browser can send a request to the server, and the server sends a response in return.
The request consists of a URL and optionally extra data, like POSTed form data.
The server normally always sends a reponse, unless something goes wrong (in terms of connection loss or a time-out). Even in case of a server error, you will often still get a response).
So, since you got a php script that processes the form data, it is its reponsibility to send a reponse that tells you, the user of the browser, whether posting has failed or succeeded.
The response is loaded and displayed by the browser as a new page, so after sending the form, the browser will render the response it got, which might contain an error message or a thank you message, and if you like, a new form to upload another file.
After that, there's also the possibility to post forms using AJAX. Then, you can get the response in the background (asynchonously) and add it to the existing page. But that's a whole new chapter, and I think you should try the normal way first.
After process your Post data use
use this code on that page :-
header('Location: http://www.example.com?msg="success"');
and on that side use that code
if(isset($_GET['msg'])){echo $_GET['msg']//or show what ever you want like you message in echo ;})
I've a need that I didn't think to be so weird, but seeking for answer on web I realized it's a bit complicated.
I have a page with a list of documents to download, let's call it index.html. Once a user click on a "download button", a form appears and the user's gonna be requested to provide its email. I have a proxy server file, let's name it proxy.php, which receive an email and a document id, does some things (store the email, read document in a buffer and then provide a http-response with it) and then response with a server status and, eventually, a content.
If I use a html-form to do this, it works perfectly when nothing is wrong, but I can't intercept any exception in case of error
Instead, if I use an ajax request, I can manage status codes, but I find my self with my document stored in a javascript variable and I don't know how to use it.
Thanks
You can try looking at this thread here: POST to server, receive PDF, deliver to user w/ jQuery
Assuming your document is pdf-based (though other types should be fine), you can try what the accepted answer has provided:
On the server side, add the HTTP header:
Content-Disposition: attachment; filename="whatever.pdf"