Log in

View Full Version : Why are global variables needed in this code?



kuau
05-20-2009, 03:28 AM
Can someone please tell me how this code works and if it is the best way to replace variables? It seems to me that the guy just wants to use the variable values in some email messages. Why not use $_POST variables? I know he wrote if for php4 and I thought global variables were frowned upon in php5 (?).

I also don't get the use of $body_text without the dot -- doesn't it just keep replacing the value rather than appending? What is the point of it? Don't you just end up with $body_text equal to the last value? The emails seem to work OK but I'm not sure if this code has anything to do with it. $body_text is not mentioned anywhere in the email.


function replace_email_template_variables($body_text) {
// This function will replace the variables in the Body text for the email... uses several global variables.
global $Today;
global $First_Name;
global $Last_Name;
global $Phone;
global $Email;
global $DriverLicenseFrom;
global $Age_Group;
global $Flight;
global $Cruise;
global $Credit_Card;
global $Best_Price_Net;
global $Best_Price_Full;
global $New_Pick_Date;
global $N_Pick_Time;
global $Pick_Location;
global $Pick_AP;
global $New_Drop_Date;
global $N_Drop_Time;
global $Drop_Location;
global $Drop_AP;
global $Car_Class;
global $Days_Num;
global $country;
global $agencyname;
global $employee;

$body_text = eregi_replace('%Today%', $Today, $body_text);
$body_text = eregi_replace('%First_Name%', $First_Name, $body_text);
$body_text = eregi_replace('%Last_Name%', $Last_Name, $body_text);
$body_text = eregi_replace('%Phone%', $Phone, $body_text);
$body_text = eregi_replace('%Email%', $Email, $body_text);
$body_text = eregi_replace('%DriverLicenseFrom%', $DriverLicenseFrom, $body_text);
$body_text = eregi_replace('%Age_Group%', $Age_Group, $body_text);
$body_text = eregi_replace('%Flight%', $Flight, $body_text);
$body_text = eregi_replace('%Cruise%', $Cruise, $body_text);
$body_text = eregi_replace('%Credit_Card%', $Credit_Card, $body_text);

$body_text = eregi_replace('%Best_Price_Net%', $Best_Price_Net, $body_text);
$body_text = eregi_replace('%Best_Price_Full%', $Best_Price_Full, $body_text);

$body_text = eregi_replace('%New_Pick_Date%', $New_Pick_Date, $body_text);
$body_text = eregi_replace('%N_Pick_Time%', $N_Pick_Time, $body_text);
$body_text = eregi_replace('%Pick_Location%', $Pick_Location, $body_text);
$body_text = eregi_replace('%Pick_AP%', $Pick_AP, $body_text);

$body_text = eregi_replace('%New_Drop_Date%', $New_Drop_Date, $body_text);
$body_text = eregi_replace('%N_Drop_Time%', $N_Drop_Time, $body_text);
$body_text = eregi_replace('%Drop_Location%', $Drop_Location, $body_text);
$body_text = eregi_replace('%Drop_AP%', $Drop_AP, $body_text);

$body_text = eregi_replace('%Car_Class%', $Car_Class, $body_text);
$body_text = eregi_replace('%Days_Num%', $Days_Num, $body_text);
$body_text = eregi_replace('%country%', $country, $body_text);
$body_text = eregi_replace('%Employee%', $employee, $body_text);

return ($body_text);
}


Further down there is this:


$body = replace_email_template_variables($body);

but no mention of $body except in some commented-out text. I just don't get it. Thanks for any light you can shed on this. e :)

forum_amnesiac
05-20-2009, 10:35 AM
My interpretation of this is that the original string, $body, doesn't consist of PHP variables, ie not prefixed by '$'.

There must be a list of variables in the string that he is looking for and replacing them by PHP compliant names.

As to $body_text, this is just a name that is used internally in the function for $body.

The line that calls the function passes $body to the function, the function receives $body into the variable $body_text and it is returned as $body.

