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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)