To send yourself an email, I’d create an ‘include’ script that assigns it to a var and then add it as a prepend to all my scripts. You could also use a db to store the email and use that include script to read it from there, but a simple assignment of a string is the simplest. One change when necessary and all your scripts requiring it will have it.
Of course you don’t have to use the prepend feature - simply add the include line to your php template (if you have one) and it will be there with every new script, and you can drop it if you don’t need it for that script.
A ‘prepend script’ is something you can set in your .ini file. It points to a piece of code (like something you would include) that will go in front of every script you run. As I said it’s an option, but you can just as easily have an include statement pointing to a small piece of code in your standard template that you use to begin every new script.
Contant/ variable that’s set - no difference. You have to set the constant every time as well.
Something like:
<?
// This is /inc/myemailaddr.php
$MyEmailAddr = 'johndoe@yahoo.com';
and then assuming you have some kind of template that starts like :
If you have a block of code that you are including in your scripts that send emails to you, well, of course you would put the address in there, either as a var or a constant. This is not much different from what I was saying above. You just didn’t mention that you had an email module so I skipped that.
The point here is that you can’t define a constant in one place unless it is in a database. You keep asking about a Constant, but you have to realize it is nothing more than a variable that gets assigned but is readonly. It still has to be defined at some point in every script.
If you have a template that is your starting point for developing new scripts, I’d recommend doing it that way. Once you’ve set it up, you’ll eventually forget all about it.
So on a related note, cpradio, how could I make the following code more efficient…
// Log Error in Database. *
// Build Query.
// Execute query.
// Verify Insert.
if (mysqli_stmt_affected_rows($stmt1)==1){
// Error Logged in DB.
// Continue processing...
}else{
// Error Not Logged in DB.
// Email Admin about Failed Insert.
// LOOK
$emailTo = 'Debbie@mail.com';
$headers = "From: Admin <admin@MySite.com>\\r\
";
$headers .= "MIME-Version: 1.0\\r\
";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\\r\
";
$subject_FailedInsert = 'Re: Unloggable Website Error (' . $currTime . ')';
$body_FailedInsert = "<p>" . $currTime ."</p>\\r\
";
$body_FailedInsert .= "<p>Dear Admin,</p>\\r\
";
$body_FailedInsert .= "<p>A website error has occurred, but could not be logged in the database.</p>\\r\
";
$body_FailedInsert .= "<p>Please take immediate action!!</p>\\r\
";
mail($emailTo, $subject_FailedInsert, $body_FailedInsert, $headers);
// Continue processing...
}// End of LOG ERROR IN DATABASE
That is a lot of code, but if you look closer, most of it is just me defining the E-mail From, Subject, and Body.
I can’t think of a way to make it more efficient, because even if I made that a Class or Function, I would still need to have the verbose arguments to my mail function, right?? :-/
Well, I can only think of a small few ways, but it is based on whether all of the different email scenarios you have can follow a template(s). Think of the following:
error.template
<p>{current_time}</p>
<p>Dear Admin,</p>
<p>A website error has occurred, but could not be logged in the database.</p>
<p>Please take immediate action!!</p>
<p>Error occurred in {function_name} trying to do {action}</p>
Like superglobals, the scope of a constant is global. You can access constants anywhere in your script without regard to scope. For more information on scope, read the manual section on variable scope.