Hi there.
I am creating a social networking website and I am having an issue when posting messages on a users profile. The message posts correctly and displays correctly too but when I refresh the page a duplicate of the last message is submitted. I do not want to prevent the user from submitting more than one message in a single visit but I do need to stop messages from being posted on refresh.
I hope you can help, here is my code below.
<?php // rnmessages.php
include_once 'rnheader.php';
if (!isset($_SESSION['user']))
die("<br /><br />You need to login to view this page");
$user = $_SESSION['user'];
if (isset($_GET['view'])) $view = sanitizeString($_GET['view']);
else $view = $user;
if (isset($_POST['text']))
{
$text = sanitizeString($_POST['text']);
if ($text != "")
{
$pm = substr(sanitizeString($_POST['pm']),0,1);
$time = time();
queryMysql("INSERT INTO rnmessages VALUES(NULL,
'$user', '$view', '$pm', $time, '$text')");
}
}
echo <<<_END
<form method='post' action='rnmessages.php?view=$view'>
Type here to leave a message:<br />
<textarea name='text' cols='40' rows='3'></textarea><br />
Public<input type='radio' name='pm' value='0' checked='checked' />
Private<input type='radio' name='pm' value='1' />
<input type='submit' value='Submit' name='submit' /></form>
_END;
$query = "SELECT * FROM rnmessages WHERE recip='$view'
ORDER BY time DESC";
$result = queryMysql($query);
$num = mysql_num_rows($result);
for ($j = 0 ; $j < $num ; ++$j)
{
$row = mysql_fetch_row($result);
if ($row[3] == 0 ||
$row[1] == $user ||
$row[2] == $user)
{
echo date('M jS \'y g:sa:', $row[4]);
echo " <a href='rnmessages.php?";
echo "view=$row[1]'>$row[1]</a> ";
if ($row[3] == 0)
{
echo "wrote: "$row[5]" ";
}
else
{
echo "whispered: <i><font
color='#006600'>"$row[5]"</font></i> ";
}
if ($row[2] == $user)
{
echo "[<a href='rnmessages.php?view=$view";
echo "&erase=$row[0]'>erase</a>]";
}
echo "<br>";
}
}
}
if (!$num) echo "<li>No messages yet</li><br />";
echo "<br><a href='rnmessages.php?view=$view'>Refresh messages</a>";
echo " | <a href='rnfriends.php?view=$view'>View $name2 friends</a>";
?>
Thanks Luke.