douya1974 2013-11-16 13:35
浏览 46

cUrl保持跟随位置标题

Okay I post 2 fields to a url using cUrl, this page checks my post fields and based on them it returns me cookies. However before i can even read the cookies I'm already redirected to an other page.
I tried setting _FOLLOWLOCATION, 0 but I'm still getting redirected. Are there any thoughts on this?
Code:

$headers[] = "Accept: */*";
$headers[] = "Connection: Keep-Alive";
$headers[] = "Content-type: application/x-www-form-urlencoded;charset=UTF-8";

$fields_string = "login_url=/login.do?school=windesheim";
$fields_string=rtrim($fields_string, "&");
$cSession = curl_init(); 
$tmpfname = dirname(__FILE__).'/cookie2.txt';
curl_setopt($cSession, CURLOPT_REFERER,  "https://website.com/login.do?error=nomandant");
curl_setopt($cSession, CURLOPT_HTTPHEADER,  $headers)
curl_setopt($cSession, CURLOPT_COOKIEFILE, $tmpfname);
curl_setopt($cSession, CURLOPT_COOKIEJAR, $tmpfname);
curl_setopt($cSession,CURLOPT_URL,"https://website.com/j_spring_security_check");
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cSession, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($cSession, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($cSession, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($cSession,CURLOPT_HEADER, true); 
curl_setopt ($cSession, CURLOPT_POST, true);
curl_setopt ($cSession, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($cSession,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
$result=curl_exec($cSession);
curl_close($cSession);
echo htmlspecialchars($result);

If I print the header it says location: https://website.com/login.do?error=nomandant

So I go to this page, I can't get the cookies because I'm redirected to the next page which then redirects me back to the first page because I don't have the cookies set. I want to get on the first page, don't be redirected, and read the cookies from the header.
How come that I'm being redirected even though followlocation is set to 0/false?

Update:
When I'm in my browser at website.com and I enter Windesheim in postfield and post it this it posts to /j_spring_security_check

browser network inspector

So i try to simulate this using cUrl using the code from above (updated with more info). But the output is only:

HTTP/1.1 302 Found Connection: Keep-Alive Content-Length: 0 
Date: Sat, 16 Nov 2013 14:28:33 GMT 
Location: https://website.com/login.do?error=nomandant Server: Apache-Coyote/1.1

The expected result is something like the response header in the picture above.
So something is going wrong that it is redirecting me to

/login.do?error=nomandant 

instead of

/index.do;jsessionid=etc.

But I can't figure out what I am doing wrong as there are only 2 field being posted and I included them both in my code.

  • 写回答

1条回答 默认 最新

  • dongshai2022 2013-11-16 14:10
    关注

    First, note that you'll be able to read cookies after the session is closed with curl_close.

    See http://php.net/manual/en/function.curl-setopt.php

    From what you say you're actually being redirected by the server to another location, but curl is not following that location. This is the reason you see an header with a redirection.

    curl is behaving as expected!

    If you set it to follow the new location you would see the haeder and the page at the new location https://website.com/login.do?error=nomandant

    Given the data you're posting to the URL the server wants to send you back to https://website.com/login.do?error=nomandant

    If this is not what you expect either the cookies you're sending with the request are not good (for example missing session value...) or you're posting the wrong data.

    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用