capturing redirect query strings

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
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

capturing redirect query strings

Post by kendall »

Hey ppl,

The following code redirects a user to a login form if a session isnt present

Code: Select all

if(!session_is_registered('User')){
	$query = $_SERVER['QUERY_STRING'];
	header('Location: login_form.php?redirect=post_message_form.php?'.$query);
}
in the login form page

Code: Select all

if($_REQUEST['redirect'])
	$redirect = $_REQUEST['redirect'];
else $redirect = $PHP_SELF.'?'.$_SERVER['QUERY_STRING'];
how ever in running a request with a paramters script.php?redirect=page.php?param=1&param=2&param=3
$redirect = page.php?param=1
so what happens to the rest?

how do you guys handle redirecting situations?

the thing is i want the person to be able to go back to the way he was before he had to go log in and stuff after he logs in

Kendall
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

The first GET variable has to use a ? before the variable name. All the others after it must use &.
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post by kendall »

Sami,

uhhm...so what does that mean? it cant see pass the '&'?

Kendall
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

You could probably use urlencode() to encode you redirect string then use that as a url parameter. Then, $_GET that string and urldecode() it.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Re: capturing redirect query strings

Post by m3mn0n »

I would do this

Send a get variable to the page, eg.

?error=true

and then also send

&errortype=nosession

And then within a section of your page, add a series of header's that will redirect a user to a specific page based on if the error exist and which type of error it is.

eg.

Code: Select all

<?php
if ($_GET['error'] == true)
{
 switch ($_GET['errortype'])
  default:
    header ("Locaiton: error1.php");
  break;
  case 'nosession':
    header ("Locaiton: error1.php");
  break;
  case 'hackattempt':
    header ("Locaiton: error2.php");
  break;
  case 'missingvariable':
    header ("Locaiton: error3.php");
  break;
 }
}
?>
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post by kendall »

Yes,

microthick...ok ill try that

sami...yes but...why is it that if i have

Code: Select all

$redirect = 'script.php?param=1&param=2'; 
header('Location: script2.php?redirect='.$redirect);
i will get
$redirect = script.php?param=1
and not
$redirect = script.php?param=1&param=2
?
Kendall
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

you need:

Code: Select all

$redirect = urlencode('script.php?param=1&param=2'); 
header('Location: script2.php?redirect='.$redirect);
then:

Code: Select all

$redirect=urldecode($_REQUEST["redirect"]);
Post Reply