Jump to content
Bosque Bill

using PHP sessions with exec() or shell_exec()

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?

Share this post


Link to post
Share on other sites
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']}");

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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)

Share this post


Link to post
Share on other sites

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.

 

Share this post


Link to post
Share on other sites

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 &');
    }
  
  }

?>

Share this post


Link to post
Share on other sites

what pc you got, maybe it comes down to the processing speed or ram.

Share this post


Link to post
Share on other sites

try a different loop then can help.

 

i am still running towards processing power it a processing problem.

 

it works fine fast until it hits the loop that right.

Share this post


Link to post
Share on other sites

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

 

you can't send processes to the background with & on windows

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

Don't hijack other people's threads, especially when they are nine years old.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.