Forum Moderators: coopster

Message Too Old, No Replies

PHP Re-direction

Having trouble re-directing from template to saved page

         

dougmcc1

12:14 am on Jul 5, 2003 (gmt 0)

10+ Year Member



Ok I have 2 files. form.php and template.php.

form.php sends variables to template.php. One of the variables is $page_name, which I input as 'page.php'.

In template.php, it copies itself as temp.php, then copies temp.php to ../$page_name and deletes temp.php. This works. page.php is created in the root directory.

But here is my problem. I want template.php to redirect to the created page, ../$page_name. I tried a few different methods as shown below:

<?
if ($_SERVER['PHP_SELF'] == "/folder/template.php")
{
/* determine if $page_name already exists in the above directory */
/* alert me if this page already exists and ask me whether or not to overwrite it */
copy ("template.php", "temp.php");
copy ("temp.php", "../$page_name");
unlink ("temp.php");
header( 'refresh: 0; url=../$page_name' );
}
?>

Also same code but with

header( 'Location: ../$page_name' );

I also tried echo "<meta refresh to <? echo ../$page_name?>" but then template.php just kept refreshing itself.

Any ideas on why it's not redirecting or how to go about it with a different approach? Thanks in advance.

Timotheos

6:11 am on Jul 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



From the manual [us3.php.net...]


Note: HTTP/1.1 requires an absolute URI as argument to Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself:

<?php
header("Location: [".$_SERVER['HTTP_HOST']...]
.dirname($_SERVER['PHP_SELF'])
."/".$relative_url);
?>

jamesa

10:55 am on Jul 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For the header(Location...) redirect to work, it has to be the first output in the script. So if your script sends any HTML, spaces, newlines, whatever... before the header() function then you'll have a problem.

This would work:

<?
header("Location: newfile.php");
?>

This wouldn't (the difference is the blank line before the opening php tag):


<?
header("Location: newfile.php");
?>

vincevincevince

1:35 pm on Jul 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jamesa, thanks for highlighting an all too common mistake.
Personally, I have always believed that .php should ignore anything outside <?php ...?>, and use .phtml for doing that mix of language affair that some scripters do (eg "<body><h1>Title</h1><?php some stuff;?><table>Some stuff</table>" etc). If anyone knows how to set php to do this ignoring - I'd love to hear it :-)

dougmcc1

4:09 am on Jul 6, 2003 (gmt 0)

10+ Year Member



Well someone else helped me through it, but basically they suggested the same thing as jamesa. So it's working now, but thanks for all your help.

jaski

4:31 am on Jul 6, 2003 (gmt 0)

10+ Year Member



If anyone knows how to set php to do this ignoring - I'd love to hear it :-)

.php is same as .phtml :)

PHP parser sends any thing outside <? ..?> as it is.

That infact is the reason why having an empty line above <? header()?> will cause problems. PHP assumes that line to be "plain html" and sends it out.

Although it only sends "\n" a newline character .. the output has started .. and by the rules of HTTP, headers cannot be sent after the html output starts .. which causes the error.