Sunday, February 09, 2014

Creating Website using C programming Language - read|more at i-visionblog.com

C program is a powerful programming language.number of web interpreter languages and database are built by this C language.even unix,Most of Windows and other basic network are driven by the C language.we can't imagine without c language and thanks to Dennis.So,we are going to built a simple website using c program and compile using GCC compiler and execute as CGI using Apache server.Thanks to Shuvojit Sarkar - programmer for configuring my linux environment successfully.

Requirements:

Apache server 2+ version is most preferable(Apache 2.2.22)
GCC compiler 
Text Editor :D

configure Apache server:

Assuming you have already Apache server running or follow below instructions
 sudo apt-get install apache2 

after installation try to open your browser and visit your localhost and it should show the message Working!


You will need to use the CGI interface of Apache 2 webserver. So first find out the path for the cgi-bin directory. You can see it in the /etc/apache2/sites-available file where it mentions:
   ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

   ErrorLog ${APACHE_LOG_DIR}/error.log
Finally it should look like above.If you dont have just try to copy it and configure it.

check now http://localhost/cgi-bin/ works without any errors,i mean with error message of Forbidden

Now let us code C language to run in Web browser.
there are two ways to running C program to run in web browser via apache server.
1)the first way is to directly code the html codes in printf(); function which would be tough in huge file of html.
2)the second way is to create txt file and using file operation to import the text file and read it using the loop concept and generate html codings.
hope you got the clear idea on this.

Code easily:

 create a C file using text editor.

#include <stdio.h>
int main()
{
    printf ("Content-Type: text/html\n\n");
    printf ("<html>");
    printf ("<head><title>C-Html sync</title></head>");
    printf ("<body>");
        printf ("<img src=\"https://lh5.googleusercontent.com/-kTNnF876PYk/AAAAAAAAAAI/AAAAAAAAAus/QLD7dahdifE/s120-c/photo.jpg \"");
     printf ("This page is created by a C program.<br />");
    printf ("I love to share my ideas to the world <strong>bold</strong> and <em>italic</em> text.");
    printf ("</body> </html>");
    return 0;
}

we had created the C file and time to compile now using GCC compiler.
change it to the present working directory for compiling.

$ cd <preferable directory>

compile and it should be successful compilation.

$ gcc file.c

just run your output and see .

$ ./a.out

have a look at my screenshot for visual reference.
after successful compilation now we are in the stage to create application/executable that runs on apache server in the cgi-bin directory.

$ gcc -o <your preferred executable name> <space><c program file name>

eg: $ gcc -o index file.c

now it's time to test your file using cgi-bin directory

$ sudo cp index /usr/lib/cgi-bin

this prompt for root password and copy the file to cgi-bin directory using sudo cpy command.

open your browser test your file as 

http:/localhost/cgi-bin/index   - awsome awaiting for you!


so the above method is some difficult to code huge html page.so, i have decided to try file operation in c programming.

steps:

i have coded the normal <html> page and saved as input.txt file in my own dorectory.
next step is i had made the c program to load the input file and read the file line by line and create output as html tags.

have look at c program coding:

#include<stdio.h>
  int main()
    {
            FILE *ptr_file;
            char blog[1000];
printf ("Content-Type: text/html\n\n");
            ptr_file =fopen("input.txt","r");
            if (!ptr_file)
                return 1;

            while (fgets(blog,1000, ptr_file)!=NULL)
                printf("%s",blog);

        fclose(ptr_file);
            return 0;
    }

the file operation opens the file input.txt in read mode and we have declared ptr_file as pointer and blog character array as 1000.since we are going to read the file line by line and just executing printf statement ,assuming that i dont have more than 1000 characters in single line.and followed by verification of file existance ans using while loop and condition is end of the line must have a null character or new line character and stopping there and thus moves to next line and proceeds till the file is completely read.And we close the file.

