Forum Moderators: coopster

Message Too Old, No Replies

typical yet unsolved redirection problem

         

cameraguy

10:35 pm on Dec 4, 2004 (gmt 0)

10+ Year Member



I am trying to "goto" a particular page with a header("Location: url") function. And I get a "Warning: Cannot modify header information" error.

Yes, I read all over the place that I must not output anything else to the server before the header.

OK, but how does that work in real life, when I have to use html to draw tables, declare CSS styles, etc?

I just cannot find a real-life example of a php file that manages that...

cameraguy

10:47 pm on Dec 4, 2004 (gmt 0)

10+ Year Member



What do you think of the two following suggestions I found:

1) upload to server a .htaccess file containing "PHP_FLAG output_buffering on".
(tried, but my server returns an error)

2) place <? ob_start();?> at the very beginning of your php page and <? ob_end_flush();?> at the very end. (works so far)

sjthomas

2:32 am on Dec 6, 2004 (gmt 0)

10+ Year Member



I don't really get what your trying to do here. There are plenty of real-world examples of using header to redirect to a different page, I use them all the time! Surely if your outputting tables and the like you don't actually want to redirect the user do you? Just get them to click on a link to take them to the next page. The best thing about using header is that it is invisible to (most) users. They click on index.php and get silently re-directed to homepage.php or whatever.

If you want to redirect them after stuff has loaded then use Javascript.

bubone2

3:27 am on Dec 6, 2004 (gmt 0)

10+ Year Member



yeah, I would do what sjthomas suggests and use a snippet of JavaScript in your code. Goes like this:

<script language=javascript>
parent.leftFrame.location="menu.php";
parent.mainFrame.location="login.php";
</script>

This is an adaptation of a line I have used .. in this case, I have a frame structure (leftFrame and mainFrame) for a menu and a main page. So do a conditional check (after submit for instance .. or whatever) and then execute this code. Be sure to note that the "parent.leftFrame.location" part of that will have to change according to the structure of your page.

Another way of doing it is to stay with HTML:

<meta http-equiv="refresh" content="0; url=login.php">

Use this in the same way, but executing after a conditional statement is met. This will only affect the page in which it is embedded. That is great for some (maybe even most) redirects. But if you need to affect more than one page at a time, use the JavaScript example. One nice thing about the meta redirect is that you can assign a time value to it (redirect after x number of seconds). That is what the number is for in: content="#

I tend to shy away from messing with the header at all, but that is due to my lack of comfort with it (not because it is insufficient for the task).

Hope that helps,
Josh

cameraguy

9:40 am on Dec 6, 2004 (gmt 0)

10+ Year Member



Thank you guys.

I guess I was trying to do it all with php. I really need to better understand how and when to use one language over the other.

Thanks again for the time!