Page 1 of 1

passing session variables to another page

Posted: Wed Feb 10, 2010 1:23 am
by hawker
I have a registration website that collects name, email, address etc from a form. The php code that processes the form validates the inputs and creates sessions for the variables so I can use them on a confirmation page once the form is submitted.

I have used a shell script .. eg exec ("/usr/local/bin/php emailsend.php >/dev/null &");
on the confirmation page that runs a php file in the background once the confirmation page has loaded in the browser. This php file sends a "thank you for registering" email as well as sending a copy to us. (It runs in the background because it has a delay built in before it sends the emails -if I put the code directly onto the confirmation page then that page would not be able to load fully until the delay was finished) I want to personalise the thank you email with the persons name. However for some reason the session variables that are created by the form submission won't print on the thank you email. It keeps coming up blank where the persons name should be. Is there a reason why session variables that I can access and write to any other page on the site won't show up on a page that hasn't been loaded into a browser but simply runs in the background and sends out emails?

I have posted the code for the emailsend.php page below with the name greeting in red. Can anyone let me know whether I'm coding this wrong in the way I'm trying to access the username session variable. Any help would be appreciated.

Code: Select all

 
<?php
session_start();  
$email = $_REQUEST['access_login'] ;
//define the receiver of the email
$to = "<$email>";
$to2 = "reply@emailaddress.com";
//define the subject of the email
$subject = 'Welcome to our website'; 
//create a boundary string. It must be unique 
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time())); 
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: reply@emailaddress\r\nReply-To: reply@emailaddress.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\""; 
//define the body of the message.
ob_start(); //Turn on output buffering
?>
 
 
--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit
$message = '
<html>
<head>
</head>
<body>
<font face="arial" color="black" size="2">
    <table cellSpacing="0" cellPadding="0" width="605" align="center" border="1">
      <tr>
        <td style="WHITE-SPACE: nowrap" vAlign="top" width="684">
        <div style="MARGIN-BOTTOM: 0px" align="center">
          <a href="http://www.ourwebsite.com" target="_blank">
          <font color="#000000">
          <img alt hspace="0" src="http://www.ourwebsite/image.jpg" border="0" width="664" height="385"></font></a>
        <table cellSpacing="8" cellPadding="8" width="678">
          <tr>
            <td vAlign="top" width="697">
                          <p style="margin-top: 0; margin-bottom: 0" align="left">
              <font color="#000000" size="4" face="Arial Narrow">[color=#FF0040]Dear <?php echo $_SESSION['username']; ?>[/color] ,</font>
                          </p><br>
                          <p align="left">                     
                          <font face="Arial Narrow" size="4"><font color="#000000">Welcome 
              to our website, </font>
              <a 
 
</body>
</html>
 
 
--PHP-alt-<?php echo $random_hash; ?>--
<?
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
$mail_sent = @mail( $to2, $subject, $message, $headers );
 
?>
 

Re: passing session variables to another page

Posted: Wed Feb 10, 2010 3:04 am
by dejvos
So you run a sending script via shell command. So, in that case you could use "linux type" of calling of a script:

Code: Select all

 
exec('/usr/local/bin/php emailsend.php '.$_SESSION['username'].' >/dev/null &');
 
then inside the script you'll get the argument:

Code: Select all

 
<?php
$username = $argv[1];
?>
 
And thats it ... you don't need session global. You can't get the $_SESSION because no request header is delivered. Next, I thing if you use exec() function the PHP is not executed in apache or other environment.

Good luck!

Re: passing session variables to another page

Posted: Wed Feb 10, 2010 4:25 am
by hawker
Thanks for your help dejvos. Much appreciated. :D

Re: passing session variables to another page

Posted: Wed Feb 10, 2010 5:06 am
by hawker
Just another question..if I wanted to pass another variable through, say $_SESSION['email']. How could I add that to the command line as well as $_SESSION['username']

Re: passing session variables to another page

Posted: Wed Feb 10, 2010 7:37 am
by dejvos

Code: Select all

 
exec('./script.php '.$_SESSION['username'].' '.$_SESSION['email'].' > /dev/null &');
 
script.php then includes

Code: Select all

 
$username = $argv[1];
$email = $argv[2];
 
Is that what did you want?

Re: passing session variables to another page

Posted: Wed Feb 10, 2010 1:08 pm
by hawker
Yes, that's it..all works great now. Thanks for your help :D