arjang Posted June 29, 2011 Share Posted June 29, 2011 hi all:) it is me again having some problem with php. i hope you guys can help me again. i do appreciate your replies. i have a page which is linked from three different pages. visitors can get to this page via any of these three pages. i want to create a return link which returns visitors to the page they came from. i use the following php code which works fine but i wonder if it is good way of doing it!! or it is safe or not. one more thing, when clicking on the Go back link to searchresult page, returned articles dont exist anymore. you have to enter a key word again. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/240716-return-to-previous-page-using-php/ Share on other sites More sharing options...
Alfa9 Posted June 29, 2011 Share Posted June 29, 2011 Use Javascript With php : echo "<a href='#' onclick="javascript:history.back();">Go Back</a>"; Quote Link to comment https://forums.phpfreaks.com/topic/240716-return-to-previous-page-using-php/#findComment-1236401 Share on other sites More sharing options...
arjang Posted June 29, 2011 Author Share Posted June 29, 2011 thank you Alfa9, but this code return visitors to external pages too. for example if they came from google, it will return them back to google. Quote Link to comment https://forums.phpfreaks.com/topic/240716-return-to-previous-page-using-php/#findComment-1236406 Share on other sites More sharing options...
DavidT Posted June 29, 2011 Share Posted June 29, 2011 You may store the previous page information through a session. Quote Link to comment https://forums.phpfreaks.com/topic/240716-return-to-previous-page-using-php/#findComment-1236407 Share on other sites More sharing options...
xyph Posted June 29, 2011 Share Posted June 29, 2011 This is a good way of doing it with PHP. You could use JavaScript to do it as well <a href="javascript: history.go(-1)">Back</a> This will solve the issues with search results, however a user might be prompted that the page has expired if you've used POST to carry the data from one page to another. Using GET won't have this issue. DavidT - Sessions are NOT a good way of doing this. If the user has two tabs open at the same time, the session variable will be overwritten by either tab. arjang - Check the $_SERVER['HTTP_REFERER'] before giving the back button. You can't rely on it, but most users allow it to be passed. If it doesn't exist, you generally have a power user with a 5 button mouse who wouldn't use a previous page link. If it does, check to see that it matches your domain. Quote Link to comment https://forums.phpfreaks.com/topic/240716-return-to-previous-page-using-php/#findComment-1236408 Share on other sites More sharing options...
DavidT Posted June 29, 2011 Share Posted June 29, 2011 Won’t history.go(-1) still return the user to search engine, if that’s the case? Thanks xyph for the advice about sessions. I’m actually using this to store a $previous_page var, is there any other way to do this, then? Quote Link to comment https://forums.phpfreaks.com/topic/240716-return-to-previous-page-using-php/#findComment-1236424 Share on other sites More sharing options...
xyph Posted June 29, 2011 Share Posted June 29, 2011 The best way is to let the client deal with it through JavaScript. You don't NEED the referring page to be sent in the headers. The best way to make sure they don't just back-link to a search engine is the HTTP_REFERER value. Again, this can't be relied on, so a fallback is necessary. In my opinion, anyone that has the referrer blocked won't need a link to get them to the previous page. Quote Link to comment https://forums.phpfreaks.com/topic/240716-return-to-previous-page-using-php/#findComment-1236430 Share on other sites More sharing options...
arjang Posted June 29, 2011 Author Share Posted June 29, 2011 i used $_SERVER['HTTP_REFERER'] together with js but non is reliable. what if the server doesnt support $_SERVER['HTTP_REFERER'] or the visitor has js turned off? anyone looked at my code? is it good way of returning to previous page? it works fine but need to do some coding for the search result page. Quote Link to comment https://forums.phpfreaks.com/topic/240716-return-to-previous-page-using-php/#findComment-1236432 Share on other sites More sharing options...
xyph Posted June 29, 2011 Share Posted June 29, 2011 If the user has JS turned off, then they're used to losing convenience. The referrer is sent in the headers. All servers should support this. If the user doesn't provide this, assume they are a power user and don't need a redundant back button. If you start complicating your current method of storing the previous page in the query string, you may start opening up more XSS holes. If your search can have many parameters, things are going to get UGLY. If it's just a search string, you could pass that along in the query string as well and just do a check $page = $_GET['page']; if ($page=='index.php'){ echo "<a href='index.php'>back to latest article</a>"; } else if($page=='articles.php'){ echo "<a href='articles.php'>back to articles</a>"; } else if($page=='searchresult.php'){ $search = isset( $_GET['prev_search'] ) ? '?search='.$_GET['prev_search'] : ''; echo "<a href='searchresult.php$search'>back to search result</a>"; } Quote Link to comment https://forums.phpfreaks.com/topic/240716-return-to-previous-page-using-php/#findComment-1236442 Share on other sites More sharing options...
arjang Posted June 29, 2011 Author Share Posted June 29, 2011 Im using: <form id="searchbar" method="post" action="searchresult.php"> <ul> <li><input id="search-key" type="text" name="term" value="Search articles" size="25px"></li> <li><input id="searchsubmit" type="submit" name="submit" value=""></li> </ul> </form> to search the database. i used ur code but it is still the same, no result dsplayed!! this is my first website in php. i appreciate if you explain what the code does so that i learn how to use it. Quote Link to comment https://forums.phpfreaks.com/topic/240716-return-to-previous-page-using-php/#findComment-1236475 Share on other sites More sharing options...
xyph Posted June 29, 2011 Share Posted June 29, 2011 Change your method to 'get' Then use $_GET['search-key'] in searchresult.php You can then call searchresult.php?search-key=something to load a certain search result. Quote Link to comment https://forums.phpfreaks.com/topic/240716-return-to-previous-page-using-php/#findComment-1236562 Share on other sites More sharing options...
arjang Posted June 29, 2011 Author Share Posted June 29, 2011 thank you guys for the help:) Quote Link to comment https://forums.phpfreaks.com/topic/240716-return-to-previous-page-using-php/#findComment-1236571 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.