i'm not sure if php is a sensible way of doing it - it's much more suited to java scripting since it all happens on the client side.
For example, you couldnt have more than one php generated go back button:
Page 1) user Clicks to page 2
page 2) user clicks to page 3
page 3) user clicks "go back" which takes them to page 2 again
page 2) user clicks "go back", but they've come from page 3, so they actually go back to page three again
it's not a true "history" tool, so it can only work once. Thats okay if thats what you want to happen, but people expect to ba able to click a go back button as many times as needed, and actually "go back" through the pages they've previously visited.
Your code for the jave thing should be:
<a href="#" onClick="history.go(-1)">Go Back</a>
and there's no reason i can see why that wouldnt work - what problems are you having with it?