Can PHP know when a page is refreshed?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • epots9
    Recognized Expert Top Contributor
    • May 2007
    • 1352

    Can PHP know when a page is refreshed?

    i was wondering if there is a way in php to figure out if the page was refreshed or it came from another page?
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Check out $_SERVER['HTTP_REFERER'].

    Comment

    • ronnil
      Recognized Expert New Member
      • Jun 2007
      • 134

      #3
      something like this

      Code:
      <?php
      if($_SERVER['HTTP_REFERER'] != $_SERVER['SCRIPT_URI'])
      {
          //This page was loaded from another page
      } else {
         //The page loaded itself
      }
      ?>
      Don't think there's a problem with $_GET data, but not sure

      Comment

      • Motoma
        Recognized Expert Specialist
        • Jan 2007
        • 3236

        #4
        Originally posted by ronnil
        something like this

        Code:
        <?php
        if($_SERVER['HTTP_REFERER'] != $_SERVER['SCRIPT_URI'])
        {
            //This page was loaded from another page
        } else {
           //The page loaded itself
        }
        ?>
        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!

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Originally posted by ronnil
          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'].

          Comment

          • epots9
            Recognized Expert Top Contributor
            • May 2007
            • 1352

            #6
            i still can't get it to work. if it is redirected by another page i go my tasks and set [PHP]$_SERVER["HTTP_REFER ER"] = "";[/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?

            Comment

            • Motoma
              Recognized Expert Specialist
              • Jan 2007
              • 3236

              #7
              Originally posted by epots9
              i still can't get it to work. if it is redirected by another page i go my tasks and set [PHP]$_SERVER["HTTP_REFER ER"] = "";[/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.

              Comment

              • epots9
                Recognized Expert Top Contributor
                • May 2007
                • 1352

                #8
                [CODE=javascript]location.href=l ocation.href[/CODE]

                seems to be working for me now..

                Comment

                Working...