Redirect To Previous Page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Redirect To Previous Page

Post by nitediver »

Im going to make page that redirect to the previous page after login, anyone can help me?

After doing some search I found about using "$_SERVER['HTTP_REFERER']".

Any advice please, I really appreciate, thanks.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Redirect To Previous Page

Post by social_experiment »

nitediver wrote:Im going to make page that redirect to the previous page after login, anyone can help me?
Do you mean to an index page? Either way, tryin using 'header()'.

Code: Select all

<?php header('location: page.php'); exit(); ?>

There shouldn't be any other headers sent to the browser prior to this or you will receive an error, you could probably check for that with 'headers_sent()'.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
hypedupdawg
Forum Commoner
Posts: 74
Joined: Sat Apr 10, 2010 5:21 am

Re: Redirect To Previous Page

Post by hypedupdawg »

The easiest way to do this is actually not using php but javascript - all you need to do is insert this section of code in the head:

Code: Select all

<script type="text/javascript">function goBack(){window.history.go(-1);}</script>
The -1 needs to be replaced with the number of steps you want to jump backwards. So if your login is integrated into all pages, leave it as -1 (like on the Steam Forums). If it is on a separate page, as on this website, insert -2, to go back two steps, and so on and so on.

You can then call this function multiple times, eg. in a javascript block, or as a backup link:

Code: Select all

<a href="javascript :goBack()">Click here to be Redirected</a>
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Re: Redirect To Previous Page

Post by nitediver »

Sorry that's not what I mean.

Im building shopping cart website, I want to use redirect for this situation,
After choosing a product user must login first, after login I want to redirect the page to previous product already chosen by user, instead of index or other page.
Post Reply