homeHome contactContact portfolioPortfolio musicMusic videoVideo historyHistory calendarCalendar rssRSS
karlpox | with GOD all things are possible

Avoiding Repost on Refresh

Thanks to Ronald Borla and Google for the idea.

To avoid reposting datas from your form if your going to refresh the page. This problem is very common on forms being submitted to the same page where the form is. The simple solution to this problem is to make a unique session for ever form being submitted. Analyze the code below carefully and you can apply it easily in your website.

<form action="" method="POST">
<input type="hidden" name="uid" value="<?php $_SESSION['uid']=uniqid(); echo $_SESSION['uid']; ?>" />
<p>
<label>Message: </label>
<input type="text" name="msg" />
</p>
<input type="submit" value="Submit" />
</form>

As you can see we have created a unique session via the uniqid() function. Since you are refreshing on the same page make sure to have this on the same page as well most commonly above the form.
<?php
if ( $_POST['uid'] == $_SESSION['uid'] )
{
//your code to be process will be here
//and after processing your code you create another unique session id>
$_SESSION['uid'] = uniqid();
}
?>
 
By those code above you are guaranteed you will avoid reposting of datas upon refresh of page.

Write a Comment