PHP密码保护表单帮助.1输出工作,倍数不


PHP password protected form help. 1 output works, multiples do not

我想弄清楚为什么一种形式的工作,但我的另一个没有。我试图创建一个表单,让用户选择各种包,输入密码,然后有能力下载说的文件,如果它是正确的。我可以在一个文件中这样做

<?php
 // Get the password
      $pw = md5($_POST['password']);
 // Compare against the stored password
      $valid_pw = md5("example");
      if($pw != $valid_pw){
           echo "Error! You do not have access to this file";
      }else{
           header("Location:files'bundle1.pdf");
      }
?>

然而,我不能让这个工作。当我做出选择,输入密码并点击提交时,我看到一个空白屏幕,URL是PHP文件。下面是代码:

<?php
 // Get the password
        $pw = md5($_POST['password']);                    
        $bundle1 = ($_POST['1']);
        $bundle2 = ($_POST['2']);
        $bundle3 = ($_POST['3']);
        $bundle4 = ($_POST['4']);
 // Compare against the stored password
      $valid_pw = md5("example");
      if($pw != $valid_pw){
           echo "Error! You do not have access to this file";
      }else{
$bundle1 = 'unchecked';
$bundle2 = 'unchecked';
$bundle3 = 'unchecked';
$bundle4 = 'unchecked';
if (isset($_POST['download'])) {
     $selected_radio = $_POST['bundle'];
     if ($selected_radio == '1') {
            $bundle1 = 'checked';
            header("Location:files'bundle1.zip");
            exit();
      }else if ($selected_radio == '2') {
            $bundle2 = 'checked';
            header("Location:files'bundle2.zip");
            exit(); //added exits so program wouldn't continue to run after selection -k
      }
      else if ($selected_radio == '3') {
            $bundle3 = 'checked';
            header("Location:files'bundle3.zip");
            exit();
      }
      else if ($selected_radio == '4') {
            $bundle4 = 'checked';
            header("Location:files'bundle4.zip");
            exit();
      }
    }
 }
?>

主要区别(除了else if语句)是工作文件是。pdf,而非工作文件是。zip。这有什么区别吗?

在双引号中php使用'作为转义序列,因此使用'或/代替使用header("Location:files''bundle1.zip");header("Location:files/bundle1.zip");

使用正斜杠或双反斜杠进行转义和实际斜杠

你的路径错了:

        header("Location:files'bundle1.zip");
                              ^---

HTTP使用/作为url中的路径分隔符,不使用反斜杠。由于'b在PHP "引号字符串中没有特殊意义,不像'n, 'r等…它会被忽略,你发送的是

Location:filesbundle1.zip