Forum Moderators: coopster

Message Too Old, No Replies

fget() problems with 302

Building a small robot, need help

         

Phoog

4:38 pm on Dec 28, 2006 (gmt 0)

10+ Year Member



Hello!

Im currently building a small php robot that needs to login to a site to fetch a number. Ok I have come so far that Im logged in, but when i use the form I get a 302 to another page whete the info is...

The code Im using to login is looking like:

fputs($fp, "POST $url HTTP/1.1\r\n"); 
fputs($fp, "Host: $host\r\n");
fputs($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
fputs($fp, "Content-Length: $content_length\r\n");
fputs($fp, "\r\n");
fputs($fp, $formdata);

This works really fine, but now I get a 302 to the logged in page, also it sets cookis with my username, looks like this:

HTTP/1.1 302 Found Date: Thu, 28 Dec 2006 16:10:44 GMT Content-Type: text/html; charset=UTF-8 Expires: Thu, 28 Dec 2006 16:10:44 GMT Cache-Control: private Connection: close Server: Apache X-Powered-By: PHP/5.1.6-1 Set-Cookie: PHPSESSID=e8419f7b84714d9bba6b9df92c508405; path=/; domain=.example.com Pragma: no-cache Set-Cookie: PHPSESSID=dbebd1051a5123645756b49c10da1160; path=/; domain=.example.com Set-Cookie: login=Joacim; path=/; domain=.example.com Set-Cookie: id_hash=#*$!#*$!#*$!#*$!#*$!XX; path=/; domain=.example.com Location: http://example.com/auth Via: 1.1 supercache4 (NetCache NetApp/6.0.4)

When I try to open a new connection the cookies and so on doesent follow, even if I dont close the old one...

[edited by: coopster at 4:41 pm (utc) on Dec. 28, 2006]
[edit reason] generalized domain [/edit]

mcavic

5:35 pm on Dec 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That sounds like the correct behavior. Scripts that handle POST forms usually do a 302 redirect to another landing page after they process the input.

I would print another \r\n after the formdata just for good measure.

And as for the cookies, you have to send the cookie data on each request you make, otherwise all of your requests will be different sessions.

Phoog

9:26 pm on Dec 28, 2006 (gmt 0)

10+ Year Member



Oh okey, so Session == Cookie then?

How do I send the cookie information in the headers with fputs() then?

Thanks

mcavic

12:11 am on Dec 29, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, the cookie identifies the session.

Here's the specification I've always used:
[cgi.netscape.com...]

And I believe it'd be like:
fputs($fp, "Content-Length: $content_length\r\n");
fputs($fp, "Cookie: key=value\r\n");
fputs($fp, "\r\n");