无法在嵌入式脚本中设置cookie

I am fairly new to PHP and am trying to embed a php script into a frame (Web Presence Builder / Parallels) which sets 2 cookies for a webpage. I am using the following code to set the cookies.

$hour = time() + 3600; 
setcookie("Cookie1", $_POST['username'], $hour, '.mydomain.com'); 
setcookie("Cookie2", $_POST['pass'], $hour, $hour, '.mydomain.com');

The cookies are not set, however, when I run the same script on a blank page (same server, no html or css), the cookies are set. I have read the documentation where it states

Like other headers, cookies must be sent before any output from your script...

Is there a way to insert the script in order for the cookies to be set?

Thanks.

Put

<?php
$hour = time() + 3600; 
setcookie("Cookie1", $_POST['username'], $hour, '.mydomain.com'); 
setcookie("Cookie2", $_POST['pass'], $hour, $hour, '.mydomain.com');

at the very top of your file. Putting that right at the top won't affect where any text inputs on your page are, as that code isn't outputting any input fields.

On a more general note, I think it is always good practice to lay out you Dynamic HTML (DHTML) scripts with, as far as is possible, the bulk of your PHP at the top of your script. This helps with maintaining awareness of the server nature of PHP. (i.e. that all PHP is executed on the server before the resulting output is passed to the browser - what you then see in a view source). Only spread PHP through your code where it is required contextually. (e.g. to conditionally display different content, to populate the options of selects or echo default values of fields). In a classic setup where the current page contains a form that posts data to the same page for validation - put all the PHP validation and processing code at the top of your script. You should avoid any PHP calculations and/or assignments through the rest of the code (only tests and/or echos).