Bosque Bill Posted February 25, 2009 Share Posted February 25, 2009 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? Link to comment Share on other sites More sharing options...
trq Posted February 26, 2009 Share Posted February 26, 2009 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']}"); Link to comment Share on other sites More sharing options...
Bosque Bill Posted February 26, 2009 Author Share Posted February 26, 2009 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. Link to comment Share on other sites More sharing options...
Bosque Bill Posted February 27, 2009 Author Share Posted February 27, 2009 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. Link to comment Share on other sites More sharing options...
rhodesa Posted February 27, 2009 Share Posted February 27, 2009 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) Link to comment Share on other sites More sharing options...
Bosque Bill Posted March 1, 2009 Author Share Posted March 1, 2009 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. Link to comment Share on other sites More sharing options...
aliks0905 Posted April 8, 2009 Share Posted April 8, 2009 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 &'); } } ?> Link to comment Share on other sites More sharing options...
redarrow Posted April 8, 2009 Share Posted April 8, 2009 what pc you got, maybe it comes down to the processing speed or ram. Link to comment Share on other sites More sharing options...
aliks0905 Posted April 8, 2009 Share Posted April 8, 2009 I'm on a 384mb VPS. The exec function works fine when I execute without passing the session Link to comment Share on other sites More sharing options...
redarrow Posted April 8, 2009 Share Posted April 8, 2009 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. Link to comment Share on other sites More sharing options...
rhodesa Posted April 9, 2009 Share Posted April 9, 2009 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 Link to comment Share on other sites More sharing options...
bhautikng143 Posted January 16, 2018 Share Posted January 16, 2018 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? Link to comment Share on other sites More sharing options...
Barand Posted January 16, 2018 Share Posted January 16, 2018 Don't hijack other people's threads, especially when they are nine years old. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.