Common Gateway Interface(CGI) is one of the most popular way of creating dynamic websites. CGI is a server-side scripting language. It is an interface between The advantage of CGI is the applications can be written in any almost any language. A programmer familiar with C can create programs for CGI. It is common that Perl is usually used for CGI applications. Perl is simple and powerful language used for creating dynamic websites. It is not a strictly interpreted language.
In this post I am going to share my little experience with CGI programs using C in Linux platform( with root privileges). Not going too deeply into CGI or C, how it works?... etc. Just a demonstration
First of all you need a Apache server. Check is it available in your system and make sure that it is running. If it is available then just follow the steps below to start the server.
Select System->Administration->Server settings->Services
A window appears select Select Background services tab
from the list select httpd and click start or in terminal you can type the following commands:
To start the server, as root type
httpd start
To restart the server, as root type
httpd restart
To stop the server, as root type
httpd stop
To check Apache is running or not type the url http://127.0.0.1 in your default browser. If the Apache is running then the following page will appear.
Now write the following C program
#include<stdio.h>
main()
{
char env[30][30]={{"SERVER_NAME"},{"SERVER_PROTOCOL"},{"SERVER_SOFTWARE"},{"AUTH_TYPE"}};
int i;
printf("content-type:text/html\n\n");
printf("<html><body><table border=3>");
printf("<tr><th>Environment varaible</th><th>Descriptiom</th>");
for(i=0;i<4;i++)
{
printf("<tr><td>");
printf("%s",env[i]);
printf("</td><td>");
printf("%s",getenv(env[i]));
printf("</td></tr>");
}
printf("</table></body></html>");
}
compile the code
gcc <programname>.c
After compilation it would have .c extension. We need script file of extension .cgi so use the command gcc <programname>.c -o <scriptname>.cgi
Now write the following html.
<html>
<body>
<form method=GET action="http://127.0.0.1/cgi-bin/Env.cgi">
<input type="submit" value="Environment variables">
</form>
</body>
</html>
Move to /var/www/. In www contains cgi-bin directory,in which you should move your compiled CGI script files, and html directory in which you move the html file you just created.Load the html document using the browser.
See the working of the application:
In the above shown html page on clicking the Environment variable button will show some Environment variables
Reference:
Thank you for reading
No comments:
Post a Comment