Setting up Cookies
It is very easy to send cookies to browser. These cookies will
be sent along with HTTP Header before the Content-type filed.
Assuming you want to set UserID and Password as cookies.
So cookies setting will be done as follows:
#include <iostream>
using namespace std;
int main ()
{
cout << "Set-Cookie:UserID=XYZ;\r\n";
cout << "Set-Cookie:Password=XYZ123;\r\n";
cout << "Set-Cookie:Domain=www.onlionedu.blogspot.com;\r\n";
cout << "Set-Cookie:Path=/perl;\n";
cout << "Content-type:text/html\r\n\r\n";
cout << "<html>\n";
cout << "<head>\n";
cout << "<title>Cookies in CGI</title>\n";
cout << "</head>\n";
cout << "<body>\n";
cout << "Setting cookies" << endl;
cout << "<br/>\n";
cout << "</body>\n";
cout << "</html>\n";
return 0;
}
From this example, you must have understood how to set
cookies. We use Set-Cookie HTTP header to set cookies.
Here, it is optional to set cookies attributes like Expires,
Domain, and Path. It is notable that cookies are set before
sending magic line "Content-type:text/html\r\n\r\n.
Compile above program to produce setcookies.cgi, and try
to set cookies using following link. It will set four
cookies at your computer:
/cgi-bin/setcookies.cgi
No comments:
Post a Comment