note 106788 deleted from book.session by thiago
| From: | thiago@php.net | Date: | Tue, 06 Dec 2011 18:34:45 +0000 |
| Subject: | note 106788 deleted from book.session by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-184942@lists.php.net to get a copy of this message | ||
Note Submitter: eustace russell
----
I often see nubes like me asking how to detect when a page is refreshed. This simple function using
sessions does the trick for me. I have included the HTML to test in the script so just save it as
testingrefreshed.php but I changed the '<' to '[' and '>' to
']' so you will have to change them back.
<?php
session_start();
// testing if the page is refreshed
function refreshed($postrefresh)
{ // testing if the page is refreshed
//$postrefresh is the value of $_POST['refresh'];
if (!isset($_SESSION['refresh']) )
{
$ret['refreshed'] = false;
$_SESSION['refresh'] = $ret['newpostrefresh'] = 0;
}
elseif ($_SESSION['refresh'] == $_POST['refresh'])
{
$ret['refreshed'] = false;
$ret['newpostrefresh'] = ++$_SESSION['refresh'];
}
else
{
$ret['refreshed'] = true;
$ret['newpostrefresh'] = ++$_SESSION['refresh'];
}
return $ret;
}
print_r($_SESSION);
print_r($_POST);
$r = refreshed($_POST['refresh']);
echo "post refresh:{$_POST['refresh']} session
refresh:{$_SESSION['refresh']}<br>";
echo $r['refreshed']?'refreshed':'clicked';
?>
[html]
[head]
[title]testing refreshed[/title]
[/head]
[body bgcolor="#FFFFFF"]
[form name="input" action="testingrefreshed.php" method="POST"]
[input type="text" name="refresh" value="<?php echo
$r['newpostrefresh']?>" /]
[input type="submit" value="btn" /]
[/form]
[/body]
[/html]