douji6940 2014-12-05 13:05
浏览 48
已采纳

isset和$ _GET混乱

I've created a private messaging system but I have an annoying problem with $_GET.

First of all here's the full code for the "sendmail.php" page which is used for the form to send the mail

<?php
include("header.php");
include("connect.php");

$mailid = $_GET['username'];
$replyname = $_GET['user'];

?>

<form action="send.php" method="post" name="sendpm">
<table>
<tr>
  <td>
     <font color='orange'>To</font>
  </td>
  <?php IF ($mailid != ''){ ?>
  <td>
     <input type="text" name="touser" id="touser" value="<?php echo $mailid;?>"/>
  </td>
  <?php }elseif($replyname != ''){ ?>
  <td>
     <input type="text" name="touser" id="touser" value="<?php echo $replyname;?>"/>
  </td>
  <?php }else{ ?>
  <td>
     <input type="text" name="touser" id="touser" value="<?php echo $replyname;?>"/>
  </td>
  <?php } ?>
</tr>
<tr>
  <td>
     <font color='orange'>Subject</font>
  </td>
  <td>
     <input type="text" name="subject" id="subject" />
  </td>
</tr>   
<tr>
  <td>
     <font color='orange'>Message</font>
  </td>
  <td>
     <textarea name="message" cols="60" rows="10" id="message"></textarea>
  </td>
</tr>
<tr>
  <td colspan="2">
     <input type="submit" value="Send PM" />
  </td>
</tr>
</table>
</form>

<?php
include("footer.php");
?>

Okay so as you can see here

$mailid = $_GET['username'];
$replyname = $_GET['user'];

I can get two different results depending on where it comes from. The "mailid" is if you've been directed from a profile. The "replyname" is if you've clicked "reply" to a message you've received.

Now, if I try to send a mail from scratch, I will be told that "username" and "user" are undefined (as I haven't come from the links that include them). If I come from one link then the other will be undefined and vice versa.

I can 'solve' this by adding isset but for some reason, that keeps displaying a "1" instead of the username that should be shown (and IS shown if I follow a link without isset).

This is very annoying and I've been trying to solve it for ages!

I just realised isset deals in true or false. So the "1" means it's true, so I've changed it to

IF(isset($_GET['username'])){
$mailid = $_GET['username'];
}

IF(isset($_GET['user'])){
$replyname = $_GET['user'];
}

I thought this would be fine but still shows 1?

  • 写回答

5条回答 默认 最新

  • dongye6377 2014-12-05 13:24
    关注

    Well i would like to see how you are using isset. If you are trying something like this

    $mailid = isset($_GET['username']);
    $replyname = isset($_GET['user']);
    

    Then it will return boolean.

    Try this.

    <?php
    include("header.php");
    include("connect.php");
    ?>
    <form action="send.php" method="post" name="sendpm">
    <table>
    <tr>
      <td>
         <font color='orange'>To</font>
      </td>
      <td>
         <input type="text" name="touser" id="touser" value="<?php echo isset($_GET['username'])?$_GET['username']:$_GET['user']; ?>"/>
      </td>
    </tr>
    <tr>
      <td>
         <font color='orange'>Subject</font>
      </td>
      <td>
         <input type="text" name="subject" id="subject" />
      </td>
    </tr>   
    <tr>
      <td>
         <font color='orange'>Message</font>
      </td>
      <td>
         <textarea name="message" cols="60" rows="10" id="message"></textarea>
      </td>
    </tr>
    <tr>
      <td colspan="2">
         <input type="submit" value="Send PM" />
      </td>
    </tr>
    </table>
    </form>
    

    and dont forgot to filter your input.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解