jonw118 Posted December 16, 2008 Share Posted December 16, 2008 Ok, I have a script that if you perform an action, it calls a php file to perform the action... then that php file that is called sends the user to the page specified in the file with the: header('Location: X.php'); What I really need it to do is send it back 2 pages... is this possible? I tried using javascript to get it to go back... but that didn't do the trick, just stalled out. Any ideas would be greatly appreciated! Quote Link to comment Share on other sites More sharing options...
ratcateme Posted December 16, 2008 Share Posted December 16, 2008 well i would use window.history.go(-2) in javascript but if you don't want to use javascript you could store the last few pages viewed in $_SESSION and redirect them with vars from it. Scott. Quote Link to comment Share on other sites More sharing options...
studgate Posted December 16, 2008 Share Posted December 16, 2008 you can simply use a function that redirect to any page that you want do this: function redirect ($location){ header('Location: '.$location); exit; } Usage: redirect (../../pagetosend.php); Quote Link to comment Share on other sites More sharing options...
jonw118 Posted December 16, 2008 Author Share Posted December 16, 2008 Studgate... the problem is the page I need it to send back to varies. Basically there are multiple pages that all access one php file. Once they perform the action I want them sent back 2 pages... and that will vary based on where they came from. Ratcateme... it's not that I'm opposed to JS, I just can't make it work. When I put that in, it just stalls on the php file it calls (but does perform that action). Here's my code, it's late, am I missing something: edit, I meant to post this is what I have: header('Location: <script language="JavaScript">window.history.go(-2)</script>'); Quote Link to comment Share on other sites More sharing options...
studgate Posted December 16, 2008 Share Posted December 16, 2008 have you tried this?? <a href="javascript:history.go(-2)">GO back</a> Quote Link to comment Share on other sites More sharing options...
jonw118 Posted December 16, 2008 Author Share Posted December 16, 2008 Yeah I tried that just now... same thing. Quote Link to comment Share on other sites More sharing options...
jonw118 Posted December 16, 2008 Author Share Posted December 16, 2008 This would do the trick for me if I only wanted to go back one page, but I need to go back 2. header("Location: " . $_SERVER['HTTP_REFERER']); Is it possible to make that go back 2? Quote Link to comment Share on other sites More sharing options...
ratcateme Posted December 16, 2008 Share Posted December 16, 2008 you have the js wrong try <a href="javascript:window.history.go(-2)">GO back</a> Scott. Quote Link to comment Share on other sites More sharing options...
jonw118 Posted December 16, 2008 Author Share Posted December 16, 2008 Unfortunately Scott, that didn't do it... here's what I used: header('Location: <a href="javascript:window.history.go(-2)">GO back</a>'); Quote Link to comment Share on other sites More sharing options...
ratcateme Posted December 16, 2008 Share Posted December 16, 2008 no you need to put it in as html code you cannot send it in a header it needs to be like <html> <body> <a href="javascript:window.history.go(-2)">GO back</a> </body> </html> Scott. Quote Link to comment Share on other sites More sharing options...
trq Posted December 16, 2008 Share Posted December 16, 2008 PHP has no recollection of any previous request so no, there is no simple method of redirecting (via a call to header) to a place you where two requests ago. Quote Link to comment Share on other sites More sharing options...
waynew Posted December 16, 2008 Share Posted December 16, 2008 You could work out something using cookies, or sessions if you want to. Quote Link to comment Share on other sites More sharing options...
Parhs Posted December 16, 2008 Share Posted December 16, 2008 header('Location: <script language="JavaScript">window.history.go(-2)</script>'); THat is totally wrong.How tec heck would this work however. For every page the user visits make a function that stores in an array the history. like that i suppose that you have started the session. function keepHistory() { $history=$_SESSION["history"]; $history[]=$_SERVER["PHP_SELF"]; $_SESSION["history"]=$history; } call this stuff at every page that user sees. then you would just do something like that $history=$_SESSION["history"]; header("Location : $history[Elements of array -2]"); Quote Link to comment Share on other sites More sharing options...
Adam Posted December 16, 2008 Share Posted December 16, 2008 I'd recommend something along the lines of what Parhs is showing you.. But you should be able to use: header("Location: javascript:window.history.go(-2);"); ** Not tested - plus it requires the user to have JS enabled.. A Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.