Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need help understanding PHP Include... 1

Status
Not open for further replies.

hneal98

Programmer
Aug 13, 2002
1,637
US
Hi,

I am just learning PHP and I have an issue with a site where an include for a file that once showed up at the end of a transaction no longer shows up.

What does not make sense to me is there is a piece of code that checks to see if an email went out and if so, it is supposed to "include" another file that basically draws a Thank You page.

However, the code to check for the email going out seems to be before the email goes out. So I am confused on the order of execution.

Is it possible to check for an even happening before it happens in PHP?

Here is an example:

if ((isset($_SESSION['ordersent']))&&($_SESSION['ordersent']=="Sent")) {
echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head></head><body>';
echo "<center><br /><br /><br /><br /><br /><br /><div style='width: 500px; font-size: 26px; font-weight: bold;'>";
echo "Your order has been sent. You may now close this browser window.</div>";
echo "</center></body></html>";
} else {
$v=true;
$v=mail($to, $subject, $HTMLmsg, $headers);
$g=mail("myemail@email.com",$subject,$HTMLmsg2,$headers2);
if($v) {
$_SESSION['ordersent']="Sent";
$ErrorMsg="";
//show confirmation page
//This is the part that does not seem to happen...
include("email.php");
if (isset($_GET['clear'])) {
$shield=$_GET['clear'];
if ($shield==$_SESSION['shield']) {
foreach($_SESSION as $key => $value) {
unset($_SESSION[$key]);
}
}
$_SESSION['cart_empty']=1;
$cart=array("desc" => "FootLog","qty" => "0", "price" => "19.95");
$_SESSION["Item1"]=$cart;
$cart=array("desc" => "BodyLog","qty" => "0", "price" => "19.95");
$_SESSION["Item2"]=$cart;
$cart=array("desc" => "FootMag","qty" => "0", "price" => "15.95");
$_SESSION["Item3"]=$cart;
$cart=array("desc" => "Foot Cream","qty" => "0", "price" => "29.95");
$_SESSION["Item4"]=$cart;
$cart=array("desc" => "ToesEase®","qty" => "0", "price" => "19.95");
$_SESSION["Item5"]=$cart;
$cart=array("desc" => "ToeSoak®","qty" => "0", "price" => "19.95");
$_SESSION["Item6"]=$cart;
$shield=md5(time());
$_SESSION['shield']=$shield;
}

}
else
{
$ErrorMsg="Email not sent, there was a problem";
}
}
}
else
{

$Item1=$_SESSION[Item1];
$Item2=$_SESSION[Item2];
$Item3=$_SESSION[Item3];
$Item4=$_SESSION[Item4];
$Item5=$_SESSION[Item5];
$Item6=$_SESSION[Item6];


I have done some programming in VB, but I am having difficulty in following the logic of this code. For example, in my mind, the $v variable, should 1) be set before the else, not after; and 2) should not receive the value of the email information before the if($v) statement. Am I missing something?

Any help would be appreciated
 
Lets step through the code:

Code:
if ((isset($_SESSION['ordersent']))&&($_SESSION['ordersent']=="Sent")) {
        echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head></head><body>';
        echo "<center><br /><br /><br /><br /><br /><br /><div style='width: 500px; font-size: 26px; font-weight: bold;'>";
        echo "Your order has been sent.  You may now close this browser window.</div>";
        echo "</center></body></html>";
    }

The first IF statement checks to see whether the session variable order sent exists, and if its value is set to "Sent"
In which case it presents the user with the Order Has been sent, and close the page message" And Does not send the email again.
Any time the page opens it will check for that, to see if an email has already been sent. Probably done to avoid repeat emails if you refresh the web page, or access it again.


Code:
 else { 
        $v=true;
        $v=mail($to, $subject, $HTMLmsg, $headers);
        $g=mail("myemail@email.com",$subject,$HTMLmsg2,$headers2);
If it hasn't already been sent, then it sets up to send the email.
It defines the variable $v that will hold the result of the mail function. It will remain true as long as the mail function can successfully hand the email off to the server to be sent. Whether it then gets sent or not it doesn't know. If for whatever reason it can't hand over the email to be sent, then it will be false.

Code:
     if($v) {
          $_SESSION['ordersent']="Sent";
        $ErrorMsg="";
       //show confirmation page
//This is the part that does not seem to happen...
        include("email.php");
        if (isset($_GET['clear'])) {
                $shield=$_GET['clear'];
                  if ($shield==$_SESSION['shield']) {
                       foreach($_SESSION as $key => $value) {
                             unset($_SESSION[$key]);
                   }
               }


Once the email is sent, then it checks what the value of $v is. if its true the mail got handed over to the email server to be sent.
So it goes and sets the session variable 'ordersent' so if the page is opened again or refreshed in the future it can check it to see if the order has already been sent.

Then it includes the mail.php. What that does I don't know.

If the include is not happening, its probably because $v is not true. You can add an echo statement to see whether the code inside the if statement is getting run.

Also if you refresh the page and you don't get the "Your order has been sent. You may now close this browser window." message after having sent one message, it can safely be assumed that the email is not going through.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Thank you for your help. I can see I have along way to go. The email.php just creates a Thank You page for the order and displays it to the client so they can print it out.

How does the System variable get set? I did a search in the code and could not find that variable being used anywhere else. Could it have been set in another file with a different name or something?

Thanks again.
 
What system variable?

If you are referring to the SESSION variable 'ordersent' its set right after the email() function is run.

Code:
if($v) {
[red]          $_SESSION['ordersent']="Sent";[/red]

$_SESSION is a global PHP variable that remains alive as long as the session exists, that is as long as the browser remains open. Once the browser closes the session expires.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Perfect. That explains what I needed. Thank you so much for taking the time to explain this to me. If I could give you more than one star, I would give you 10.

Thanks again.
 
I do have one more question. I tried to set the variable as you stated, but when I ran the code again, I just got a blank web page. I know that means I did something wrong, but I can't tell what.

I know you don't know my code, but if you have any insight, I would appreciate it.

Thanks again.
 
Not sure what you tried to do, as that variable was already being set in your code.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
As Is started to respond to your post, I had a thought about something I did where I changed the order of the one of the '}'. It turns out that was the problem. Just one little thing can bring the mess tumbling down.

Thanks again,

Harvard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top