[SOLVED] Redirect from string in URL

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
MarbleKittyKat
Forum Newbie
Posts: 5
Joined: Sat Feb 14, 2004 7:57 am

[SOLVED] Redirect from string in URL

Post 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.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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
MarbleKittyKat
Forum Newbie
Posts: 5
Joined: Sat Feb 14, 2004 7:57 am

Post 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.
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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.
MarbleKittyKat
Forum Newbie
Posts: 5
Joined: Sat Feb 14, 2004 7:57 am

Post by MarbleKittyKat »

Thanks, I think this has solved it :)
Post Reply