Hi,
I can't seem to think of a good way to write php code to a new php page.... ok, that sounds confusing... here is my code:
<?php
// Location and name of folder to create quotes/bids (no ending /, example: "./some_dir/some_dir":
$q_fldr_loc = './clients';
// Extension of file to create:
$doc_type = '.php';
// Location to file managing script:
$file_mng = './mng_files.php';
error_reporting(E_ALL ^ E_NOTICE); // Turn off error "Notice" reporting.
//error_reporting(0); // Turn off error reporting.
echo "<form action='".$_SERVER['PHP_SELF']."?action=create_quote' method='post' name=''>"."\n";
print <<< PRINT_PAGE
Company: <input name="company" type="text" size="15" maxlength="40">
Quote: $<input name="quote" type="text" size="15" maxlength="40">
<br />
Auth user: <input name="auth_user" type="text" size="15" maxlength="40">
Auth pass: <input name="auth_pass" type="text" size="15" maxlength="40">
<input name="submit" type="submit" value="Submit">
</form>
PRINT_PAGE;
if ($_GET['action'] == 'create_quote') { // Generate quote/bid:
$en_form_num = number_format($quote); // Add commas to the quote number.
// HTML to be written:
$html='<?php ';
$html.='if (!isset($PHP_AUTH_USER)) {';
$html.='header("WWW-Authenticate: Basic realm="'.$company.'"");';
$html.='header("HTTP/1.0 401 Unauthorized");';
$html.='echo "Authorization Required.";';
$html.='exit;';
$html.='} else if (isset($PHP_AUTH_USER)) {';
$html.='if (($PHP_AUTH_USER != "'.$auth_user.'") || ($PHP_AUTH_PW != "'auth_pass.'")) {';
$html.='header("WWW-Authenticate: Basic realm="My Privates"");';
$html.='header("HTTP/1.0 401 Unauthorized");';
$html.='echo "Authorization Required.";';
$html.='exit;';
$html.='} else {';
$html.='echo "You are authorized!<br>";';
$html.='echo "You have entered this username: $PHP_AUTH_USER<br>";';
$html.='echo "You have entered this password: $PHP_AUTH_PW<br>";';
$html.='}';
$html.='}';
$html.='?>';
$file_name = strtolower($company); // Make company name lower-case for making file.
$file_name = htmlspecialchars(stripslashes($file_name)); // Convert to special chars and strip slashes.
$urls = array(' ', '.', '?', '=', '+', ':', '%', '$', ','); // Create array and pop with items to be stripped from var.
$file_name = str_replace($urls,"_",$file_name); // Now remove above array items from company name.
$qfname = $file_name.'_quote';
// Make directory and open file:
if(!is_dir($q_fldr_loc.'/'.$file_name)) {
$oldumask = umask(0);
mkdir($q_fldr_loc.'/'.$file_name, 0777); // or even 01777 so you get the sticky bit set
umask($oldumask);
}
$fp = fopen($q_fldr_loc.'/'.$file_name.'/'.$qfname.$doc_type,'wb'); // Open for writing only; If the file does not exist, attempt to create it.
fwrite($fp,$html);
fclose($fp);
echo "<p>Your $".$en_form_num." quote/bid for ".$company." has been generated, <a href='".$q_fldr_loc."/".$file_name."/".$qfname.$doc_type."'>click here</a> to scope it!</p>"."\n";
echo "Or, <a href='".$file_mng."'>click here</a> to manage/remove directories.";
}
?>
Now, my main problem is trying to write the var $html to a new php file. How do I write a huge chunk of code to a new php file? As you can see above, the first part of code is a form that submits a few vars... I want to be able to produce a new PHP page, that has user authentication with a name and password of my choosing upon submission of form... well, again, I am making things complicated... Um... hopefully you will be able to read my code and see what I am trying to do. I do not want to use a mySQL db for this (mainly because I do not have any exp. with that)
Anyway, any help would be cool! How do I $html to write the PHP to a new PHP file and make it function correctly?
Thanks in advance!
cheers
M