Robert Elsdon Posted July 19, 2009 Share Posted July 19, 2009 hello, i hope yous can help me, Ok what it is, is when i post a comment on my shoutbox, it posts and i can see it, but when i press refresh it posts again, i want to prevent this from happning. any ideas? please help! thanks. <?PHP session_start(); ?> <? // Your database connection commands mysql_connect("localhost","root","12345678"); mysql_select_db("shoutbox"); if($_POST['submit']) { ?> <? $time=date("h:ia d/m/y"); $insertshout="INSERT INTO shoutbox(name,message,time) VALUES ('$name', '$message','$time')"; mysql_query($insertshout) or die("Cannot insert shout"); ?> <? } else { } ?> <? // Get the messages from the databse $get_messages = mysql_query("select * from shoutbox"); $amountofmessages = mysql_num_rows($get_messages); $select_shouts = mysql_query("select * from shoutbox ORDER BY id DESC LIMIT 10"); while($r=mysql_fetch_array($select_shouts)) { ?> <font color="#b2eb62"><?=$r["time"];?></font> <b><font color="#b2eb62"><?=$r["id"];?></font></b> <br> <b><i><?=$r["name"];?></i></b><font color="#b2eb62"> : </font><?=$r['message'];?><br> <? } ?> Quote Link to comment https://forums.phpfreaks.com/topic/166457-solved-how-to-stop-my-shoutbox-posting-after-i-refresh-page/ Share on other sites More sharing options...
ldougherty Posted July 19, 2009 Share Posted July 19, 2009 Anytime you press refresh any variables sent will be resent to the browser therefore causing it to resubmit and duplicate your post. This is just standard browser behavior, I suggest adding text to your page that says do not refresh the page after a submission. Quote Link to comment https://forums.phpfreaks.com/topic/166457-solved-how-to-stop-my-shoutbox-posting-after-i-refresh-page/#findComment-877783 Share on other sites More sharing options...
.josh Posted July 19, 2009 Share Posted July 19, 2009 send a token as a hidden input field. check for duplicates on post submission. Quote Link to comment https://forums.phpfreaks.com/topic/166457-solved-how-to-stop-my-shoutbox-posting-after-i-refresh-page/#findComment-877784 Share on other sites More sharing options...
Andy-H Posted July 19, 2009 Share Posted July 19, 2009 You could log their IP and restrict posts to 30 secs. Personally I would suggest recaptcha image verification. Quote Link to comment https://forums.phpfreaks.com/topic/166457-solved-how-to-stop-my-shoutbox-posting-after-i-refresh-page/#findComment-877785 Share on other sites More sharing options...
Robert Elsdon Posted July 19, 2009 Author Share Posted July 19, 2009 i fixed it.. <?PHP session_start(); ?> <? // Your database connection commands mysql_connect("localhost","root","12345678"); mysql_select_db("shoutbox"); if($_POST['submit']) { ?> <? $name = $_POST['name']; $message= $_POST['message']; $time=date("h:ia d/m/y"); $insertshout="INSERT INTO shoutbox(name,message,time) VALUES ('$name', '$message','$time')"; mysql_query($insertshout) or die("Cannot insert shout"); ?> [b]<script type="text/JavaScript">window.location='index.php';</script>[/b] <? } else { } ?> <? // Get the messages from the databse $get_messages = mysql_query("select * from shoutbox"); $amountofmessages = mysql_num_rows($get_messages); $select_shouts = mysql_query("select * from shoutbox ORDER BY id DESC LIMIT 10"); while($r=mysql_fetch_array($select_shouts)) { ?> <?=$r["time"];?> <b><font color="#b2eb62"><?=$r["id"];?></font></b> <br> <b><?=$r["name"];?></b><font color="#b2eb62"> : </font><?=$r['message'];?><br> <? } ?> Quote Link to comment https://forums.phpfreaks.com/topic/166457-solved-how-to-stop-my-shoutbox-posting-after-i-refresh-page/#findComment-877786 Share on other sites More sharing options...
MadTechie Posted July 19, 2009 Share Posted July 19, 2009 okay a little slow but i wrote a quick example of the using a token <?php session_start(); if(!empty($_POST['data'])) { if($_SESSION['token'] == $_POST['token']) { echo "User submitted: ".$_POST['data']; }else{ echo "Error: invalid Token"; } } $_SESSION['token'] = uniqid("token",true); ?> <form action="" method="post"> <input name="token" type="hidden" value="<?php echo $_SESSION['token'];?>" /> <textarea name="data" cols="50" rows="3"></textarea><br /> <input name="submit" type="submit" value="submit" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/166457-solved-how-to-stop-my-shoutbox-posting-after-i-refresh-page/#findComment-877790 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.