ColdFusion Stats:

How to support the site


Site Wide Message: (current site time 9/6/2010 12:48:31 PM EDT)
  • We want your input! One of our sponsors wants to know your opinion about development related issues. Click here to tell us what you think.
  • Are you an emerging/young developer (aged 18-30)? If so, would you like the chance to affect future developer tools and products?
    If so, then click here to give your feedback.
 

GetCookies

Print
Email
VB icon
Submitted on: 4/23/2007 5:13:22 PM
By: Rabid Nerd Productions  
Level: Beginner
User Rating: Unrated
Compatibility:Cold Fusion 5, Cold Fusion MX

Users have accessed this code  8435 times.
 
author picture
(About the author)
 
     This code allows for simulated browser based interaction with remote websites.
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
// for :GetCookies
//**************************************
De-versioning adaptation of open source Cold Fusion MX script by rickroot:
http://www.opensourcecf.com/1/2006/05/Using-coldfusion-to-log-into-a-phpBB-system.cfm
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: GetCookies
// Description:This code allows for simulated browser based interaction with remote websites.
// By: Rabid Nerd Productions
//
// Inputs:Cookies to be set as returned by remote servers in response headers (cfhttp.header)
//
// Returns:GetCookies variable as an array
//
// Assumes:Example Usage:
A site where the page sneaky.php requires a set of server cookies and a session variable set in a form field from the homepage.
Example Session Field Value:
<input type="hidden" name="__SESSION" value="dhu}FR_VKROJeiCNWJrif">
<cfhttp url="http://www.site.com" method="GET" useragent="#cgi.http_user_agent#">
<cfset cookies = getCookies(cfhttp.header)>
<cfset getstate = cfhttp.filecontent>
<cfset getstate = replacenocase(getstate,"__SESSION"" value=""",chr(7))>
<cfset getstate = listgetat(getstate,2,chr(7))>
<cfset getstate = listgetat(getstate,1,"""")>
<cfhttp url="http://www.site.com/sneaky.php" method="POST" useragent="#cgi.http_user_agent#">
	<cfhttpparam name="__SESSION" value="#SESSION#" type="FORMFIELD">
<cfloop from="1" to="#arrayLen(cookies)#" step="1" index="i">
	<cfhttpparam type="cookie" name="#cookies[i].NAME#" value="#cookies[i].VALUE#">
	</cfloop>
</cfhttp>
//
// Side Effects:Edited - "WriteOutput" to screen of cookie values for debug use were left in in initial posting... Have now commented them below..
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=135&lngWId=9//for details.//**************************************

<cfscript>
function getCookies(headers){
cookies = arraynew(1);
thiscookie = structnew();
header="";
aCookie="";
crumb="";
paramName="";
paramValue="";
for (i=1;i lte listlen("#headers#","#chr(10)#");i=i+1) {
	header = listgetat(headers,i,chr(10));
	if(reFind("^Set-Cookie: ",header) gt 0){
		acookie = reReplace(header,"^Set-Cookie: ","","ALL");
		thisCookie = StructNew();
		for (ii=1;ii lte listlen("#acookie#",";");ii=ii+1){
			// writeoutput(crumb);
			// writeoutput("<p><hr></p>");
			crumb = listgetat(acookie,ii,";");
			crumb = trim(crumb);
			paramName = listgetat(crumb,1,"=");
			if (listlen(crumb,"=") gt 1) {paramValue = listgetat(crumb,2,"=");} else {paramValue=" ";}
			if(paramName eq "expires"){
				thisCookie.expires = paramValue;
			} else {
			if(paramName eq "path"){
				thisCookie.path = paramValue;
			} else {
			if(paramName eq "domain"){
				thisCookie.domain = paramValue;
			} else {
			if(paramName eq "secure"){
				thisCookie.secure = paramValue;
			} else {
				thisCookie.name = paramName;
				thisCookie.value = paramValue;
			}}}}
		}
		arrayAppend(cookies,thisCookie);
	}
}
return cookies;
}
</cfscript>


Other 4 submission(s) by this author

 

 
 Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:
 
Your Vote!

What do you think of this code(in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments

 There are no comments on this submission.
 
Add Your Feedback!

Note:Not only will your feedback be posted, but an email will be sent to the code's author from the email account you registered on the site, so you can correspond directly.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
To post feedback, first please login.