Forum Moderators: coopster

Message Too Old, No Replies

$argv variable screws up my DB connection

         

defanjos

1:25 am on Apr 13, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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 $argv variables to page 2.

The issue is, the DB connection on page 2 breaks because the $argv variable messes my environmental variables for the DB connection ($_ENV['DB_USER'] and $_ENV['DB_PW']).
I get a 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.

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

Does anyone have any ideas?

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

Thanks in advance.

phranque

9:57 pm on May 15, 2023 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



i would try using the escapeshellarg function to escape the $argv variable before you pass it to the exec command, which will escape the special characters in the $argv variable and prevent them from being interpreted as query parameters.

defanjos

11:13 pm on May 15, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for your reply, unfortunately that did not work.

TessCompton

9:54 am on Sep 14, 2023 (gmt 0)



I get your frustration. Exec might be affecting your environment. To isolate the issue, try setting your environmental variables explicitly on page 2 before the DB connection:

php
putenv("DB_USER=your_db_user");
putenv("DB_PW=your_db_password");

This should ensure the correct DB credentials are used even when calling page 2 with exec from page 1.