Hmm. its internal mail.. like uhh, I'll show you a simplified down version of the code
//form goes here.
if(isset($submit)) {
//If the submit button was pressed do:
//check if user exists (erased to simplify code)
// set up & check basic ftp connection (erased)
//if the to users mail folder doesnt exist create it now
if (!file_exists("users/$touserfolder/$tousername/mail")) {
mkdir("$home_dir/users/$touserfolder/$tousername/mail");
chmod("$home_dir/users/$touserfolder/$tousername/mail", 0777); }
//check message field isnt empty
$message = $_POST['message'];
if ($message == "") { echo ("could not send mail because message field was empty");
exit; }
if ($message != "") {
//generate a random number
$random_number = rand(100000, 999999);
$extension = ".txt";
//add it to the mails filename
$mailname = $random_number.$to.$extension;
$fp = fopen("$home_dir/users/$touserfolder/$tousername/mail/$mailname", "w") or die ("could not send mail because message could not be created");
chmod("$home_dir/users/$touserfolder/$tousername/mail/$mailname", 0666);
// write the data to the file
fwrite($fp, $message) or die ("could not send message");
//now add the data to the database
$date = gmdate("Y-m-d");
$insert = "INSERT into mail VALUES ('NULL', '$to', '$mysite_username', '$date', '0', '$subject', '$mailname')";
$insert_result = mysql_query($insert) or die(mysql_error());
}
// close the connection
ftp_close($conn_id);
}
}
Ok now this works but problem is if someone refreshes the page, the form thinks its trying to resend the data and you get the same message two or three or fifty million times depending on how inpatient our adorable little refresher is. I need for it not to do that. I cant really check for messages with the same name, as Im using a random number as part of the name in order to give each mail message a unique identifier. The only other way I could think of is by inserting the data into the database FIRST and using the id as the unique identifier but if i do that, and a mistake occurs between entering message into the database, and writing the text file with the message on, the sender will recieve a message that contains a error because my inbox works on retrieving rows from the database. Is there a neater way to do this that Ive not thought of, where I can still write the text file FIRST?