Jump to content

return to previous page using php


arjang

Recommended Posts

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]

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>";
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.