CURL PHP(Wamp)遵循Javascript windows.location重定向?

时间:2016-08-02 10:10:56

标签: php curl

我的问题与一个php文件中的curl有关,该文件跟随Javascript windows.location由服务器返回,我无法成功绕过该行为

事实上,我编写了一个脚本,该脚本连接到一个带有用户身份验证表单的网站。该脚本在其全局性中完美运行:

  • 第一个请求:获取在Cookie中获取PHP会话的请求
  • 第二次请求:带有cookie的发布请求和包含用户/密码的发布数据

问题:我总是通过windows.location = XXXX的服务器应答中的javascript函数重定向

有关信息,我使用的是WampServer版本2.5 / PHP 5.5.12

我的脚本是通过网络浏览器调用的:http://localhost/glpiv2/rechercheDerniersSuivisV2.php

我第一次创建一个cookie

function createCookie (){   

  global $proxy;
  global $proxyauth;
  global $cookies_file;
  global $timeout;

  $url='https://xxx.xxxx.xxx.xxx/glpi/index.php';
  $ch = curl_init(); 

  // Proxy Authentication, keep cool with security
  curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, true);
  curl_setopt($ch, CURLOPT_PROXY, $proxy);
  curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  0);
  curl_setopt ($ch, CURLOPT_URL, $url); 
  curl_setopt($ch, CURLOPT_HEADER, true); 
  curl_setopt($ch, CURLOPT_POST , false);
  curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);  
  curl_setopt($ch,CURLOPT_HTTPHEADER,array('User-Agent: Mozilla/6.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko','DNT: 1','Connection: Keep-Alive'));

  // => WRITE A NEW COOKIE FILE
  curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies_file);

  // => ESTABLISH A NEW SESSION
  curl_setopt ($ch, CURLOPT_COOKIESESSION, true); 

  $file_contents = curl_exec($ch); 

  //  If Error
   if(curl_errno($ch)){
      // Le message d'erreur correspondant est affiché
      echo "ERREUR curl_exec : ".curl_error($ch);
    } 

  curl_close($ch);  
}

服务器响应第一个请求的标头

Server: Apache
Set-Cookie: PHPSESSID=3c5939450c6811b8df981f83c9539f64; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, precheck=0
Pragma: no-cache
Content-Length: 253
Connection: close
Content-Type: text/html

使用PHPSESSID创建Cookie:确定

第二次使用新创建的cookie执行POST请求以使用真实用户/密码进行身份验证

function authenticateSession(){

  echo "Lancement de authenticateSession";
  global $proxy;
  global $proxyauth;
  global $cookies_file;
  global $timeout;
  global $authenticationGLPIPost;

  $url='https://xxx.xxx.xxx.xxx/glpi/login.php';

  $ch1 = curl_init(); 

  // Proxy SSL and other stuff
  curl_setopt($ch1, CURLOPT_HTTPPROXYTUNNEL, TRUE);
  curl_setopt($ch1, CURLOPT_PROXY, $proxy);
  curl_setopt($ch1, CURLOPT_PROXYUSERPWD, $proxyauth);
  curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, FALSE);
  curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST,  0);

  // Post preparation
  curl_setopt ($ch1, CURLOPT_URL, $url); 
  curl_setopt($ch1, CURLOPT_HEADER, FALSE); 
  curl_setopt($ch1, CURLOPT_POST , TRUE);

  // POST DATA with variable containing user / password
  curl_setopt($ch1, CURLOPT_POSTFIELDS, $authenticationGLPIPost);
  curl_setopt($ch1, CURLOPT_COOKIEFILE, $cookies_file);
  curl_setopt ($ch1, CURLOPT_CONNECTTIMEOUT, $timeout);  

  $file_contents = curl_exec($ch1); 
  curl_close($ch1);     
 }

第二次请求的服务器响应标头

Server: Apache
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, precheck=0
Pragma: no-cache
Content-Length: 269
Connection: close
Content-Type: text/html

<script language=javascript>
   NomNav = navigator.appName;
     if (NomNav=='Konqueror'){
        window.location="/glpi/front/central.php?tokonq=fsrb7s";
     } else {
        window.location="/glpi/front/central.php";
     }
  </script>

=&GT;在响应中使用javascript windows.location重定向的问题,将我重定向到http://localhost/glpi/front/central.php

服务器响应显示在webbrowser中。

我怀疑网络浏览器确实执行了返回的Javascript并重定向了我 我验证使用代理拦截器和更改服务器响应我的意思是当我压制Javascript块或如果我更改windows.location的参数重定向修改其行为

我尝试不重定向但没有成功,但每个选项都没有成功

curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 0); 
curl_setopt ($ch, CURLOPT_MAXREDIRS, 0); 

这意味着我第一次运行脚本并调用这两个函数我总是在身份验证成功的网页上重定向,这意味着相对路径为/glpi/front/central.php。

2 个答案:

答案 0 :(得分:1)

哦,我明白了。

$file_contents = curl_exec($ch1)
  

返回curl_exec

的值      

成功时返回TRUE,失败时返回FALSE。但是,如果设置了CURLOPT_RETURNTRANSFER选项,它将在成功时返回结果,在失败时返回FALSE。

除非设置了CURLOPT_RETURNTRANSFER选项,否则$file_contents将不包含文件内容。它们将被打印(相当于echo $file_contents)。

答案 1 :(得分:0)

奇怪的是,当我设置

时,一切正常
 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);