Jump to content

Recommended Posts

I have a working set of scripts that allows a non-technical client to maintain a mailing list. The scripts use php sessions to store things like $subject, $message, etc. That works.

 

I now need to sleep my script to satisfy the server number-of-emails-per-time requirements, so want to fork off the actually mail sending to a background script using either exec() or shell_exec() so the client won't have to wonder why the page isn't loading. This too works, however, the script I forked off doesn't seem to be able to access my session variables.

 

I tried passing the session id as part of the filename, but that breaks things.

 

Can I use php sessions to pass these variables? Or do I need to stuff them into a db table in the calling script and pull them out in the forked script?

Or do I need to stuff them into a db table in the calling script and pull them out in the forked script?

 

You should be able to pass them in as arguments to the script. eg;

 

#!/usr/bin/env php
<?php

  $_SESSION['subject'] = $argv[1];
  $_SESSION['message'] = $argv[2];


  // rest of your script.

?>

 

Then simply call the script using something like....

 

exec("myscript.php {$_SESSION['subject']} {$_SESSION['message']}");

Oh, that was so close to working, however, the white space in my strings were splitting the string into separate arguments. Still, you pointed me in the right direction.

 

I searched and found shell_quote which isn't PHP, but I then found escapeshellarg(). Perfect! This is what I ended up with:

 

$pass_subject = escapeshellarg($_SESSION['subject']);
$pass_message = escapeshellarg($_SESSION['message']);
$pass_footer = escapeshellarg($_SESSION['footer']);

$command = "/usr/bin/php /home/[mydir]/send-mail-fork.php {$pass_subject} {$pass_message} {$pass_footer} 2> /dev/null &";
$status = exec($command);

Thanks for your help.

Oops, the final redirection I showed in my example above actually didn't allow background operation.

This works: "> /dev/null 2>&1 &"

This didn't: "> /dev/null &"

 

You may have to test what works with your server.

 

For clarity the above line should have been:

$command = "/usr/bin/php /home/[mydir]/send-mail-fork.php {$pass_subject} {$pass_message} {$pass_footer} > /dev/null 2>&1 &";

 

Hope this helps someone else.

a tip for those reading this thread. if the amount of data you pass via arguments gets long, or is dynamic, this is the method i sometimes use:

 

Parent Script

-Put all data into an array

-Serialize the array

-Store serialized array into environmental variable with putenv()

 

Child Script

-Read value in environmental variable with getenv()

-Unserialize

-Use it!

 

this will also keep the arguments from being stored in any shell history files (in case there is any sensitive information)

A tip from another forum gave me the idea of passing the Session ID via arguments (I'd previously failed passing the ID via URL.) By passing the ID, you avoid all the problems with complex variables/arguments.

 

I tried variations of using SID without success and finally stumbled upon this technique.

For the parent script:

$pass_id = escapeshellarg(session_id());
$command = "/usr/bin/php /home/mydir/send-mail-fork.php {$pass_id} > /dev/null 2>&1 &";
exec($command);

For the child script:

<?php 
session_id($argv[1]);
session_start();

Hope this helps someone else trying to use Sessions in a background script.

 

  • 1 month later...

I'm having an issue with getting this method to work and was hoping someone could help me out.

 

I get no errors, however the page loads forever and never completes the task.

 

Here is my code (I run on a Windows server):

 

<?php session_start();
$pass_id = escapeshellarg(session_id());
  require 'includes/configs.inc.php';
  require 'includes/mconfig.php';
  $status = 0;
  
  for($i=0;$i<count($mrr_1);$i++)
  { 
  $mrr=strtolower($mcon[$mrr_1[$i]][1]);
  $mrr_r=$mrr.'remote';
  $mrr_p=$mcon[$mrr_1[$i]][2];
  
  
  	if ((isset ($_POST_DATA[$mrr]) OR isset ($_POST[$mrr_r])))
    {


    exec ('' . $php_path . ' -c ' . $config_ini . ' ' . $mrr_p .' ' . $pass_id . ' ' . $file_uid . ' ' . $status . ' >C:\1.txt 2>&1 &');
    }
  
  }

?>

  • 8 years later...

Oops, the final redirection I showed in my example above actually didn't allow background operation.

This works: "> /dev/null 2>&1 &"

This didn't: "> /dev/null &"

 

You may have to test what works with your server.

 

For clarity the above line should have been:

$command = "/usr/bin/php /home/[mydir]/send-mail-fork.php {$pass_subject} {$pass_message} {$pass_footer} > /dev/null 2>&1 &";
Hope this helps someone else.

 

 

Can you tell me how will you to access this variable in send-mail-fork.php?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.