You would not expect to see $body_text anywhere else in the code than in the function.

Hope this gives some explanation to you.

kuau
05-20-2009, 11:41 AM
Thanks, but that was as clear as mud, as they say. Why the global variables? Are these necessary? He is taking values from a form the customer fills in to get a price quote and then sending them an email with the response. For example, he gets $First_Name from the form and then places it in the email as %First_Name%. Isn't it more secure to use $_POST['First_Name'] ? Why is a function even necessary?

In php.ini register_globals is On. I thought this was no longer acceptable. There must be a better way to do this. Thanks.

forum_amnesiac
05-20-2009, 12:09 PM
I totally agree with you about $_POST being a better way, personally I would rewrite to get rid of the function altogether.

If you keep the function, then the variables don't need to be global, they are only being used in the function.

I was not trying to give approval to the function, just trying to offer an explanation of what it is doing and what $body_text is being used for.

kuau
05-20-2009, 03:45 PM
OK, now that is what I am asking. If the code above is not the best way to accomplish the goal, then what is? Do I just remove that whole block of code and replace the %xxx% variables in the emails with $_POST['xxx'] variables? The emails are separate files called like this, where $Confirm is the filename:


@$fp = fopen($Confirm,r);

while(!feof($fp)) {
$buffer = fgets($fp,100);
$body.= $buffer;
}

fclose ($fp);

So here is the $body variable again. I assume this reads through the selected external email html file and loads it into the $body variable line by line. Will this still work without the function and using $_POST['xxx'] variables? This is old code I inherited that is working live on the site. I have bit-by-bit rewritten almost all of it -- you helped me last week with another part of it (thank you!!) -- so I am getting down to the few remaining dark corners that I can't quite follow. I really appreciate your assistance. Mahalo plenty! e :)

forum_amnesiac
05-20-2009, 06:27 PM
What this depends on is what is in the HTML form and what "action" it has.

If all the fields are in the one form and the action is post then I would ignore the function altogether and use $_POST variables.

You would need to see what happens subsequently with $body, if it is split into its component parts to create the email then I would just use the $_POST variables for this.

If some reason, eg a PHP HTML mailer that wants the email contents in one long string, then I would create $body from the $_POST variables.

Hope this helps

kuau
05-20-2009, 09:44 PM
Thanks amnesiac... there are 2 forms which both use POST. The first form collects info about the car they want to rent in order to calculate the price, then presents the result on the second page. On the 2nd page, if they want to reserve the car, they submit their name & email address on a second form. The variables from both forms are used in the emails (to the client and the owner) which vary depending on what country they are from. How long do the $_POST variables stay available after you leave the page with the form? Does a new form automatically clear the existing $_POST variables? Maybe that's why he used globals. Is there a newer, more secure way to preserve the values from a form?

I would like to be able to turn Register_globals Off in php.ini if possible because a while ago the hosting company upgraded the php and the new default is Off, so it broke the whole site for a week until I figured out that he had had globals on.

Thanks for helping me understand this! :) e

forum_amnesiac
05-21-2009, 06:49 AM
The variables from the first form will be lost with the $_POST from the second form.

The way to get around this is to define the variables from the first form in the second, eg $form2var1=$_POST($var1), etc.

In the second form you can pass these variables using hidden fields, eg <input type="hidden" name="myform1_var" value=$form2var1>.

Then receive these hidden variables in the PHP email script just like you would any other passed variable.

The code is not exact, but the principle is correct.

kuau
05-21-2009, 08:43 AM
Dear amnesiac: Thank you so much... I'll try that after I get some sleep. When I put the variables that were "hidden" in the email, do I refer to them as $variable or as $_POST['variable']. And not declare them as global, yes?

Mahalo! :) e

forum_amnesiac
05-21-2009, 09:06 AM
kuau

You treat them as $_POST variables.

I was wondering if you were awake late or awake very early, now I know.

Lucky you in the Pacific, I'm envious.