0

I am sending email in background process. For background process I am using symfony 2.0. Background works successfully. but in background I can't get session value.. Below code of index.php

session_start();
$_SESSION['album_upload'] = {"a","b"};
$command = file_get_contents('background.php');
$process = new PhpProcess($command);
$process->run();

below code of background.php

session_start();
$message = $_SESSION['album_upload'][0];
mail("[email protected]","example",$message,"From: $from\n"); // Session doesn't give any value. 

If I remove session and just try to send mail like mail("[email protected]","example","example","From: $from\n"); then mail will send.

How to get the session value in background.php.

2
  • symfony.com/doc/current/components/http_foundation/… Commented Sep 11, 2014 at 10:45
  • 2
    Session data is retrived based on the sessio id cookie the browser sends with the request. A background task isnt initiated by a browser, so no cookie, so no session data Commented Sep 11, 2014 at 10:45

2 Answers 2

2

Try to retrieve the session id from you're first script and pass it to the next one. You can do it with the session_id() func

Code Index.php

$session_id = session_id();
new PhpProcess($command, null, array('session_id' => $session_id));
$process->start();
while ($process->isRunning()) {// waiting for process to finish }

Code background.php

session_id($_SERVER['session_id']);
session_start();
$message = $_SESSION['album_upload'][0];
mail("[email protected]","example",$message,"From: $from\n"); // Session doesn't give any value. 
Sign up to request clarification or add additional context in comments.

3 Comments

@issac I never used session_id() before. But how to set in index.php and get it in background.php so I can get my ` $_SESSION['album_upload']` data
@ Issac Your code working now. I don't know but $process->run(); waits for the execution and after run it will execute rest of code.?? I want to execute in background. It should not be wait for execution. It is possible???
@user3730347 put $process-start() to execute in background, then add this at the end of you're code while ($process->isRunning()) { // waiting for process to finish } to be sure that your email got the necessary time to be sent.
2

PhpProcess runs as a seperate php process; this means it does not have access to the $_SESSION information that was available to the original request to your server.

You can pass the data as follows:

new PhpProcess($command, null, array('album_upload' => $_SESSION['album_upload'][0]));

That should allow you to acccess the data in your background script as the variable:

$_SERVER['album_upload']

7 Comments

when I an using print_r($_SERVER['album_upload']) at background it gives me 1 output. I need to set two-dimensional array
I'd guess you need to double check the structure of your $_SESSION['album_upload'] variable.
Is PhpProcess() doing background process??. means we don't need to do like ini_set('max_execution_time', 500) for execution time???
I don't know but $process->run(); waits for the execution and after run it will execute rest of code.?? I want to execute in background. It should not be wait for execution. It is possible???
$process->start() will run the code in the background without blocking. However, Symfony automatically stops child process when its main thread completes, so your email may not be sent.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.