What if I need to use a randomly generated password in multiple locations within PHP? I have two instances of $password with in PHP. One used as part of an attachment name that will get emailed and the other inserted inline of HTML that is with in my PHP. This results in a totally different password being generated. Is there a way I can ’save’ the initially generated password into another variable that I may use throughout? Total novice here…please enlighten me. Thanks!
<?php
$pdfAttachment = file_get_contents('php://input');
$fp = fopen( 'app_complete.pdf', 'wb' );
fwrite( $fp, $pdfAttachment );
fclose( $fp );
$password = rand(10000, 99999);
$pdfname = $password.'.pdf';
$to = 'xxxxx@xxxxx.com';
$subject = 'Test email with attachment';
$random_hash = md5(date('r', time()));
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
$attachment = chunk_split(base64_encode($pdfAttachment));
ob_start();
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello World!!!
This is simple text email message.
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/pdf; name="<?php echo $pdfname; ?>"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--
2nd use of $password in mail reply
<?php
$message = ob_get_clean();
$mail_sent = @mail( $to, $subject, $message, $headers );
echo $mail_sent ? "<body style='background-color: #F1DFB7; font-family:Arial,
Helvetica, sans-serif; color:#760813;'>
<div align='center'>Thank you for your submition. <br />
<br />
Please write down your application ticket number for future reference:
<br />
<br />
<p style='font-size: 24px; color:#968a2a; font-weight:bold;'>$password</p>
<br />
<p style='font-size: 10px; color:#968a2a; font-weight:bold;'><a href='javascript: history.go(-2)'>BACK TO PREVIOUS PAGE</a></p>
</div>
</body>" : "Mail failed";
?>