On your second page put the following code BEFORE you do that IF statement check.
echo $_SERVER['HTTP_REFERER'];
The value of that is what you want to check the HTTP_REFERER for. In my test the value for that was ALWAYS page2.php
First time you login the HTTP_REFERER is not set... it is set the second time (to the correct value) which then shows your SESSION variables.