Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Response.Redirect (ASP) in PHP after outputting some HTML?

Posted on 2004-11-04
9
Medium Priority
?
510 Views
Last Modified: 2008-09-14
In ASP I can buffer the response of the headers and html until I choose to sent it.
-->Response.Buffer = True

I googled and found: Header("Location: http://123.456.com\n\n"); \

But the question is....How can I send this header AFTER displaying some HTML?  Is there a setting for PHP that I can easily cache the output until the page is done processing?
0
Comment
Question by:djlurch
9 Comments
 
LVL 1

Accepted Solution

by:
apg88 earned 200 total points
ID: 12501091
Just print this to the html:
<SCRIPT LANGUAGE="JavaScript">
<!--
window.location="http:/http://123.456.com";
// -->
</script>
0
 
LVL 1

Author Comment

by:djlurch
ID: 12501101
If possible, I would rather do it through the headers than through Javascript.  Good suggestion though.
0
 
LVL 1

Expert Comment

by:apg88
ID: 12501118
The headers can only work before outputting anything into the html.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:djlurch
ID: 12501179
Can someone with lots of EE PHP points confirm this?  apq...you'll still get some points
0
 
LVL 12

Expert Comment

by:str_kani
ID: 12501515
In php you cannot change headers after sending some output,
only way is using the javascript. no way using php.
0
 
LVL 48

Expert Comment

by:hernst42
ID: 12501874
There is also such a option in PHP. Use output-buffering See http://de3.php.net/manual/en/ref.outcontrol.php So there is a way to send headers, even if there has been output before.

Use it like this:
------test.php-------------
<?php ob_start(); ?>
text
<?php header("Location: http://123.456.com"); ?>
sadsdas

0
 
LVL 12

Expert Comment

by:minichicken
ID: 12501877
Hi

apg88 and str_kani  are both right, you cannot send a header once there is an HTML output sent to the browser, even a blank space will give errors.
I would like to provide you a PHP Javascript combination code for redirecting without using header();

Example:

********************************************************************
<?
      function redirect($url)
      {
            echo "<script language='javascript'>\n";
            echo "function redirect()\n";
            echo "{\n";
            echo 'window.location = "'. $url .'"';
            echo "\n}\n";
            echo "redirect();\n";
            echo "</script>\n";
      }

redirect ("http://www.mydomain.com"); //dont have to be direct URL can be relative paths as well
?>

regards-
0
 
LVL 5

Assisted Solution

by:eyeh8u
eyeh8u earned 600 total points
ID: 12502625
If you examine the full HTTP packet that is sent to a users browser in response from a web server you will see it goes:

(http-headers)
(html-body)

In systems like PHP/ASP etc as soon as you send any content to the html-body the app server writes the HTML headers. However, in ASP, Response.Buffer defaults to True in ASP3.0 on IIS5 or better. This means that what ACTUALY happens is you write to a string which is buffered internaly by the ASP dll until page generation finishes, THEN it writes it to the atual output stream, triggering header creation. So you can response.redirect any time you like (unless Response.Buffer is false)

The same can be acheived in PHP using the output buffering features linked by hernst42. However, you MUST make sure that ob_start(); is called before ANYTHING is output to the page, including blank space prior to the first PHP tags

----

<?php
ob_start();
header("location: page.php");
?>
---

Will fail as the blank space prior to <?php will trigger http headers to be written.

---
<?php
ob_start();
//page logic
echo('here is some html');
?>
more html
<?php
//more page logic
if( $redirect )
{
  header('location: page.php'); // this will still work
}
//more page logic
echo('here is some more html');
ob_end_flush(); // write buffers to page, must call this to cleanly output content and sort everything out.
?>
---
0
 
LVL 1

Author Comment

by:djlurch
ID: 12504633
Thank you!

Eye: good call...it works great!

Every page has an include file...so I just put it the buffer call up top and the flush at the closing....so only 2 lines of code need to be changed for the whole site.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question