<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="http://php.dzone.com"  xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dz="http://www.developerzone.com/modules/dz/1.0" xmlns:media="http://search.yahoo.com/mrss/">
<channel>
 <title>PHP Zone - Comments for &quot;PHP Redirect Function&quot;</title>
 <link>http://php.dzone.com/news/php-redirect-function</link>
 <description>Comments for &quot;PHP Redirect Function&quot;</description>
 <language>en</language>
<item>
 <title>Hey guys!
Those of you who</title>
 <link>http://php.dzone.com/news/php-redirect-function#comment-3408</link>
 <description>&lt;!--paging_filter--&gt;&lt;p&gt;Hey guys!
&lt;/p&gt;&lt;p&gt;Those of you who are wondering about the header and why it executes the lines after it, well apparantly Amir is right, PHP will execute all the commands after header and then redirect. You need to use exit() but that will not clear the resources on the server. This is especially necessary if you are redirecting to another server / website. For that, use die() so that the site clears all the resources. It will look something like this:&lt;/p&gt;&lt;p&gt;header (&#039;Location:&#039;.$URL) ;&lt;br /&gt;die();&lt;/p&gt;&lt;p&gt;Remember that when you use the syntax above, then you cannot use any type of print statements before it else you get the error, headers already sent or something like that. The basic culprits for this error are print, echo or html tags. there are 2 ways to by pass this issue, either declare a redirect function on the top of the page and call that whenever you need to redirect but a more stable way is to use &amp;quot;ob&amp;quot; functions. Something like:&lt;/p&gt;&lt;p&gt;ob_start();&lt;br /&gt;echo &amp;quot;Who Who Ha Ha! Blah Blah Bling!&amp;quot;; &lt;br /&gt;header (&#039;Location:&#039;.$URL);&lt;br /&gt;ob_flush();&lt;/p&gt;&lt;p&gt;That should do the trick. Hope that this helps some innocent soul from not spending hours on figuring out what they need to do.&lt;br /&gt;Regards,&lt;br /&gt;Ali&lt;/p&gt;</description>
 <pubDate>Wed, 21 May 2008 22:42:42 -0400</pubDate>
 <dc:creator>arashidsw</dc:creator>
 <guid isPermaLink="false">comment 3408 at http://php.dzone.com</guid>
</item>
<item>
 <title>I&#039;m having the same issue as</title>
 <link>http://php.dzone.com/news/php-redirect-function#comment-3396</link>
 <description>&lt;!--paging_filter--&gt;&lt;p&gt;I&#039;m having the same issue as as viccowface.  I&#039;m learning PHP and have arrived at a point where I have to update some information in the database.  When that&#039;s successful I&#039;d like to redirect back to a page.  Here&#039;s my code:&lt;/p&gt;&lt;p&gt;&amp;lt;?php require_once(&amp;quot;includes/connection.php&amp;quot;);?&amp;gt;&lt;/p&gt;&lt;p&gt;&amp;lt;?php require_once(&amp;quot;includes/functions.php&amp;quot;);?&amp;gt;&lt;/p&gt;&lt;p&gt;&amp;lt;?php&lt;/p&gt;&lt;p&gt;	$menu_name = $_POST[&#039;menu_name&#039;];&lt;/p&gt;&lt;p&gt;	$position = $_POST[&#039;position&#039;];&lt;/p&gt;&lt;p&gt;	$visible = $_POST[&#039;visible&#039;];&lt;/p&gt;&lt;p&gt;?&amp;gt;&lt;/p&gt;&lt;p&gt;&amp;lt;?php $query = &amp;quot;INSERT INTO subjects (&lt;/p&gt;&lt;p&gt;				menu_name, position, visible&lt;/p&gt;&lt;p&gt;				) VALUES (&lt;/p&gt;&lt;p&gt;					&#039;{$menu_name}&#039;, {$position}, {$visible}&lt;/p&gt;&lt;p&gt;					)&amp;quot;;			&lt;/p&gt;&lt;p&gt;	if (mysql_query($query, $connection)) {&lt;/p&gt;&lt;p&gt;		// SUCCESS!&lt;/p&gt;&lt;p&gt;		header(&amp;quot;location: content.php&amp;quot;);&lt;/p&gt;&lt;p&gt;		exit;&lt;/p&gt;&lt;p&gt;	} else {&lt;/p&gt;&lt;p&gt;		//Display error message.&lt;/p&gt;&lt;p&gt;		echo &amp;quot;&amp;lt;p&amp;gt;Subject creation failed.&amp;lt;/p&amp;gt;&amp;quot;;&lt;/p&gt;&lt;p&gt;		echo &amp;quot;&amp;lt;p&amp;gt;&amp;quot; . mysql_error() . &amp;quot;&amp;lt;/p&amp;gt;&amp;quot;;&lt;/p&gt;&lt;p&gt;	}?&amp;gt;&lt;/p&gt;&lt;p&gt;&amp;lt;?php mysql_close($connection)?&amp;gt; &lt;/p&gt;&lt;p&gt; I&#039;ve employed the tip at the top of this post but I still get the header error that I : Cannot modify header information - headers already sent. &lt;/p&gt;&lt;p&gt;I&#039;ve read that PHP 5+ doesn&#039;t allow for headers to be resent to avoid an injection threat.  How can I redirect people after successfully completing the upload to the database if I can&#039;t use HEADER?&lt;/p&gt;&lt;p&gt; Any help would be greatly appreciated.  I&#039;m stuck here. &lt;/p&gt;</description>
 <pubDate>Wed, 21 May 2008 09:57:10 -0400</pubDate>
 <dc:creator>fightingmongoose</dc:creator>
 <guid isPermaLink="false">comment 3396 at http://php.dzone.com</guid>
</item>
<item>
 <title>hi... I got a problem with</title>
 <link>http://php.dzone.com/news/php-redirect-function#comment-3328</link>
 <description>&lt;!--paging_filter--&gt;&lt;p&gt;hi... I got a problem with the header.... my code is....&lt;/p&gt;&lt;p&gt;&amp;lt;?php&lt;/p&gt;&lt;p&gt;include &amp;quot;conexion.php&amp;quot;;  //llama a conectar a BD&lt;/p&gt;&lt;p&gt;$result = mysql_query(&amp;quot;select usuario, pass from t_usuario where usuario = &#039;$usuario&#039; and pass = &#039;$pass&#039;&amp;quot;, $conexion);&lt;br /&gt;if(mysql_num_rows($result)&amp;gt;0){  &lt;br /&gt;    header( &amp;quot;Location: &lt;a href=&quot;http://www.solucionesclimaticascr.com/ingreso.html&quot;&gt;http://www.solucionesclimaticascr.com/ingreso.html&lt;/a&gt;&amp;quot; ); &lt;br /&gt;    exit; &lt;br /&gt;  } else {&lt;br /&gt;  echo &amp;quot;No le es permitido ingresar a esta &amp;amp;aacute;rea, es restringida&amp;quot;;&lt;br /&gt; &lt;br /&gt; }&lt;br /&gt;  &lt;br /&gt;include &amp;quot;cerrar_conexion.php&amp;quot;;&lt;br /&gt;?&amp;gt;&lt;/p&gt;&lt;p&gt;it doesn&#039;t work the header.... don&#039;t call the page.... now if I use this one:&lt;/p&gt;&lt;p&gt;&amp;lt;?php&lt;/p&gt;&lt;p&gt;include &amp;quot;conexion.php&amp;quot;;  //llama a conectar a BD&lt;/p&gt;&lt;p&gt;$result = mysql_query(&amp;quot;select usuario, pass from t_usuario where usuario = &#039;$usuario&#039; and pass = &#039;$pass&#039;&amp;quot;, $conexion);&lt;br /&gt;if(mysql_num_rows($result)&amp;gt;0){   &lt;br /&gt;  echo &amp;quot;&amp;lt;td&amp;gt;Su ingreso es satisfactorio! &amp;lt;/td&amp;gt; \n&amp;quot;;&lt;br /&gt;  //echo &amp;quot;&amp;lt;td&amp;gt; &amp;lt;/td&amp;gt; \n&amp;quot;;&lt;br /&gt;  echo &amp;quot;&amp;lt;td&amp;gt;&amp;lt;a href=&#039;lista_usuarios_noticias.php&#039;, &#039;mainFrame&#039;&amp;gt;Ingrese ac&amp;amp;aacute; para administrar listado de personas inscritas en Newsletter.&amp;lt;/a&amp;gt;&amp;lt;/td&amp;gt; \n&amp;quot;;&lt;br /&gt;   } else {&lt;br /&gt;  echo &amp;quot;No le es permitido ingresar a esta &amp;amp;aacute;rea, es restringida&amp;quot;;&lt;br /&gt; &lt;br /&gt; }&lt;br /&gt;  &lt;br /&gt;include &amp;quot;cerrar_conexion.php&amp;quot;;&lt;br /&gt;?&amp;gt;&lt;/p&gt;&lt;p&gt; it works... but i don&#039;t need display the link... i need if &amp;gt;0 go to page ... ingreso.html and display it on the mainframe cause my page has frames (top and mainframe)&lt;/p&gt;&lt;p&gt;where is my error?&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;tnks&lt;/p&gt;</description>
 <pubDate>Fri, 16 May 2008 15:21:23 -0400</pubDate>
 <dc:creator>viccowface</dc:creator>
 <guid isPermaLink="false">comment 3328 at http://php.dzone.com</guid>
</item>
<item>
 <title>it&#039;s a client-side</title>
 <link>http://php.dzone.com/news/php-redirect-function#comment-3053</link>
 <description>&lt;!--paging_filter--&gt;&lt;p&gt;it&#039;s a client-side redirect.&lt;/p&gt;&lt;p&gt;The header function deals with HTTP response headers so the server script will not deal with it much differently than any other header. The script will just run its natural course. You&#039;ll never see the output in the browser because your browser will simply follow the redirect and ignore the output.&lt;/p&gt;&lt;p&gt;You can see for yourself by setting up a small script, then using telnet from a command prompt &lt;/p&gt;&lt;p&gt; Example script redirect-script.php (in your document root):&lt;/p&gt;&lt;pre class=&quot;php&quot;&gt;&amp;lt;?php&lt;br /&gt;header(&#039;Location: http://www.google.com/&#039;);&lt;br /&gt;echo &amp;quot;private stuff\n&amp;quot;;&lt;br /&gt;?&amp;gt;&lt;/pre&gt;&lt;p&gt;Telnet test:&lt;/p&gt;&lt;p&gt;Type the following from a dos or unix command prompt: &lt;/p&gt;&lt;blockquote&gt;&lt;pre&gt; telnet yourservername.com 80&lt;/pre&gt;&lt;/blockquote&gt;&lt;p&gt;(Personally I&#039;m using localhost. It&#039;ll say something about your server) &lt;/p&gt;&lt;p&gt;Now type in the following HTTP request header, followed by 2 carriage returns:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt; &lt;code&gt;GET /redirect-script.php  HTTP/1.0 &lt;/code&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;(you need to press enter twice before the server knows you&#039;re finished)&lt;/p&gt;&lt;p&gt;It should output something like this ... &lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;HTTP/1.1 302 Found&lt;br /&gt;Date: Sat, 03 May 2008 08:26:59 GMT&lt;br /&gt;Server: Apache/2.2.8 (Ubuntu) PHP/5.2.4-2ubuntu5 with Suhosin-Patch&lt;br /&gt;X-Powered-By: PHP/5.2.4-2ubuntu5&lt;br /&gt;Location: http://www.google.com/&lt;br /&gt;Content-Length: 14&lt;br /&gt;Connection: close&lt;br /&gt;Content-Type: text/html&lt;br /&gt;&lt;br /&gt;private stuff&lt;br /&gt;Connection closed by foreign host.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;As you can see it still executes the private stuff, whether it be unauthorised data manipulation or just printing top-secret information back in the response. One other thing to note is that PHP cleverly realises it needs to set the HTTP response code to 302 (rather than 200 OK) &lt;/p&gt;&lt;p&gt;hth&lt;/p&gt;&lt;p&gt; Amir &lt;/p&gt;</description>
 <pubDate>Sat, 03 May 2008 04:40:11 -0400</pubDate>
 <dc:creator>amir75</dc:creator>
 <guid isPermaLink="false">comment 3053 at http://php.dzone.com</guid>
</item>
<item>
 <title>@David - I&#039;m new to PHP</title>
 <link>http://php.dzone.com/news/php-redirect-function#comment-3049</link>
 <description>&lt;!--paging_filter--&gt;&lt;p&gt;@David - I&#039;m new to PHP (coming from other server side web languages though) and not sure I understand the need for exit(); &lt;/p&gt;&lt;pre class=&quot;php&quot;&gt;header(&#039;Location: &#039;.$url);&lt;br /&gt;exit();&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;If you are already doing a redirect (using header) then why exit the script? Will it not exit automatically as you&#039;re redirecting?&lt;/p&gt;&lt;p&gt;Sorry if that&#039;s a silly qestion!&lt;/p&gt;&lt;p&gt;&lt;b&gt;Update&lt;/b&gt;: Hmm, after reading the docs at php.net it seems you really do need to exit() after calling header(). Still not sure why though if it&#039;s a server redirect. &lt;/p&gt;</description>
 <pubDate>Sat, 03 May 2008 01:12:07 -0400</pubDate>
 <dc:creator>sharmo</dc:creator>
 <guid isPermaLink="false">comment 3049 at http://php.dzone.com</guid>
</item>
<item>
 <title>I think a header Location</title>
 <link>http://php.dzone.com/news/php-redirect-function#comment-1828</link>
 <description>&lt;!--paging_filter--&gt;&lt;p&gt;I think a header Location redirect is an awkward way to force people into a direction. Even after a form submission there are more gentle ways to do that.&lt;/p&gt;&lt;p&gt; I would say: avoid this kind of redirects all together. Why? Because it is another request on your server. If you can&#039;t handle the request at once, why redirect it?&lt;/p&gt;&lt;p&gt; Do something like this (pseudocode):&lt;/p&gt;&lt;p&gt; if (GET || POST)&lt;/p&gt;&lt;p&gt;    handle submitted information&lt;/p&gt;&lt;p&gt;    show result page (instead of redirect, show it here directly, saves another request)&lt;/p&gt;&lt;p&gt;else&lt;/p&gt;&lt;p&gt;    show form&lt;/p&gt;&lt;p&gt;endif&lt;/p&gt;&lt;p&gt; It sounds strange, but another request implicates loading the libraries again, loading stylesheets and templates again etc.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description>
 <pubDate>Fri, 14 Mar 2008 18:04:02 -0400</pubDate>
 <dc:creator>Unomi</dc:creator>
 <guid isPermaLink="false">comment 1828 at http://php.dzone.com</guid>
</item>
<item>
 <title>The one I use is pretty much</title>
 <link>http://php.dzone.com/news/php-redirect-function#comment-1645</link>
 <description>&lt;!--paging_filter--&gt;&lt;p&gt;The one I use is pretty much the same but makes sure the URL is absolute (as required by the RFC) and fixes a strange design bug about fragments (I don&#039;t remember what solves exactly but I needed it once).&lt;/p&gt;&lt;p&gt;I still don&#039;t like that &amp;quot;exit&amp;quot; but is more convenient.&lt;/p&gt;&lt;p&gt;BTW, is session_write_close() really needed?&lt;/p&gt;&lt;br /&gt;&lt;pre class=&quot;php&quot;&gt;static function redirect ($url)&lt;br /&gt;{&lt;br /&gt;    $url = Common_Url::create($url)-&amp;gt;toAbsolute();&lt;br /&gt;   &lt;br /&gt;    // Bug fix for http://www.w3.org/Protocols/HTTP/Fragment/draft-bos-http-redirect-00.txt&lt;br /&gt;    if ($url-&amp;gt;equals(Common_Url::getCurrent()-&amp;gt;setFragment(&#039;&#039;))) {&lt;br /&gt;        $url-&amp;gt;setFragment(&#039;redirect-fragment-bugfix&#039;);&lt;br /&gt;    }&lt;br /&gt;           &lt;br /&gt;    header(&#039;Location: &#039; . $url);&lt;br /&gt;    exit;&lt;br /&gt;}&lt;/pre&gt;</description>
 <pubDate>Wed, 05 Mar 2008 18:47:54 -0500</pubDate>
 <dc:creator>frosas</dc:creator>
 <guid isPermaLink="false">comment 1645 at http://php.dzone.com</guid>
</item>
<item>
 <title>Filter the input directly in</title>
 <link>http://php.dzone.com/news/php-redirect-function#comment-1622</link>
 <description>&lt;!--paging_filter--&gt;Filter the input directly in this function so you can send anything without worrying and you&#039;re good.</description>
 <pubDate>Tue, 04 Mar 2008 21:16:50 -0500</pubDate>
 <dc:creator>lhoguin</dc:creator>
 <guid isPermaLink="false">comment 1622 at http://php.dzone.com</guid>
</item>
<item>
 <title>Thank you for the tip Amir!</title>
 <link>http://php.dzone.com/news/php-redirect-function#comment-1575</link>
 <description>&lt;!--paging_filter--&gt;Thank you for the tip Amir!</description>
 <pubDate>Mon, 03 Mar 2008 09:00:32 -0500</pubDate>
 <dc:creator>davidwalsh</dc:creator>
 <guid isPermaLink="false">comment 1575 at http://php.dzone.com</guid>
</item>
</channel>
</rss>
