G
G
Grigory2017-04-17 08:54:10
PHP
Grigory, 2017-04-17 08:54:10

Authorization on another site using PHP curl: how to solve the problem with COOKIE?

Dear colleagues!
I ask for help, perhaps I do not understand something.
Task: using a PHP script, automatically log in to another site and go to it (in the user's browser).
Those. the user visits my site, the authorization script sends information for authorization to a third-party site, after which it transfers the user to this third-party site, where authorization has already been made using a specific login and password.
I am logging in with curl.
Problem: for some reason, when outputting results to the browser using echo, authorization works, but is "lost" when you subsequently go to this site using the Refresh or Location header

// URL скрипта авторизации на стороннем сайте
        $login_url = 'https://example.com/index.php?action=login2';
  
        // параметры для отправки запроса - логин и пароль
      $post_data = array(
        'user' => 'MYUSERNAME',
        'passwrd' => 'MYPASSWORD',
        'cookielength' => '-1',
        'hash_passwrd' => ''
      );
  
        // создание объекта curl
        $ch = curl_init();
  
        // используем User Agent браузера
        $agent = $_SERVER["HTTP_USER_AGENT"];
        curl_setopt($ch, CURLOPT_USERAGENT, $agent);
  
        // задаем URL
        curl_setopt($ch, CURLOPT_URL, $login_url );
  
        // указываем что это POST запрос
        curl_setopt($ch, CURLOPT_POST, 1 );
  
        // задаем параметры запроса
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  
        // указываем, чтобы нам вернулось содержимое после запроса
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  
        // в случае необходимости, следовать по перенаправлени¤м
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  
        /*
            Задаем параметры сохранени¤ cookie
            как правило Cookie необходимы для дальнейшей работы с авторизацией
        */
  
        curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
        curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');

  
        // выполняем запрос для авторизации
        $postResult = curl_exec($ch);
        
        curl_close($ch);

             // Если выводим результат работы curl на страницу через Echo, 
              // то показывает страницу с авторизованным пользователем (всё работает)
             // echo $postResult;

        // А если перекидываем пользователя на нужную страницу 
        //в браузере пользователя, то авторизация теряется, и мы видим "Привет гость"

        header( 'Refresh: 0; url=https://example.com/index.php' );

Why is authorization triggered when the results are displayed in the browser, but "lost" when going to this site using the Refresh or Location header? And how to solve this problem?
Thank you very much in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2017-04-17
@rpsv

Duck no way?
You will not be able to set cookies for another domain.
Otherwise, authorization will not work.
Or send a token (when redirecting to a third-party site), which will authorize not by cookies, but by the token in the parameter.
PS very similar to the "man in the middle" attack, hmm...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question