tags:

views:

134

answers:

3

I wanted to copy files from a remote server, but it seems that the remote host is using session, and needs cookies.

well I used this method after defining variables...

$url="http://example.org/exmple.mp3";
$nn = "\r\n";
$cookies="";
$request = GET . " " . str_replace ( " ", "%20", $url ) . " HTTP/1.1" . $nn . "Host: " . $host . $nn . "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14" . $nn . "Accept: */*" . $nn . "Accept-Language: en-us;q=0.7,en;q=0.3" . $nn . "Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7" . $nn . "Pragma: no-cache" . $nn . "Cache-Control: no-chache" . $nn  . $proxyauthorization . $referer . $cookies . "Connection: Close";

$fp= socksopen($Proxy,$port, $errno, $errstr, 15 );

socket_set_timeout ( $fp, 120 );

fputs ( $fp, $request );

fflush ( $fp );

//read header

$i = 1;

do {

$header.= @fgets ( $fp, 128 );

$i++;

} while ( strpos ( $header, $nn . $nn ) === false );

echo $header;

fclose ($fp);

now I have the header with cookies that the url said :

HTTP/1.1 302 Moved Temporarily Content-length: 0 Content-type: text/html Server: Sun-ONE-Web-Server/6.1 Date: Mon, 28 Dec 2009 13:40:53 GMT Set-cookie: ERIGHTS=5YAaxxmNsMuTK87E1TCAohwDRuyqBaCgM-oehmg24bkzHplCtmgn7zMA==;path=/;domain=.example.org Set-cookie: WLSESSION=1528980108.20480.0000; expires=Tue, 29-Dec-2009 13:40:52 GMT; path=/ Location: http://example.org/exmple.mp3&tag=1 Via: 1.1 proxy-server1 Proxy-agent: Sun-Java-System-Web-Proxy-Server/4.

then I did some sting code and built this in the code:

$cookies="ERIGHTS=5YAaxxmNsMuTK87E1TCAohwDRuyqBaCgM-oehmg24bkzHplCtmgn7zMA==, WLSESSION=1528980108.20480.0000";

re-requesting the url with same method mentioned above and again i have got same header with another cookies

It seams that the remote website is treating me as a first-time visitor each time and sets new cookies gain

what is wrong?

+1  A: 

It seems a lot of trouble to be handcrafting the HTTP request. Why not use something like cURL in PHP which supports cookies out of the box.

mopoke
thanks seems I have to forget whole my code and turn to cURL :( :(
safaali
+1  A: 

First off, congratulations for trying to do this by hand yourself instead of using something pre-packaged like cURL. "Real programmers" do that from time to time, even if it's less efficient or reliable, because it increases our understanding of the systems we use and build.

Now, on to the problem at hand.

You're not transmitting the cookies to the server correctly. Your code is sending just the cookie value itself, not the "Cookie" headers that you need to send. The correct way to send a cookie with a request is Cookie: name=value, as in Cookie: ERIGHTS=5YAaxxmNsMuTK87E1TCAohwDRuyqBaCgM-oehmg24bkzHplCtmgn7zMA==,WLSESSION=1528980108.20480.0000.

Ross Patterson
thanks :), I want to know what I am doing, when using cURL, i am blind........ so the difference between what I said and u is a space, i included a space mine: ...n7zMA==, WLSESSION=1528... yours: n7zMA==,WLSESSION=1528 is that the problem?
safaali
safaali
Well, the difference between your original code and mine is that yours doesn't have "Cookie: " at the start of $cookies. That means that the cookies aren't being recognized as cookies. The spaces between multiple cookies in a single header are optional.
Ross Patterson
well, still have problem, see my another clarification,
safaali
A: 

there is one thing, the file i am trying to copy is a .pdf file exactly

i checked that by my browser, the results:

say the URL is:

http://example.com/stamp/stamp.jsp?tp=&arnumber=5344171&isnumber=5344169

I checked the source code of the URL, it contains a redirection to another page that either is a javascript with iframe

source of the page (from my browser)

     <frameset rows="65,35%">
<frame src="http://example.com/stamp/banner.jsp" frameborder="0" framespacing="0" framepadding="0" scrolling="no" />
<frame src="http://example.com/stampPDF/getPDF.jsp?tp=&amp;arnumber=5255176&amp;isnumber=5255174" frameborder="0" />

as you see when I click the URL, it redirects me to URL2 the URL2 is this:http://example.com/stampPDF/getPDF.jsp?tp=&amp;arnumber=5255176&amp;isnumber=5255174

the URL2 contains .pdf file I can easily copy(download).

but when i use my code mentioned above, $url="http://example.com/stamp/stamp.jsp?tp=&amp;arnumber=5255176&amp;isnumber=5255174" it needs cookies to set with my request, when receive cookies and set and send cookies with my request it again replies a new cookie !!

what is wrong guys?

safaali