VBScript/PHP クッキーデータが自動的に受け渡しされる事のテスト


  MSXML2.XMLHTTP は仕様として自動的にクッキーが有効です




もともと、IE の API を使っているようなので、予想通りでした。
以下は、呼び出し側のテストコードです

  

' ***********************************************************
' クライアント用オブジェクト
' ***********************************************************
Set objHTTP = Wscript.CreateObject("MSXML2.XMLHTTP")

' ***********************************************************
' クッキーが自動的に受け渡しされている事を確認するテスト
' ***********************************************************
Call objHTTP.Open("GET","http://localhost/web/test/sv1.php",False)
Call objHTTP.Send("")

' ***********************************************************
' 全てのヘッダ( クッキーが含まれる )
' ***********************************************************
strHeaders = objHTTP.getAllResponseHeaders()
Wscript.Echo strHeaders

' ***********************************************************
' このレスポンスにクッキーがセットされている事を確認
' ***********************************************************
Call objHTTP.Open("GET","http://localhost/web/test/sv2.php",False)
Call objHTTP.Send("")

Wscript.Echo objHTTP.responseText
  

以下は、受け取り側の PHP のコードです。

sv1.php

  

<?

setcookie( 'data',"1234" );
setcookie( 'lightbox',"winofsql" );

?>

OK
  


sv2.php

  

------------------------------
<?

print $_SERVER['HTTP_COOKIE'] . "\n";

?>
------------------------------
OK
  

以下がその実行結果となります。単純に二度呼び出しているだけで、2回目の呼び出しの
結果に最初の Set-Cookie が反映されています

  

C:\user\web\test>cscript test1.vbs
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

MIME-Version: 1.0
Server: AnWeb/1.42p
Date: Fri, 18 Jun 2010 01:56:01 GMT
Transfer-Encoding: chunked
Expires: -1
Pragma: no-cache
Cache-control: no-cache
Last-Modified: Fri, 18 Jun 2010 01:56:01 GMT
X-Powered-By: PHP/5.3.2
Set-Cookie: data=1234
Set-Cookie: lightbox=winofsql
Content-type: text/html


------------------------------
data=1234; lightbox=winofsql
------------------------------
  







  同様の事を php の cURL でテストしました




  

<?
// **********************************************************
// curl クッキーテスト
// **********************************************************
header( "Content-Type: text/html; Charset=utf-8" );
header( "pragma: no-cache" );
header( "Expires: Wed, 31 May 2000 14:59:58 GMT" );
header( "Cache-control: no-cache" );

// *********************************************************
// curl 処理
// *********************************************************
$target_url = 'http://localhost/web/test/sv1.php';
$curl = curl_init();
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $target_url);

// ***********************************************************
// クッキーが自動的に受け渡しされるようにする
// ***********************************************************
curl_setopt($curl, CURLOPT_COOKIEFILE, 'c:\\cookiefile.txt'); 
curl_setopt($curl, CURLOPT_COOKIEJAR, 'c:\\cookiefile.txt'); 

// *********************************************************
// 送信( 一回目 )
// *********************************************************
curl_setopt($curl, CURLOPT_VERBOSE, true);	// デバッグ
$handle_1 = fopen("./debug1.txt", "w");
curl_setopt($curl, CURLOPT_STDERR, $handle_1);
$handle_2 = fopen("./ret_header1.txt", "w");
curl_setopt($curl, CURLOPT_WRITEHEADER, $handle_2);
$result = curl_exec($curl);

fclose($handle_2);
fclose($handle_1);

print $result;


// *********************************************************
// curl 処理
// *********************************************************
$target_url = 'http://localhost/web/test/sv2.php';
$curl = curl_init();
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $target_url);

// ***********************************************************
// クッキーが自動的に受け渡しされるようにする
// ***********************************************************
curl_setopt($curl, CURLOPT_COOKIEFILE, 'c:\\cookiefile.txt'); 
curl_setopt($curl, CURLOPT_COOKIEJAR, 'c:\\cookiefile.txt'); 

// *********************************************************
// 送信( 二回目 )
// *********************************************************
curl_setopt($curl, CURLOPT_VERBOSE, true);	// デバッグ
$handle_a = fopen("./debug2.txt", "w");
curl_setopt($curl, CURLOPT_STDERR, $handle_a);
$handle_b = fopen("./ret_header2.txt", "w");
curl_setopt($curl, CURLOPT_WRITEHEADER, $handle_b);
$result = curl_exec($curl);

