2014-12-05 80 views
-1

我创建了一个私人消息传递系统,但是我有一个讨厌的$ _GET问题。

的一切都在这里首先是针对“sendmail.php”网页的完整代码,用于形式发送邮件

<?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"); 
?> 

好了,你可以在这里看到

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

取决于它来自哪里,我可以得到两个不同的结果。 “mailid”是如果你从一个配置文件被定向的。 “回复姓名”是如果你点击了“回复”你收到的邮件。

现在,如果我尝试从头开始发送邮件,我会被告知“用户名”和“用户”未定义(因为我并非来自包含它们的链接)。如果我来自一个链接,那么另一个链接将不确定,反之亦然。

我可以通过添加isset来解决这个问题,但由于某种原因,它会一直显示一个“1”而不是应该显示的用户名(如果我按照没有isset的链接显示IS)。

这非常烦人,我一直在努力解决它很多年!

我刚刚意识到isset交易是真的还是假的。因此,“1”是指它是真实的,所以我把它改成

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

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

我想这将是罚款,但仍显示1?

+1

你在哪里回应他们? – 2014-12-05 13:11:09

+0

只是因为你不知道如何使用功能并不意味着它不能正常工作 – 2014-12-05 13:32:12

+0

@LoganMurphy任何需要的?一个函数显然总是能正常工作。 – Ben 2014-12-05 13:33:55

回答

0

嗯,我想看看你是如何使用isset。如果你正在尝试这样的事情

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

然后它会返回布尔值。

试试这个。

<?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> 

并且不忘记过滤您的输入。

+0

看到我的“答案”,我用isset看它是否在那里。如果你可以帮助更多的检查了这一点,因为每个人都忽略它大声笑http://stackoverflow.com/questions/27314895/weird-if-insert-statements-not-working?noredirect=1#comment43089737_27314895 – Ben 2014-12-05 13:28:30

+0

是的你是正确的。 Isset返回布尔值而不是值。只有一个建议使用三元运算符(?:)这很快,然后“如果其他”。此外,您可以像上面那样缩短您的代码。 – 2014-12-05 13:34:50

+0

谢谢,我使用了很多if和elses,这将有助于加载:) – Ben 2014-12-05 13:37:50

0

尝试用 -

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

isset回报boolean

0

也许它的工作原理,如果你还需要检查!空($ VAR)或尝试使用filter_input()

$mailid = filter_input(INPUT_GET, 'username'); 
0

我固定它自己(花了2小时,不能在这里工作了,后来看看吧直接去.lol)

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

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

这是因为isset只检查是否它是真的。

因此,它基本上是说“如果用户名是真实的,可变成为用户名”

0

我看不出有什么isset在你的代码,那么你如何使用它目前还不清楚。

通常你这样的(我更喜欢empty超过isset):

$mailIdOrName = ""; 

if (!empty($_GET['username'])) { 
    $mailIdOrName = $_GET['username']; 
} elseif (!empty($_GET['user'])) { 
    $mailIdOrName = $_GET['user']; 
} 

/* Later you will not need if-elseif-else */ 

<input type="text" name="touser" id="touser" value="<?php echo $mailIdOrName;?>"/>