0

So I want to create a simple result page that lets users download their results using the given code.

This is the script:

<form action="" method="post" >
    <input type="text" name="logincode">
    <input type="submit" name="send">
</form>

<?php
    $name = $_POST['logincode'];
    $filename = $name.'/'.$name.'pdf';  
    header('Location: ./'$filename'');
?>

The principle is when the user writes into the input field, for example (1234) and hits enter, it should redirect him to:

./1234/1234.pdf

I don't know where the mistake is in my code.

1
  • 2
    What goes wrong? From the looks of it, you'll need to put the header() command before any output. Commented Mar 15, 2018 at 18:14

4 Answers 4

2

Few issues,

  • Your header should be before anything else as @showdev mentioned in a comment.
  • You're missing a . between filename and extension
  • You also have a syntax error in the header trailing ''
  • And you should exit redirect headers.

You should also be checking your variables as you go, plus check the file exists, so you can show errors.

<?php
// check post request
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
   $errors = [];
   // check logincode is set and a number
   if (!isset($_POST['logincode']) || !is_numeric($_POST['logincode'])) {
       $errors['logincode'] = 'Invalid login code';
   } else {
       $name = $_POST['logincode'];

       // check file is found
       if (!file_exists($name.'/'.$name.'.pdf')) {
           $errors['logincode'] = 'Your results are not ready.';
       }

       // now check for empty errors
       if (empty($errors)) {
           exit(header('Location: ./'.$name.'/'.$name.'.pdf'));
       }
   }
}
?>

<form action="" method="post">
    <?= (!empty($errors['logincode']) ? $errors['logincode'] : '') ?>
    <input type="text" name="logincode">
    <input type="submit" name="send">
</form>
Sign up to request clarification or add additional context in comments.

Comments

0

You are missing a “.” before pdf aren’t you?

And also wrong header('Location: ./'$filename'');

Try this :)

<?php
$name = $_POST['logincode'];

$filename = $name.'/'.$name.'.pdf';
header('Location: ./'.$filename);
?>

3 Comments

thank you brothers but when i tried any of those codes it not redirect to the pdf file the page refresh that's it
Could it be that you need two dots to get back a folder like “Location: ../‘.$filename); ?
no sir the folder is in the same script's folder so i sould use one dot i think the probem is on the submit button not works good
0

It's very insecure code!

Major changes below:

  • write test for user input data TODO by you
  • change order PHP block code first, form (HTML) code next in snippet
  • add test is post request_method before any $_POST['...']; in snippet
  • add .(dot) before filename extension i $filename in snippet

        <?php
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $name = $_POST['logincode'];
            
            $filename = $name.'/'.$name.'.pdf';  
            header('Location: ./'$filename'');
        }
    
        ?>
    
        <form action="" method="post" >
        <input type="text" name="logincode">
        <input type="submit" name="send">
        </form>

6 Comments

Whys it insecure?
It was about @walidz code (without any tests of user input data or http_request method) not yours. Your answer is great. We wrote answers in the same time.
thank you brothers but when i tried any of those codes it not redirect to the pdf file the page refresh that's it
It works. I'm sure. I've tested it again a moment ago Server Linux/Apache/php-7.1.13; browsers ffx / chrome / IE11 / MS Edge in all browsers works as expected, it means redirect to pdf file.
i dunnno why it not work , i'v tried on 000webhost and localhost and some other servers , always the same thing it not redirec to the pdf file it just refresh when iclick enter
|
0
<form action="" method="post" >
<input type="text" name="logincode">
<input type="submit" name="send">
</form>

<?php
if($_POST){
$name = $_POST['logincode'];


$filename = $name.'/'.$name.'.pdf';

header('Location: ./'.$filename.'');
}
?>

7 Comments

thank you brothers but when i tried any of those codes it not redirect to the pdf file the page refresh that's it
@walidz I have edited it, will work now, working on my localhost btw.
when you fill the input with xxxx and hit enter it will be redirect to xxx/xxx.pdf ? are you sur sir ? i tried on 3 web hosting but its still not work
@walidz Yes it's working. Assume that your file url is localhost/location.php, enter xxxx and submit, it will go to localhost/xxxx/xxxx.pdf . If there is no file there, it will display an error. Be sure you have a file at the redirect location.
its okey sir yes you r right the error is the file extention of the script
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.