PHP: $argv variable screws up environmental variables in DB connection

52 Views Asked by At

I have a weird problem. I am "firing" a PHP page (page 2) from within another PHP page (page 1) by using exec, which sends a $argv variable to page 2.

The issue is, the DB connection on page 2 breaks because the $argv variable messes my environment variables for the DB connection ($_ENV['DB_USER'] and $_ENV['DB_PW']). I get an error like the password is incorrect ([1045] Access denied for user...). It only happens if I call page 2 using the exec command on page 1, otherwise the connection works fine.

If I comment out the DB connection, the $argv[1] (ID I need) is passed to page 2 without issues.

It works fine if the DB connection has hardcoded values for user and pass.

I've tested a lot, and am positive $argv is screwing up my environmental variables.

I tried encasing the variable for the on page 1 in escapeshellarg, but no difference.

Does anyone have any ideas why?

By the way, I don't want to use includes or cUrl to run page 2.

Thanks in advance.

EDIT:

PAGE 1:

$doc_id = "123";
$doc_id = escapeshellarg($doc_id);

$path = $_SERVER['DOCUMENT_ROOT'];
exec('php '.$path.'/dashboard/test/argv-2.php '.        
$doc_id .' > /dev/null 2>/dev/null &');

PAGE 2

require_once __DIR__ . '/../../vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createUnsafeImmutable(__DIR__ . '/../../../../'); //root
$dotenv->load();


$tz_sync = (new DateTime('now', new  DateTimeZone('America/New_York')))->format('P');

$db = new     PDO('mysql:host=localhost;dbname=xxxx_main;charset=utf8mb4',             
$_ENV['DB_USER'], $_ENV['DB_PW']);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$db->exec("SET time_zone='$tz_sync';");


$doc_id = $argv[1]; 

//get doc title
$rowDocTitle = getDocTitle($db,$doc_id);
$doc_title = $rowDocTitle['doc_title'];


//email
$tox = '[email protected]';
$subjectx = 'Doc title';
$messagex = "Doc title: " . $doc_title . "\n\n";
$headersx = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
mail($tox, $subjectx, $messagex, $headersx);
0

There are 0 best solutions below