print $result;

fclose($handle_b);
fclose($handle_a);


// *********************************************************
// curl 終了
// *********************************************************
curl_close($curl);

?>
  

こちらも、クッキーを保存するファイルを指定してやると、自動的に保持してくれます

dubug1.txt
  

* About to connect() to localhost port 80 (#0)
*   Trying 127.0.0.1... * connected
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET /web/test/sv1.php HTTP/1.1
Host: localhost
Accept: */*

< HTTP/1.1 200 Document follows
< MIME-Version: 1.0
< Server: AnWeb/1.42p
< Date: Fri, 18 Jun 2010 02:00:40 GMT
< Transfer-Encoding: chunked
< Expires: -1
< Pragma: no-cache
< Cache-control: no-cache
< Last-Modified: Fri, 18 Jun 2010 02:00:40 GMT
< X-Powered-By: PHP/5.3.2
* Added cookie data="1234" for domain localhost, path /web/test/, expire 0
< Set-Cookie: data=1234
* Added cookie lightbox="winofsql" for domain localhost, path /web/test/, expire 0
< Set-Cookie: lightbox=winofsql
< Content-type: text/html
< 
* Connection #0 to host localhost left intact
* Closing connection #0
  

dubug2.txt
  

* About to connect() to localhost port 80 (#0)
*   Trying 127.0.0.1... * connected
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET /web/test/sv2.php HTTP/1.1
Host: localhost
Accept: */*
Cookie: data=1234; lightbox=winofsql

< HTTP/1.1 200 Document follows
< MIME-Version: 1.0
< Server: AnWeb/1.42p
< Date: Fri, 18 Jun 2010 02:00:41 GMT
< Transfer-Encoding: chunked
< Expires: -1
< Pragma: no-cache
< Cache-control: no-cache
< Last-Modified: Fri, 18 Jun 2010 02:00:41 GMT
< X-Powered-By: PHP/5.3.2
< Content-type: text/html
< 
* Connection #0 to host localhost left intact
* Closing connection #0
  

cookiefile.txt
  

# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.

localhost	FALSE	/web/test/	FALSE	0	data	1234
localhost	FALSE	/web/test/	FALSE	0	lightbox	winofsql
  

  リダイレクトテスト

MSXML2.XMLHTTP の場合は自動ですが、cURL では設定が必要です。
サーバー側1を以下のようにしました。これによって、クライアントの呼び出しは1回で
おなじ結果になります

  

<?

setcookie( 'data',"1234" );
setcookie( 'lightbox',"winofsql" );

header( "Location: http://localhost/web/test/sv2.php");

?>

OK
  

  

<?
// **********************************************************
// curl クッキーテスト
// **********************************************************
header( "Content-Type: text/html; Charset=utf-8" );
header( "pragma: no-cache" );
header( "Expires: Wed, 31 May 2000 14:59:58 GMT" );
header( "Cache-control: no-cache" );

// *********************************************************
// curl 処理
// *********************************************************
$target_url = 'http://localhost/web/test/sv1.php';
$curl = curl_init();
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $target_url);

// ***********************************************************
// クッキーが自動的に受け渡しされるようにする
// ***********************************************************
curl_setopt($curl, CURLOPT_COOKIEFILE, 'c:\\cookiefile.txt'); 
curl_setopt($curl, CURLOPT_COOKIEJAR, 'c:\\cookiefile.txt'); 

curl_setopt($curl, CURLOPT_MAXREDIRS, 10); 
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 


// *********************************************************
// 送信( 一回目 )
// *********************************************************
curl_setopt($curl, CURLOPT_VERBOSE, true);	// デバッグ
$handle_1 = fopen("./debug1.txt", "w");
curl_setopt($curl, CURLOPT_STDERR, $handle_1);
$handle_2 = fopen("./ret_header1.txt", "w");
curl_setopt($curl, CURLOPT_WRITEHEADER, $handle_2);
$result = curl_exec($curl);

fclose($handle_2);
fclose($handle_1);

print $result;

// *********************************************************
// curl 終了
// *********************************************************
curl_close($curl);

?>
  

以下はそのデバッグトレースです

  

* About to connect() to localhost port 80 (#0)
*   Trying 127.0.0.1... * connected
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET /web/test/sv1.php HTTP/1.1
Host: localhost
Accept: */*

< HTTP/1.1 302 Moved Temporarily 
< MIME-Version: 1.0
< Server: AnWeb/1.42p
< Date: Fri, 18 Jun 2010 02:35:30 GMT
< Transfer-Encoding: chunked
< Expires: -1
< Pragma: no-cache
< Cache-control: no-cache
< Last-Modified: Fri, 18 Jun 2010 02:35:30 GMT
< X-Powered-By: PHP/5.3.2
* Added cookie data="1234" for domain localhost, path /web/test/, expire 0
< Set-Cookie: data=1234
* Added cookie lightbox="winofsql" for domain localhost, path /web/test/, expire 0
< Set-Cookie: lightbox=winofsql
< Location: http://localhost/web/test/sv2.php
< Content-type: text/html
< 
* Ignoring the response-body
* Connection #0 to host localhost left intact
* Issue another request to this URL: 'http://localhost/web/test/sv2.php'
* Re-using existing connection! (#0) with host localhost
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET /web/test/sv2.php HTTP/1.1
Host: localhost
Accept: */*
Cookie: data=1234; lightbox=winofsql

< HTTP/1.1 200 Document follows
< MIME-Version: 1.0
< Server: AnWeb/1.42p
< Date: Fri, 18 Jun 2010 02:35:32 GMT
< Transfer-Encoding: chunked
< Expires: -1
< Pragma: no-cache
< Cache-control: no-cache
< Last-Modified: Fri, 18 Jun 2010 02:35:32 GMT
< X-Powered-By: PHP/5.3.2
< Content-type: text/html
< 
* Connection #0 to host localhost left intact
* Closing connection #0
  


関連する記事

VB.net : HttpWebRequest と HttpWebResponse でクッキーのやり取り





yahoo  google  MSDN  MSDN(us)  WinFAQ  Win Howto  tohoho  ie_DHTML  vector  wdic  辞書  天気 


[sh_web]
Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
24/04/20 02:42:12
InfoBoard Version 1.00 : Language=Perl

1 BatchHelper COMprog CommonSpec Cprog CprogBase CprogSAMPLE CprogSTD CprogSTD2 CprogWinsock Cygwin GameScript HTML HTMLcss InstallShield InstallShieldFunc JScript JScriptSAMPLE Jsfuncs LLINK OldProg OracleGold OracleSilver PRO PRObrowser PROc PROconePOINT PROcontrol PROftpclient PROjscript PROmailer PROperl PROperlCHAT PROphp PROphpLesson PROphpLesson2 PROphpLesson3 PROphpfunction PROphpfunctionArray PROphpfunctionMisc PROphpfunctionString PROsql PROvb PROvbFunction PROvbString PROvbdbmtn PROvbonepoint PROwebapp PROwin1POINT PROwinSYSTEM PROwinYOROZU PROwindows ProjectBoard RealPHP ScriptAPP ScriptMaster VBRealtime Vsfuncs a1root access accreq adsi ajax amazon argus asp aspSample aspVarious aspdotnet aw2kinst cappvariety centura ckeyword classStyle cmaterial cmbin cmdbapp cmenum cmlang cmlistbox cmstd cmstdseed cmtxt cs daz3d db dbCommon dbaccess dnettool dos download flex2 flex3 flex4 framemtn framereq freeWorld freesoft gimp ginpro giodownload google hdml home hta htmlDom ie9svg install java javaSwing javascript jetsql jquery jsp jspTest jspVarious lightbox listasp listmsapi listmsie listmsiis listmsnt listmspatch listmsscript listmsvb listmsvc memo ms msde mysql netbeans oraPlsql oracle oracleWiper oraclehelper orafunc other panoramio pear perl personal pgdojo pgdojo_cal pgdojo_holiday pgdojo_idx pgdojo_ref pgdojo_req php phpVarious phpguide plsql postgres ps r205 realC realwebapp regex rgaki ruby rule sboard sc scprint scquest sdb sdbquest seesaa setup sh_Imagick sh_canvas sh_dotnet sh_google sh_tool sh_web shadowbox shgm shjquery shvbs shweb sjscript skadai skywalker smalltech sperl sqlq src systemdoc tcpip tegaki three toolbox twitter typeface usb useXML vb vbdb vbsfunc vbsguide vbsrc vpc wcsignup webanymind webappgen webclass webparts webtool webwsh win8 winofsql wmi work wp youtube