Karem
Karem

Reputation: 18103

Detect whether the JS refreshed the page or if user did

Is there any way I can detect if the window got refreshed by user or if it was a window.location.reload(); js command that has been executed and refreshed the page ?

Because I want to unset a Filter session variable if the user has refreshed the page / navigated, and would like to keep this filter session variable if I did the reload (javascript).

I tried by working with the $_SERVER['HTTP_REFERER']

When I do window.location.reload(), $_SERVER['HTTP_REFERER'] becomes the full current path link, so I tried to do:

if(!isset($_SERVER['HTTP_REFERER']) || $_SERVER['HTTP_REFERER'] != url::base(true).Request::current()->uri())
{
    $session = Session::instance();
    $session->delete('deal_filter');
}

But this is not effective and only works at times.

If I can not detect it this way, is there any other method for accomplishing this?

Upvotes: 0

Views: 106

Answers (1)

Pete Schlette
Pete Schlette

Reputation: 2038

You could try submitting a POST request with a value to your page instead of refreshing. I suppose all you'd need to do is add a form,

<form name="refresh_form" action="mypage.php" method="post">
<input type="hidden" name="refresh" value="yes">
</form>

replace your Javascript code for refreshing with some code for submitting the form,

document.refresh_form.submit();

and check for the value in the PHP on your page:

if (isset($_POST['refresh']))
{
    // handle the case when your Javascript submitted the refresh
}

Upvotes: 2

Related Questions