0

Regarding Is it possible to read cookie/session value while executing PHP5 script through command prompt? I tried to make a bash file that executes php and sets $_COOKIE before that.

I tried first in Windows environment creating simple batch file:

php -r "$_COOKIE['PHPSESSID']='a095y187'; session_start(); $id_client=%1; $id_supplier=%2; $_COOKIE['id_chosen_client_auto']=$id_client; $_COOKIE['id_chosen_client']=$id_client; require 'manual_automap.php';"

Which works as i expected $_COOKIE is set and $id_client, and $id supplier were set with the first and second argument after the bat file:

manual_automap.bat 1 1

Now i try to migrate this on Linux environment and tried to change %1 and %2 to $1 and $2, but it seems this doesn't work like that. The variables are not set:

php -r "$_COOKIE['PHPSESSID']='a095y187'; session_start(); $id_client=$1; $id_supplier=$2; $_COOKIE['id_chosen_client_auto']=$id_client; $_COOKIE['id_chosen_client']=$id_client; require 'manual_automap.php';"

What am i missing here?

I expect when run manual_automap.sh 1 2 $id_client should be 1 and $id_supplier should be 2

Thanks.

PS: The php script i run is very old and can't be changed.

Community
  • 1
  • 1
bksi
  • 1,606
  • 1
  • 23
  • 45

1 Answers1

1

You should escape all $ signs (except $1 and $2).

php -r "\$_COOKIE['PHPSESSID']='a095y187'; session_start(); \$id_client=$1; \$id_supplier=$2; \$_COOKIE['id_chosen_client_auto']=\$id_client; \$_COOKIE['id_chosen_client']=\$id_client; require 'manual_automap.php';"
sectus
  • 15,605
  • 5
  • 55
  • 97