Can you pass the page number and anchor name in the query string during the log in process and then redirect the user to the page in the querystring after a successful log in?
When they try to comment but are not logged in:
<a href="login.php?returnurl=article7.php#comment">Login to Comment</a>
So after they log in, you would just need to redirect them to “article7.php#comment”
You can also target ID’s the same way, so rather than stuffing some pointless empty anchor in there, put an ID on the element you want and then target it just like you would name.