Good day, there is currently such a php code for authorization:

<?php header('Content-Type: text/html; charset=utf-8'); setlocale(LC_TIME, "ru_RU.utf8"); $login_params = explode('=', file_get_contents('php://input')); auth(); function auth() { $auth_curl = curl_init(); curl_setopt_array($auth_curl, array( CURLOPT_URL => 'http://uapi.ucoz.com/index/sub/', CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => true, CURLOPT_HTTPHEADER, array( 'Host: uapi.ucoz.com' ), CURLOPT_POST => true, CURLOPT_POSTFIELDS => http_build_query(array( 'user' => $login_params[0], 'password' => $login_params[1], 'a' => '2', 'ajax' => '2', 'api_request' => '1', 'api_login_rnd' => get_rand_str(), 'api_login_crc' => get_rand_str(), '_tp_' => 'xml' )) )); $response = curl_exec($auth_curl); curl_close($auth_curl); echo $response; } function get_rand_str() { $length = rand(8, 10) - 1; $num = rand(0,9); for($i = 0; $i < $length; $i++) { $num .= rand(0, 9); } return $num; } ?> 

A логин;пароль expected at the entrance, but the server always responds that the login or password is not correct. If you enter a specific login value instead of $login_params[0] , and instead of $login_params[1] specific password value, with the same ones that are sent via file_get_contents('php://input') , then the authorization is successful. As a result, the values ​​are the same, but if you use the elements of the array, instead of the specific embedded values, the authorization will not be successful. What could be the problem?

  • 2
    The problem is that in the auth function, the value of login_params not available. - u_mulder
  • @u_mulder, global'a not enough? - PhoEn-X
  • @ PhoEn-X: why global? pass this array as a parameter to the auth function auth($params) {....} auth($login_params); : function auth($params) {....} auth($login_params); - Boris
  • @Boris, an option, of course, but as I write, I have a ton of variables, nesting reaches 5 functions, transferring a bunch of parameters to each, of course, not an option, saved the global. I did not expect that the scope in php is different from the same pluses or java. But thanks for the answer!) - PhoEn-X

1 answer 1

Reply from comment:

The problem is that in the auth function, the value of login_params not available. You can pass this array through the function argument:

 $login_params = explode('=', file_get_contents('php://input')); auth($login_params); function auth($login_params) { // ... } 

You can also declare the $login_params variable global:

 $login_params = explode('=', file_get_contents('php://input')); auth(); function auth() { global $login_params; // ... } 

It is worth noting that the use of global variables will significantly complicate code support in the future.

  • Will complicate? How, if not a secret? - PhoEn-X
  • @ PhoEn-X, a large project that actively uses global variables is difficult to debug . The difficulty lies in tracking all those places where these variables are accessed and the logic of their change. And the most terrible thing is that it is often difficult to understand in what order the references to the global variable go and which piece of code writes incorrect information into the variable. I would recommend generally to abandon global variables in any project larger than 500 (conditionally) lines of code. - Dmitriy Simushev
  • did not observe such in programs on a C ++, Java. If you stick global in all functions, then you can simulate the same scope of variables as in the above languages, and there my codes reach 1k lines, I never noticed a problem) - PhoEn-X