thus just code your input.txt file with html codings on your way.
my coded input.txt

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Shivasurya iniatives</title>
<style type="text/css">
.nav {
    width: 100%;
    float: left;
    margin: 0 0 3em 0;
    padding: 0;
    list-style: none;
    background-color: #f2f2f2;
    border-bottom: 1px solid #ccc;
    border-top: 1px solid #ccc; }
.nav li {
    float: left; }
.nav li a {
    display: block;
    padding: 8px 15px;
    text-decoration: none;
    font-weight: bold;
    color: #069;
    border-right: 1px solid #ccc; }
.nav li a:hover {
    color: #c00;
    background-color: #fff; }
</style>

</head>

<body style=" margin:0px;background-color:#FFE7E7;">
<div id="s_01" style=" height:120px; width:100%; background-color: #900;">
<img src="http://www.tce.edu/sites/default/files/header.png" style="margin-left:100px;"  />
</div>
<div id="s_02" style="width:100%;">
<ul class="nav" style="width:100%;"><li class="first leaf"><a href="/" title="" class="active" style="margin-left:110px; ">Home</a></li>
<li ><a href="/about" title="">About TCE</a></li>
<li><a href="/Academics" title="">Academics</a></li>
<li><a href="/admission" title="">Admission</a></li>
<li><a href="/departments" title="">Departments</a></li>
<li><a href="/Research" title="">Research</a></li>
<li><a href="/contact" title="">Contact Us</a></li>
</ul>
</div>
<div id="main">
<img src="http://www.tce.edu/sites/default/files/banner/wrappergreen.jpg" style="width:100%; margin-top:-50px;" />
</div>

<div id="r1" style="height:auto; margin:20px;font-size:80%;">
<div id="m0" style="height:30px; width:350px;background-color: #FF5050;line-height: 30px;">&nbsp;&nbsp;&nbsp;<b>About</b></div>
<div id="m1" style="height:100px; width:350px;background-color: #FFADAD;">
<div style="margin-left:10px;">
<div style="height:33px;line-height: 33px;">
Founder: <b>Karumuttu Thiagarajan Chettiar</b>
</div>
<div style="height:33px;line-height: 33px;">
Located in <b> Thiruparankundram,Madurai</b> </div>
<div style="height:33px;line-height: 33px;">
Establishment Year: <b>1957 </b></div>
</div>
</div>
</div>
</body>
</html> <! shivasurya productions !>

and now create the application/executable file using compilation process.

gcc -o mainhtml file.c

now copy the both executable and input.txt file to cgi-bin using sudo commands.

$ sudo cp mainhtml input.txt /usr/lib/cgi-bin

verify using $ echo $?   must return 0 for success.

now it's time to check the website.go to http://localhost/cgi-bin/mainhtml

my output based on input.txt file :

using c program we could get query and combine database functionality using mysql C API.i will describe in detail in my next post.
feel free to share and comment guys!find out bugs if any an notify me via comments or contact by [email protected]



17 comments:

Anonymous said...

Technically speaking, you didn't "create a website using C Programming Language," what you're doing is called Spaghetti/Macaroni code. You're creating a website with CGI/HTML; embedding HTML codes in a C program and having the program "spitting" it into a file or standard output is not "creating website using C PL," so convince yourself it is. Anyway, what's its use in production today, is the resulting code 2x faster than a conventional "write a html file, save it with a .html extension" way of creating a website?

shivasurya said...

so , as technically speaking you cant do database manipulation using html since it is a mark up language . and this is a start up i am going to introduce C API mysql
so i have given basic make up site using c lang and to proccess using db so it might be kidding for u ... c my future post ahaed. about mysql C API that connects Clang -HTML-Mysql Database

Shaikh Mohd Faizan said...

How to connect mysql database and execute query using C language

shivasurya said...

Forbidden error came
you dont have permission to access /cgi-bin/ on this server

shivasurya said...

Amazing blog post. Keep it up good work.

shivasurya said...

This is a great blog, It is very easy to understand to readers and I want more unique techniques in this topic...
Social Media Marketing Courses in Chennai
Social Media Marketing Training in Chennai
Oracle Training in Chennai
Tableau Training in Chennai
Pega Training in Chennai
Embedded System Course Chennai
Oracle DBA Training in Chennai
Social Media Marketing Courses in Chennai
Social Media Marketing Training in Chennai

shivasurya said...

best

shivasurya said...

best

shivasurya said...

Websites require maintenance and will need occasional updates in order to keep functioning optimally. However, having a website built from the ground up is only the first step. As your site grows, you'll need to update it with new content, re-design pages for various devices, and make sure that your site is fully optimized for search engines. Online Marketing Help is one of the best Wordpress Development Company in the UK.

shivasurya said...

wow its going to help I think

shivasurya said...

satisfied

shivasurya said...

Excellent read, Positive site, I have read a few of the articles on your website now, and I really like your style.

ecommerce services provider in india

ecommerce catalogue management services

shivasurya said...

Planning to move locally inside city We Know Carrying goods carefully and Safely From One place to other place is a difficult part, No worries we have professional and trained employees will take care.Moving to other states or cities is made simple by Bigguymover’s low-cost domestic packing and moving services in INDIA. Relocation Services by Bigguymover
Packers and movers bangalore near me
Packers and movers bangalore online
Top packers and movers in bangalore
Packers and Movers bangalore price
Top 5 packers and movers in bangalore

shivasurya said...

Thanks
Artical is very informative.
https://pro4pc.org/4ukey-crack-download/
https://cracka2zsoft.org/ccleaner-crack/

shivasurya said...

https://premiumfilez.org/light-image-resizer-crack/

shivasurya said...

aomei partition assistant pro crack

shivasurya said...

great post on Creating Website , keep posting interesting and helpful information to us Salesforce Training In Pune

Post a Comment

feel free to post your comments! Don't Spam here!