Page 1 of 1

[SOLVED] Redirect from string in URL

Posted: Sat Feb 14, 2004 7:57 am
by MarbleKittyKat
This is probably something really obvious, but can anyone see a bug in this script?

print "<script language='javascript'>";
if (strlen($redirect) == 0) { $redirect = "members_home.phtml"; }
print "self.location = '" .$redirect. "';";
print "</script>";

All this is supposed to do is find out if $redirect is set in the URL, if it is then redirect them to that page using JavaScript, and if it isn't, redirect them to members_home.phtml. The reason I need this is so that if someone is logging in from a different page, eg the forums, it would be handy to redirect them back to there rather than to members_home.phtml.

Anyone got any suggestions? There's no syntax error that I know of, but it just doesn't work a lot of the time.

Posted: Sat Feb 14, 2004 8:01 am
by markl999
Unless you have register_globals On (and it's Off by default with PHP => 4.2.0) then you'll need to use $_GET['redirect'] where you currently use $redirect

Posted: Sat Feb 14, 2004 8:13 am
by MarbleKittyKat
Well the thing is I use globals on other pages and it works ok.

I tried changing the script anyway to

print "<script language='javascript'>";
$redirect = $_GET['redirect'];
if (strlen($redirect) == 0) { $redirect = "members_home.phtml"; }
print "self.location = '" .$redirect. "';";
print "</script>";

But it made no improvement, it still goes to members home..

Thanks though.

Posted: Sat Feb 14, 2004 5:10 pm
by DuFF
Why don't use just use PHP or HTML to do the redirecting for you?

I am assuming you are passing a URL like this to redirect:
http://www.yoursite.com/redirect.php?redirect=home.php

If you do this, you can either redirect using PHP (This must be done before anything is outputted to the browser, whitespace and text included!)

Code: Select all

<?php
$redirect = $_GET['redirect'];
header("Location: $redirect");
?>
Or just use an HTML meta refresh to do it:

Code: Select all

&lt;meta http-equiv="refresh" content="0; url=&lt;?php echo $_GET&#1111;'redirect']; ?&gt;";
The content="0 part is how many seconds to wait before redirecting.

Posted: Sun Feb 15, 2004 7:39 am
by MarbleKittyKat
Thanks, I think this has solved it :)