我编写了一些警报系统。但我们不要看系统本身,让我们看看系统如何知道系统确实向浏览用户发送了警报/错误。函数进程之后,变量没有被定义/更新?
我做了一些事情,当你随机进入?alert = name,没有做任何错误,它会说'没有错误'。 但是,如果系统让你去?alert = name,它会回应错误。
如何处理我的帖子
function postComment() {
if (!empty($_POST['name']) || !empty($_POST['comment'])) {
$comment = mysql_real_escape_string(htmlentities($_POST['comment']));
$guest = mysql_real_escape_string(htmlentities($_POST['name']));
}
$guestId = 1;
if (empty($guest)) {
$alert = 1;
return header('location: index.php?alert=name');
}
if (empty($comment)) {
$alert = 2;
return header('location: index.php?alert=comment');
}
if (!isset($_COOKIE['alreadyPosted'])) {
mysql_query("INSERT INTO `comments` (`comment_guest`, `guest_id`, `comment`, `comment_date`, `comment_time`) VALUES ('$guest', '$guestId', '$comment', CURDATE(), CURTIME())") or die(mysql_error());
header('Location: index.php?action=sucess');
setcookie(alreadyPosted, $cookieId+1, time() + 60);
} else {
$alert = 3;
header('location: index.php?alert=delay');
}
}
正如你看到的,要检查用户是否真正得到这个错误,我会设置$警惕任何错误号是。
并检查HES得到的错误,我会用这个:
if (isset($_GET['alert']) == 'name') {
if ($alert == 1) {
echo 'hai';
} else {
echo 'No errors';
}
}
你可能会想,为什么我做这种方式..,还有因为我用1个功能后,我的岗位函数在窗体下,我希望警报显示在表单中。
问题:
变量要么不被设置为数字,它应该运行的功能, 或..东西从它阻止它的时候..我不知道..
我的猜测:因为在变量甚至被设置之前,错误检查位于postComment函数之前?
<?php
if (isset($_GET['alert']) == 'name') {
if ($alert == 1) {
echo 'hai';
} else {
echo 'No errors';
}
}
?>
<form action="index.php" method="POST">
<input type="text" name="name" placeholder="Your name here" class="field">
<textarea class="textarea" name="comment" placeholder="Your comment here..."></textarea>
<input type="submit" name="send" class="blue_button" value="Post Comment">
</form><a href="#"><input type="submit" name="" id="margin" class="blue_button" value="See all messages"></a>
<br />
<?php
//Show the comments
showComments();
if (isset($_POST['send'])) {
postComment();
}
if (isset($_GET['delete']) == "comment"){
deleteComment();
}
echo '<br />';
?>
如果是,那么解决方案是什么? 谢谢!
请不要对mysql_函数的故事开始,我明白&我会用PDO来代替,但我使用mysql_目前用于测试目的
但是,然后我假设用户可以只是去index.php?alert = name&alert_type = 1,看到完全相同的错误,对吧? – 2013-03-07 19:15:05
是的。如果您担心这一点,请将警报类型存储在'$ _SESSION'中。 – MichaelRushton 2013-03-07 19:16:30
是的,我基本上忘了$ _SESSION,会试试它的欢呼声! – 2013-03-07 19:17:43