function for redirect

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
competent
Forum Newbie
Posts: 8
Joined: Mon May 17, 2004 5:37 pm

function for redirect

Post by competent »

Hello!!! :)
I would like to avoid using redirection through the
<?php
header("Location: some_location.php");
?>
Is there any othher way to do this!!!
Thanks for any suggestions
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

You could use an html meta-refresh
competent
Forum Newbie
Posts: 8
Joined: Mon May 17, 2004 5:37 pm

Yeah but...

Post by competent »

I don`t want header redirect because it must be before any information is sent to browser. Meta redirect can be in <html> but only in <head> with other meta tags. So meta redirect is not the remedy for it. But thank you anyway. If you know any other kinds of redirections which can be used without warnings/errors in <body> part of the website I would be grateful
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

acctually it's suppose to be in the head but it will work anywhere in the page, a bit messy but i use em all the time rather then header redirects
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

You've confused me.
You either want to redirect before any output, in which case header() will do it, or you want to display some output, then redirect, in which case a meta-refresh will do it.
What other senario do you have?
competent
Forum Newbie
Posts: 8
Joined: Mon May 17, 2004 5:37 pm

Post by competent »

No. It`s true that meta tag works after any output is sent to browser but BEFORE <body> witch is the case. I want to redirect AFTER <body> tag, and meta tag won`t do it.

eg with header which would return error that it cannot send header info.
<body>
<?php
if(sth)
sth;
else
header("Location: some_page.php");
?>
</body>
Last edited by competent on Mon May 17, 2004 7:45 pm, edited 1 time in total.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

how about javascript?

<script language=javascript>location.replace('http://www.disneyland.com')</script>
competent
Forum Newbie
Posts: 8
Joined: Mon May 17, 2004 5:37 pm

Post by competent »

tim wrote:how about javascript?

<script language=javascript>location.replace('http://www.disneyland.com')</script>
Yeah maybe, but what if sb doesn`t have javascript enabled?? :)
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

lol then perhaps your asking the impossible

=]
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

you could set a time delay.....

Code: Select all

<meta http-equiv="refresh" content="time in seconds" url="url">
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

*cough* *cough*

Code: Select all

<?php ob_start(); ?>
<body>
<?php
header("Location: some_page.php");
?>
</body>
[php_man]ob_start[/php_man]

Behold, the power of output buffering.
hedge
Forum Contributor
Posts: 234
Joined: Fri Aug 30, 2002 10:19 am
Location: Calgary, AB, Canada

Post by hedge »

well if you need to do that, you're logic is hooped. You should figure out what actions you need to take before you get to that point.

I have ran into cases where I cannot use a header though (usually when trying to target frames) so I use the following function, it provides a clickable link that is automatically ran with javascript but they can click if js is disabled.

Code: Select all

function urlRedirect($url, $msg='') &#123;
     $msg .= '<br><br>Redirection In Progress.  Click here if your browser does not support automatic redirection!';
     header('Pragma: no-cache');
     header('Cache-Control: no-cache, must-revalidate');
     echo '<html>';
     echo '<head>';
     echo ' <style type="text/css">';
     echo '  A &#123;color:#336699; font-family:Arial; font-size: 12pt; font-weight:bold&#125;';
     echo ' </style>';
     echo ' <script language="javaScript">';
     echo '  function redirect() &#123;window.parent.location=''' . $url . ''';&#125;';
     echo ' </script>';
     echo '</head>';
     echo '<body onload="setTimeout(''redirect()'',2000);">';
     echo '<table border=0 width=100% height=100%><tr>';
     echo '<td width=20%>&nbsp;</td>';
     echo '<td width=60% align="center" valign="center"><a target="_parent" href="' . $url . '">' . $msg . '</a></td>';
     echo '<td width=20%>&nbsp;</td>';
     echo '</tr></table>';
     echo '</body></html>';
     exit();
   &#125;
PS. I hate sights that use meta-refresh html with a timeout of zero... when you go back through your history you get captured by that page because you have no time to click past it.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

i like that function hedge...

im slightly using it on my user authentication script
Post Reply