Page 1 of 1
Need one script to access variable in another script
Posted: Fri Aug 08, 2008 12:13 pm
by hokeyplyr48
I have a registration page called main.php the user fills out all their information, it checks it, adds it to a SQL database, etc. when they click the registration button, and all the conditions have been met (passwords are the same, data successfully added to the SQL database, etc.) it sends an email using the mail() function. This works great.
The message of this email is in another script called confidentiality_agreement.php and i have the entire email message stored in a variable called $confidential. in the top of main.php i have require(confidentiality_agreement.php) and later in the mail function have $mail = $confidential; which is working great as well, it sends the email and everything is formatted correct.
My question is, in this confidentiality agreement in confidentiality_agreement.php i wand to insert the name that the user inserted in main.php during registration. The page technically never changer/the user never actually goes anywhere so i don't think GET or REQUEST will work in this case. I just want confidentiality_agreement.php to pull the $name variable from main.php and insert it in the text of this email (confidentiality_agreement.php)
i'm trying to explain this as best as i can, hope you all can understand it. if you need more information/clarification please ask.
thanks in advance!
Re: Need one script to access variable in another script
Posted: Fri Aug 08, 2008 1:37 pm
by nowaydown1
Hi hokey,
Welcome to the forum! If I've understood your question correctly, you shouldn't need to do anything special to reference your $name variable. The require_once function is actually taking the contents of your confidentiality_agreement.php and bringing it into the same scope as your main.php file. Essentially, its like main.php actually contains confidentiality_agreement.php.
Take the following example. Let's say I have two files below:
main.php
Code: Select all
<?php
/* We'll pretend some registration type stuff happens here. */
$username = "MyNewUser";
require_once("say_hello.php");
?>
say_hello.php
So essentially, your script winds up looking something like this to the php interpreter:
Code: Select all
<?php
/* We'll pretend some registration type stuff happens here. */
$username = "MyNewUser";
echo "Hello $username";
?>
Now if you're trying to use that variable inside a function that exists in confidentiality_agreement.php, then you need to either pass the variable to your function (preferred), or bring that variable into the local function scope by using 'global $name' (which I wouldn't recommend).
If you have any additional questions, or I missed the mark on what you were trying to accomplish, feel free to ask away!

Re: Need one script to access variable in another script
Posted: Mon Aug 11, 2008 12:04 pm
by hokeyplyr48
thank you for the quick reply!
Let me kind of restate this.
confidentiality_agreement.php contains variable $confidential.
$confidential contains regular html data to display the agreement sent out in the email. it looks like so: (i've cut it short to save you the boredom and cluttering everything)
Code: Select all
<?php
$space = " ";
$confidential2 = "<html>
<body>
<center><h2><u>CONFIDENTIALITY AGREEMENT </u></h2></center>
<br \>
$space THIS CONFIDENTIALITY AGREEMENT, is dated this " . $date . " day of " . date(F) . ", 2008, by and between SELLER, a Limited Liability Company (\"SELLER\"), and $name below.
<br \>
<br \>
$space WHEREAS, SELLER and Recipient may enter into discussions concerning a possible transaction between SELLER and Recipient (the \"Transaction\").
<br \>
<br \>
$space WHEREAS, in contemplation of entering into the Transaction, SELLER and/or its agent Sperry Van Ness (\"Sperry\") will provide Recipient with property information, financial information and operating statements, leases, tenant information and financial reports and other proprietary and confidential information relating to the operation of the PROPERTY, and
<br \>
<br \>
$space WHEREAS, SELLER and Recipient desire to maintain the confidentiality of the confidential information and authorize the use of same only for the purposes contemplated by this Confidentiality Agreement, and
<br \>
<br \>
$space WHEREAS, SELLER does not want Recipient to contact any Employee of, or Tenant in the Property without SELLER’s prior written consent.
<br \>
<br \>
$space NOW, THEREFORE, the parties agree as follows:
<br>
<br>
$space <b>1.</b> As used herein, \"Confidential Information\" shall include all products, information and data furnished or made available by SELLER or Sperry to Recipient at any time prior to the etc.
</body>
</html>";
?>
then there's register.php which was previously main.php (changed the name as requested by supervisor) it calls at the beginning require("confidentiality_agreement.php") and then the mail function calls the variable $confidential2 from it for the $message in the mail.
this part looks like:
Code: Select all
$from = server;
$subject1 = "TEST EMAIL";
$subject2 = "Recently Signed Confidentiality Agreement";
$message = $confidential2;
if((mysql_query($qry2)) && (mail("(my email address, *edited*",$subject1,$message, "From: $from\nContent-Type: text/html; charset=iso-8859-1")) && (mail("$email",$subject2,$message, "From: $from\nContent-Type: text/html; charset=iso-8859-1")))
it works great and sends the email. But it won't insert the $name variable from register.php into $confidential when I send the email. My supervisor wants it to be personalized but I can't for the life of me get it to work. Hope that explains it!
Re: Need one script to access variable in another script
Posted: Mon Aug 11, 2008 9:07 pm
by nowaydown1
Ah gotcha. It sounds like what you have is a chicken and egg problem. Your $confidential2 variable is set long before you ever know what the $name variable is set to. The simplest solution (although perhaps not the cleanest) is to move your require_once statement to directly above the line you provided when your mail() function calls happens.
Some other possible solutions would be:
* Just do-away with the external include file, and declare the message in-line (again with the message body variable being set directly before the mail calls happens).
* Invent a token / variable replacement system that would be replace your email tokens in realtime, right before your email sends.
So here's why the problem occurs. When you're require_once happens, the variable you defined that holds email to be sent gets evaluated. String interpolation (fancy word for take the value of any varaibles and replace them with their actual values) also happens at this time. After the evaluation, your string is stored in memory.
At the time you do your require_once, no variable named $name exists (or its empty). So, when the string evaluation happens it has no value, and goes along on its merry way. The solutions I've proposed above just change the time when that evaluation occurs. Sometime after $name exists (hopefully

).
Sorry for the long-winded explanation. Hope that helps.