Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

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

Posts: 137
Name: Casey
Trades: 3
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
 
 
Register now for full access!
Old 04-19-2008, 02:03 PM Re: Php Redirects
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
You could pass session variables from each page and use it in the header()
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is online now
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 04-19-2008, 02:07 PM Re: Php Redirects
addonchat's Avatar
Super Talker

Posts: 115
Name: Chris Duerr
Trades: 0
$_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

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
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
Trades: 0
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.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
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
Trades: 0
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 Search this Thread
Search this Thread:

Advanced Search

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

BB 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.15218 seconds with 12 queries