VB and VBA Users Source Code: Creating an internet cookie
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Creating an internet cookie
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Friday, June 15, 2001
Hits:
2744
Category:
Internet/Remote Comms
Article:
The following code demonstates how to create internet cookies. Please read the "Notes" for an explaination of the current limitations of creating cookies using the wininet dll. Option Explicit Private Declare Function InternetSetCookie Lib "wininet.dll" Alias "InternetSetCookieA" (ByVal lpszUrlName As String, ByVal lpszCookieName As String, ByVal lpszCookieData As String) As Boolean Private Declare Function InternetGetCookie Lib "wininet.dll" Alias "InternetGetCookieA" (ByVal lpszUrlName As String, ByVal lpszCookieName As String, ByVal lpszCookieData As String, lpdwSize As Long) As Boolean 'Purpose : Creates an internet cookie 'Inputs : sURL The URL to create the cookie for. ' sCookieName The name of the cookie. ' sCookieData The data to store in the cookie 'Outputs : Returns True if succeeded in creating the cookie. 'Author : Andrew Baker 'Date : 25/05/2001 'Notes : Cookies created by this function without an expiration date are stored in memory and are ' available only in the same process that created them. Cookies that include an expiration ' date are stored in the windows\cookies directory. ' Creating a new cookie might cause a dialog box to appear on the screen if the appropriate ' registry value, AllowCookies, is set. Function InternetCookieCreate(sURL As String, sCookieName As String, sCookieData As String) As Boolean On Error GoTo ErrFailed InternetCookieCreate = InternetSetCookie(sURL, sCookieName, sCookieData) Exit Function ErrFailed: Debug.Print Err.Description InternetCookieCreate = False End Function 'Purpose : Returns ALL the data from an internet cookie 'Inputs : sURL The URL to create the cookie for. ' sCookieName Not supported in the current version of the wininet dll ' sCookieData The data to store in the cookie 'Outputs : Returns True if succeeded in creating the cookie. 'Author : Andrew Baker 'Date : 25/05/2001 'Notes : Function InternetCookieGet(sURL As String, sCookieName As String, sCookieData As String) As String Const clMaxCookieLen = 5012 'Arbitary limit Dim sResult As String * clMaxCookieLen Dim bRet As Boolean, lLen As Long On Error GoTo ErrFailed lLen = clMaxCookieLen bRet = InternetGetCookie(sURL, sCookieName, sResult, lLen) If bRet Then InternetCookieGet = Left$(sResult, lLen) End If Exit Function ErrFailed: Debug.Print Err.Description InternetCookieGet = "" End Function 'Demonstration routine Sub Test() InternetCookieCreate "http://www.vbusers.com", "Andrew", "Test Cookie" Debug.Print InternetCookieGet("http://www.vbusers.com", "", "Test Cookie") End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder