value = "Physics" > Physics type = "submit" value = "Submit" /> The following C ++ program will create cpp_dropdown.cgi to handle the input provided by the web browser via the dropdown box:
#include #include #include #include #include #include #include #include #include using namespace std ; using namespace cgicc ; int main () { Cgicc formData ; cout << "Content-type:text/htmlrnrn" ; cout << "n" ; cout << "n" ; cout << "Drop Down Box Data to CGIn" ; cout << "n" ; cout << "n" ; form_iterator fi = formData . getElement ( "dropdown" ); if ( ! fi -> isEmpty () && fi != (* formData ). end ()) { cout << "Value Selected: " << ** fi << endl ; } cout << " n" ; cout << "n" ; cout << "n" ; return 0 ; } Use Cookies in CGI HTTP Protocol is a Stateless protocol. But for a commercial website, it is necessary to maintain session information between different pages. For example, a user registration process may end after completing multiple pages. But how to maintain user session information when they browse through multiple pages is.
In many situations, using Cookie is the most effective way to remember and track orders, purchases, and favorite goods .
The way it works Your server sends some data to the visitor browser in the form of a Cookie. The browser may accept this Cookie. If it accepts, it is stored in the form of a textual record on the visitor's hard drive. Now, when other users visit another page on your site, this Cookie is available for retrieval. When revoked, your server knows / remembers what is stored:
A cookie is a textual data record of 5 variable fields:
Expires : The date when Cookie expires. If left blank, Cookie expires when a visitor exits the browser.
Domain : The domain name of your site
Path : Path to the directory or Webpage that set that Cookie. It may be empty if you want to receive Cookies from any directory or page.
Secure : If this field contains the word "secure", then Cookie can only be retrieved with a Secure Server. If this field is blank, there will be no restrictions.
Name = Value : Cookies are set and retrieved in the form of key / value pairs.
Set Cookie It's easy to send Cookies to the browser. These Cookies are sent along with the HTTP Header in front of the Content-type field. Suppose you want to set UserID and Password in Cookie format. The Cookie setting will be done as follows:
#include using namespace std ; int main () { cout << "Set-Cookie:UserID=XYZ;rn" ; cout << "Set-Cookie:Password=XYZ123;rn" ; cout << "Set-Cookie:Domain=www.tutorialspoint.com;rn" ; cout << "Set-Cookie:Path=/perl;n" ; cout << "Content-type:text/htmlrnrn" ; cout << "n" ; cout << "n" ; cout << "Cookies in CGIn" ; cout << "n" ; cout << "n" ; cout << "Setting cookies" << endl ; cout << " n" ; cout << "n" ; cout << "n" ; return 0 ; } From this example, you must understand how to set up Cookies. We use HTTP Header as Set-Cookie to set the Cookies.
Here, it is optional to set Cookie properties like Expires, Domain, and Path. Remember, Cookie is set before sending the following line: " Content-type: text / htmlrnrn" .
Compile the above program to create setcookies.cgi. It will set up 4 Cookies at your computer.
Recover Cookie Recovering all set cookies is quite easy. Cookies stored in the CGI environment variable are HTTP_COOKIE and they will have the following form:
key1 = value1 ; key2 = value2 ; key3 = value3 . Below is an example of how to retrieve Cookies:
#include #include #include #include #include #include #include #include #include using namespace std ; using namespace cgicc ; int main () { Cgicc cgi ; const_cookie_iterator cci ; cout << "Content-type:text/htmlrnrn" ; cout << "n" ; cout << "n" ; cout << "Cookies in CGIn" ; cout << "n" ; cout << "n" ; cout << "" ; // get environment variables const CgiEnvironment & env = cgi . getEnvironment (); for ( cci = env . getCookieList (). begin (); cci != env . getCookieList (). end (); ++ cci ) { cout << "" << cci -> getName () << "" ; cout << cci -> getValue (); cout << "n" ; } cout << " ; cout << " n" ; cout << "n" ; cout << "n" ; return 0 ; } Now, compile the above program to create getcookies.cgi , and try getting a list of all the Cookies available on your computer.
It will give a list of 4 cookies set up in the previous section and all other Cookies set up at your computer:
UserID XYZ Password XYZ123 Domain www . tutorialspoint . com Path / perl Example of File Upload To upload a file, HTML code must have the enctype attribute set to multipart / form-data. The input tag with this file type will create a "Browse" button.