Kudose, thanks!
This approach has a small issue.
Say test.php is the page I want to detect if it is refresh or refer.
So first access, it was refer and session set up as your code. But if the client then leave this test.php goes to some other pages and then browse back (by refer) to this test.php again. But this second time access, due to the session value set up and tested, I would assume it is a refresh not a refer (but actually it was a refer.)
Unless I put these codes in every page of the site to continue reset up the lastPage value or at least I have to put these codes in the page linked to test.php, all the refer after the first access will be considered as refresh.