I have a form page where the users must put a code that I send them by email, this form has as action file redirect.php, which contain this code:

```
<?php
header('Location: page.php?code='.$_POST['code']);
?>

```

now what I want to do with my form page is to show a error or to redirect to a page if the code they input on my form doesn's exist in code.txt file.

Is it possible that? And if is it how to do it?

**EDIT:** 

I think that must be easier: I want that this form:

```
<form action="page.php" method="post">
<input id="code" name="code" type="text" />
<input type="submit" value="Submit"/>
</form>

```

I want this form to pass to the page.php only if the value of input id="code" match values from code.txt
['https://Stackoverflow.com/questions/20132066', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3016012/']
I'm reqesting a image from yahoo and it isn't using the `content-disposition` header but I am extracting the `date` and `content-type` headers to construct a filename. This seems close enough to what you're trying to do...

```
var request = require('request'),
fs = require('fs');

var url2 = 'http://l4.yimg.com/nn/fp/rsz/112113/images/smush/aaroncarter_635x250_1385060042.jpg';

var r = request(url2);

r.on('response',  function (res) {
  res.pipe(fs.createWriteStream('./' + res.headers.date + '.' + res.headers['content-type'].split('/')[1]));

});

```

Ignore my image choice please :)
Here's my solution:

```
var fs = require('fs');
var request = require('request');
var through2 = require('through2');

var req = request(url);
req.on('error', function (e) {
    // Handle connection errors
    console.log(e);
});
var bufferedResponse = req.pipe(through2(function (chunk, enc, callback) {
    this.push(chunk);
    callback()
}));
req.on('response', function (res) {
    if (res.statusCode === 200) {
        try {
            var contentDisposition = res.headers['content-disposition'];
            var match = contentDisposition && contentDisposition.match(/(filename=|filename\*='')(.*)$/);
            var filename = match && match[2] || 'default-filename.out';
            var dest = fs.createWriteStream(filename);
            dest.on('error', function (e) {
                // Handle write errors
                console.log(e);
            });
            dest.on('finish', function () {
                // The file has been downloaded
                console.log('Downloaded ' + filename);
            });
            bufferedResponse.pipe(dest);
        } catch (e) {
            // Handle request errors
            console.log(e);
        }
    }
    else {
        // Handle HTTP server errors
        console.log(res.statusCode);
    }
});

```

The other solutions posted here use `res.pipe`, which can fail if the content is transferred using `gzip` encoding, because the response stream contains the raw (compressed) HTTP data. To avoid this problem you have to use `request.pipe` instead. (See the second example at <https://github.com/request/request#examples>.)

When using `request.pipe` I was getting an error: "You cannot pipe after data has been emitted from the response.", because I was doing some async stuff before actually piping (creating a directory to hold the downloaded file). I also had some problems where the file was being written with no content, which might have been due to `request` reading the HTTP response and buffering it.

So I ended up creating an intermediate buffering stream with `through2`, so that I could pipe the request to it before the response handler fires, then later piping from the buffering stream into the file stream once the filename is known.

Finally, I'm parsing the content disposition header whether the filename is encoded in plain form or in UTF-8 form using the `filename*=''file.txt` syntax.

I hope this helps someone else who experiences the same issues that I had.