SitePoint Sponsor

User Tag List

Results 1 to 14 of 14

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Feb 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    passing session variables to another page

    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.

    <?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">Dear <?php echo $_SESSION['username']; ?> ,</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 );




    ?>

  2. #2
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have to pass session id via command line and then set it manually
    But it is most complicated feedback form design I've ever seen in my whole life.

  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What does that emailsend.php file do actually? Why do you need to run that script in the background in the confirmation page. Quite strange! Can we try to avoid the use of exec? If you say the purpose of that, someone can present you a solution so that you don't need to use exec.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  4. #4
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    He's explained that

  5. #5
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If it has a delay built in, then why not just use cron?

  6. #6
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cron will make things even more complicated

  7. #7
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Shrapnel_N5 View Post
    cron will make things even more complicated
    Not to my small mind :P

    Perhaps I'm not getting it, but OP wants to do a bunch of stuff with a delay before sending emails but not delay user page loads? Log it to db and run cron??

  8. #8
    SitePoint Member
    Join Date
    Feb 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help guys. It's not my preference for it to be like this but it's not just a simple feedback form. It has to do a whole bunch of things all at once that normally shouldn't go together...but that's the brief I've been given so I'm going to piece it together roughly and get it working and then simplify it down the track.

  9. #9
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Shrapnel_N5 View Post
    He's explained that
    Actually what I mean is, what are the things that are to be done before/after that delay? If they are manageable with some other way then we all can think of it.

    Quote Originally Posted by hawker2 View Post
    It has to do a whole bunch of things all at once that normally shouldn't go together...but that's the brief I've been given so I'm going to piece it together roughly and get it working and then simplify it down the track.
    So you also don't know now that what are the tasks/things to be done before that delay or with that delay in that script? Are those things to be done at the time of this email sending or they can be done periodically with some schedules as has suggested before? I am quite confused what the delay is for. Did OP mention about it?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  10. #10
    SitePoint Member
    Join Date
    Feb 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, using the command line trick has worked..trial and error produced the following..

    exec('/usr/local/bin/php emailsend.php '.$_SESSION['username'].' >/dev/null &');

    Question: Is it possible to add another variable to that line, say $_SESSION['email'] as well as $_SESSION['username'].

  11. #11
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why to involve database into process, when we need to pass just a few variables to the script, which already runs at background?

    Even sessions not needed here, as we can pass variables itself using command line parameters

  12. #12
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    *shrug* perhaps the script dies before txn complete.

  13. #13
    SitePoint Member
    Join Date
    Feb 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Solved! The answer is exec ('/usr/local/bin/php emailsend.php ' . $_SESSION['username'] .' '. $_SESSION['email'] . ' >/dev/null &');

  14. #14
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i'd suggest to encode variables somehow.
    what if name would consist of 2 words? or even of something like |rm -rf /?
    base64 looks good enough


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •