On every page, have these links:
<a href="reg.php?referer=<?php echo urlencode($PHP_SELF); ?>">Register</a>
<a href="log.php?referer=<?php echo urlencode($PHP_SELF); ?>">Log in</a>
In the forms on these pages, pass along $referer as a hidden input.
Then, when they have registered or logged in, start a session (if you want to store their user auth that way) and write:
header("Location: $referer");