Reply
Php Redirects
Old 04-19-2008, 01:41 PM Php Redirects
choskins102's Avatar
Super Talker

Posts: 124
Name: Casey
I know how to redirect a page using:

Code:
header("location:my_page.php");

However, I want to redirect a user back to where they came from. Basically, I have a login form on each page of my site. Once they click submit (no matter what area they are in,) they will be logged in. Instead of redirecting a page that I have set (ie. members_only.php), I want to send them back to where they came from. I know this can be done in javascript, but I don't want javascipt. Any ideas for PHP?
choskins102 is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 04-19-2008, 02:03 PM Re: Php Redirects
wayfarer07's Avatar
$frontend->developer

Posts: 1,037
Name: Abel Mohler
Location: Asheville, North Carolina USA
You could pass session variables from each page and use it in the header()
__________________
Go FREELANCE <=|If a donkey eats a melon, it is still a donkey... |=> Hire Me
wayfarer07 is offline
Reply With Quote
View Public Profile
 
Old 04-19-2008, 02:07 PM Re: Php Redirects
addonchat's Avatar
Skilled Talker

Posts: 97
Name: Chris Duerr
$_SERVER["HTTP_REFERER"] will work, but can be spoofed. The session idea mentioned would be best, but if you don't already have one open, I'd just use a form variable/CGI parameter (again, can be spoofed).
__________________
Chris Duerr
AddonChat Java Chat Software
http://www.addonchat.com/ - Affiliate Program
addonchat is offline
Reply With Quote
View Public Profile
 
Old 04-19-2008, 08:43 PM Re: Php Redirects
Providence's Avatar
Experienced Talker

Posts: 39
Quote:
Originally Posted by choskins102 View Post
I know how to redirect a page using:

Code:
header("location:my_page.php");

However, I want to redirect a user back to where they came from. Basically, I have a login form on each page of my site. Once they click submit (no matter what area they are in,) they will be logged in. Instead of redirecting a page that I have set (ie. members_only.php), I want to send them back to where they came from. I know this can be done in javascript, but I don't want javascipt. Any ideas for PHP?
1. Maybe you should get the page they are every time they are on your website. Maybe store them in a session or something. and whenever you want to redirect them, you can just refer to that.

2. HTTP_REFERER can also be used which is the thing that first came to mind.
Providence is offline
Reply With Quote
View Public Profile Visit Providence's homepage!
 
Old 04-19-2008, 10:39 PM Re: Php Redirects
PA Eddie's Avatar
Novice Talker

Posts: 8
Name: Eddie
Location: Irwin, PA
I would use both features. On the page they are viewing, you can make it so it sets the page they are viewing in a session and then on that redirect, you can make it redirect with the header.

I provided some simple examples. There is a huge downside to the first two of them, though. If you need to collect the URL every time they view a page, you will be paying the price by slowing down your server (server request can slow down the page). If the page only needs to be collected once and stored in a session, then it's different. You can block the server request from loading by seeing if the session exist or not. Here's some examples:
PHP Code:
<?php
/** Collect the server request, store it in a session, and do the redirect, and if they are viewing random pages before hand, you can always unset the old session and re-collect the new one **/

//simple server request and redirect
$_SESSION['page'] = $_SERVER['HTTP_REFERER'];

header("location:" .$_SESSION['page']. "");

//are they always on a random page on your site? if the session exist, unset the old one and make a new one
if((isset($_SESSION['page'])) || (session_is_registered($_SESSION['page']))) {
unset(
$_SESSION['page']);
//make the new session for the new page
 
$_SESSION['page'] = $_SERVER['HTTP_REFERER'];
} else {
//just collect it, don't unset it if it doesn't exist
 
$_SESSION['page'] = $_SERVER['HTTP_REFERER'];
}

//heres the third way, if they are not viewing a random URL on your site, this is good to use, but probably not :(
if((isset($_SESSION['page'])) || (session_is_register($_SESSION['page']))) {
//do nothing, we don't have to make the server request
} else {
//the session doesn't exist, so get it
 
$_SESSION['page'] = $_SERVER['HTTP_REFERER']; 
}

//and then of course, do the re-direct whenever you want like I did in the example I provided up top using the header function
 
header("location:" .$_SESSION['page']. "");
 
?>
PA Eddie is offline
Reply With Quote
View Public Profile Visit PA Eddie's homepage!
 
Reply     « Reply to Php Redirects
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.12722 seconds with 12 queries