代码之家  ›  专栏  ›  技术社区  ›  Léo Léopold Hertz 준영

在PHP中查找缺少get变量的原因

  •  0
  • Léo Léopold Hertz 준영  · 技术社区  · 15 年前

    如何在PHP下一页的URL中获取变量?

    用户位于URL

     http://localhost/codes/index.php?ask_question&email=masi@gmail.com&passhash_md5=202cb962ac59075b964b07152d234b70
    

    他发送了一个问题,然后转到下面的URL,在那里变量由于一些未知的原因丢失了它们的值。

    http://localhost/codes/index.php?question_sent&email=&passhash_md5=
    

    以下代码是代码 处理\u a \u new \u question.php .

     $result = pg_prepare($dbconn, "query77", "INSERT INTO questions
         (body, title, users_user_id)
         VALUES ($1, $2, $3);");
     $result = pg_execute($dbconn, "query77", array($body, $title, $user_id));
    
     if(isset($result)) {
         $header = ("Location: /codes/index.php?"                                                                                                                                                           
             . "question_sent"
             . "&"
             . "email="
             . $_GET['email']             // this seems to be correct to me
             . "&"
             . "passhash_md5="
             . $_GET['passhash_md5']      // this seems to be correct to me too
             );
         header($header);
      }
    

    你看到密码有错误吗?

    4 回复  |  直到 15 年前
        1
  •  1
  •   Paul    15 年前

    尝试使用会话处理程序代替get变量。它确实为代码添加了一些按键,但在我看来,它为您节省了大量的资源。

        2
  •  3
  •   Léo Léopold Hertz 준영    15 年前

    表单代码是什么样子的?问题出现在引导您进入问题发送页面的页面上,而不是问题发送页面本身。

    您是在隐藏字段中传递这些变量,还是在窗体的操作中传递这些变量,例如

    <form method="post" action="index.php?question_sent&email=<?php echo $_GET['email']?>&passhash_md5=<?php echo $_GET['passhash_md5']?>">
    

    还是使用隐藏变量

    <form method="post" action="index.php?question_sent">
       <input type="hidden" name="email" value="<?php echo $_GET['email']?>" />
       <input type="hidden" name="passhash_md5" value="<?php echo $_GET['passhashmd5']?>" />
       ...
    </form>
    

    因为这两种方法中的任何一种都应该有效,首先你可以从$\u中检索你的变量,其次你可以从$\u post中检索它们。

    同样,对于用户会话,您可能应该将他们的登录信息存储在cookie中,当您有机会时,请看一下这个。

    http://www.tizag.com/phpT/phpcookies.php

        3
  •  1
  •   Toto    15 年前

    确保数据是通过邮局发送的。所以可以这样检索:

    $_POST['email']

    顺便说一句:

    1. 在执行重定向之前,不要忘记清理数据(在您的示例中没有这样做)。
    2. 别忘了“加盐”你的土豆泥,否则它可能不安全。
        4
  •  1
  •   Léo Léopold Hertz 준영    15 年前

    打印您的$\u get并在创建头之前检查var是否为空:

    print_r($_GET);
    

    或尝试

    $_REQUEST['email']
    

    而不是

    $_GET['email'] 
    

    哦!别忘了添加

    exit();
    

    后:

    header($header);
    

    如果您在没有有效$result的情况下执行某些操作,例如:

     if(isset($result)) {
         $header = ("Location: /codes/index.php?"                                                                                                                                                           
             . "question_sent"
             . "&"
             . "email="
             . $_GET['email']             // this seems to be correct to me
             . "&"
             . "passhash_md5="
             . $_GET['passhash_md5']      // this seems to be correct to me too
             );
         header($header);
      }
    
      sendEmailToMe('FAILED!!');
    

    可能是“sendmailtome('失败!!')”可以执行,即使$result有效。