Forum Moderators: coopster
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.
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);
?>
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");
?>
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.