By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,640 Members | 2,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,640 IT Pros & Developers. It's quick & easy.

Can PHP know when a page is refreshed?

epots9
Expert 100+
P: 1,351
i was wondering if there is a way in php to figure out if the page was refreshed or it came from another page?
Jun 1 '07 #1
Share this Question
Share on Google+
7 Replies


pbmods
Expert 5K+
P: 5,821
Check out $_SERVER['HTTP_REFERER'].
Jun 1 '07 #2

ronnil
Expert 100+
P: 134
something like this

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if($_SERVER['HTTP_REFERER'] != $_SERVER['SCRIPT_URI'])
  3. {
  4.     //This page was loaded from another page
  5. } else {
  6.    //The page loaded itself
  7. }
  8. ?>
Don't think there's a problem with $_GET data, but not sure
Jun 1 '07 #3

Motoma
Expert 2.5K+
P: 3,235
something like this

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if($_SERVER['HTTP_REFERER'] != $_SERVER['SCRIPT_URI'])
  3. {
  4.     //This page was loaded from another page
  5. } else {
  6.    //The page loaded itself
  7. }
  8. ?>
Don't think there's a problem with $_GET data, but not sure
Good one ronnil! Thanks for posting, I hope to see you around here more often!
Jun 2 '07 #4

pbmods
Expert 5K+
P: 5,821
Don't think there's a problem with $_GET data, but not sure
GET variables will appear in $_SERVER['REQUEST_URI'], but not in $_SERVER['SCRIPT_URI'].
Jun 2 '07 #5

epots9
Expert 100+
P: 1,351
i still can't get it to work. if it is redirected by another page i go my tasks and set [PHP]$_SERVER["HTTP_REFERER"] = "";[/PHP]

but that doesn't work, cuz when i hit refresh the server overwrites it and sets it back to the value indicating that it came from another page once again.

how can i counter this?
Jun 4 '07 #6

Motoma
Expert 2.5K+
P: 3,235
i still can't get it to work. if it is redirected by another page i go my tasks and set [PHP]$_SERVER["HTTP_REFERER"] = "";[/PHP]

but that doesn't work, cuz when i hit refresh the server overwrites it and sets it back to the value indicating that it came from another page once again.

how can i counter this?
A quick and dirty solution would be to set the current page in a session variable. At the load of each page, make sure that the referer and that session variable match, then set the session variable again. This will not work for pages that link to themselves though.
Jun 4 '07 #7

epots9
Expert 100+
P: 1,351
Expand|Select|Wrap|Line Numbers
  1. location.href=location.href
seems to be working for me now..
Jun 4 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.