View Full Version : cgi with c???
fady911x
hi all
i have a simple knowledge abt programming with c under linux
u know i'm still learning
so i want 2 ask abt how 2 use c in cgi programming
and which is better 4 cgi
c or perl or python???
thanx 4 ur time
habdin
Salamo 3alaikom,
I know that this might look silly but I think you are making the same typing mistake twice. The Programming language is Python and not paython. I think may be you are still internally convinced that it is Paython. Correct it please. Please read alaa's post in paython and perl once again. Do correct your typing mistakes please.
Thanks.
Salam.
fady911x
7'alas ya sedy wala tez3al
python
python
python
python
python
python
python
python
python
python
.....
wala a2olak
#include <stdio.h>
int main()
{
int c=1;
while ( c != 1000000)
{
printf("python\n");
c++;
}
}
7'alas keda erta7t
:D :D :D :D :D :D
habdin
Salamo 3alaikom,
Helw elprogram dah. Da waleed ellahza? :D.
Salam.
fady911x
ay 7'ed3a
;) :D :D :D
alaa
which language is more suitable has more to do with you than with the language itself.
however sometimes it is difficult or imposible to compile C/C++ programms for your host (or the hosting company might not allow compiled CGI programs) in that case using an interpreted language makes sense.
C/C++ have an advantage that Python and Perl lack, true portability, while Python and Perl work on many environments they are very mutable languages (specially perl) each new release breaks older code to the point where you get obscure syntax error from scripts that worked perfectly fw weeks ago.
since you probably don't control what is installed on the hosting server this could quickly become a problem (just a few days ago me and Uniball where working on a perl script, each one of us had a different version of perl and our server had yet another version).
my personal favorite would be Python, but perl's install base is much wider.
cheers,
Alaa
MadFarmAnimalz
Originally posted by fady911x
#include <stdio.h>
int main()
{
int c=1;
while ( c != 1000000)
{
printf("python\n");
c++;
}
}
Nice program, but you don't have a terminating condition for your while loop.
Unless I'm getting my bits, bytes, and ints terribly mixed up, then what you have is an infinite loop; max value of a positive signed int is 2^16 / 2, so after 32,768 iterations you begin to get negative numbers as values for variable c.
Hence, you get an infinite loop since the terminating condition is never met.
But then, taking your program in context, I think an infinite loop printing the proper spelling of paython (excuse me, I mean python!) would be more appropriate. :-)
whirlpool
Originally posted by alaa
... very mutable languages (specially perl) each new release breaks older code to the point where you get obscure syntax error from scripts that worked perfectly fw weeks ago.
since you probably don't control what is installed on the hosting server this could quickly become a problem (just a few days ago me and Uniball where working on a perl script, each one of us had a different version of perl and our server had yet another version).
my personal favorite would be Python, but perl's install base is much wider.
cheers,
Alaa
I think Alaa means PHP and not Perl ? Even If Alaa means perl. CHEAP local PHP hosting is everywhere.
SYStems
#include <stdio.h>
int main()
{
int c=1;
while ( c != 1000000)
{
//printf("python\n");
c++;
}
printf(" %d\n sizeof int= %d bytes\n ", c, sizeof(int));
return 0;
}
[ ... @debian:c]$ gcc -o infiloop infiloop.c
[ ... @debian:c]$ ./infiloop
1000000
sizeof int= 4 bytes
most processor we use nowadays are 32 bit processor, so integers are 4 bytes
32 bit == 4bytes (32 / 8)
which qualify us to do
#include <stdio.h>
int main()
{
int c=1;
while ( c != 1000000)
{
printf("MFA got beaten by a newbie_fady911x out coded MFA_lalalaaaa_MFA got his bits, bytes, and ints terribly mixed up_hakoona matata_ ");
c++;
}
printf(" %d\n sizeof int= %d bytes\n ", c, sizeof(int));
return 0;
}
MadFarmAnimalz
Originally posted by SYStems
#include <stdio.h>
int main()
{
int c=1;
while ( c != 1000000)
{
//printf("python\n");
c++;
}
printf(" %d\n sizeof int= %d bytes\n ", c, sizeof(int));
return 0;
}
[ ... @debian:c]$ gcc -o infiloop infiloop.c
[ ... @debian:c]$ ./infiloop
1000000
sizeof int= 4 bytes
most processor we use nowadays are 32 bit processor, so integers are 4 bytes
32 bit == 4bytes (32 / 8)
which qualify us to do
#include <stdio.h>
int main()
{
int c=1;
while ( c != 1000000)
{
printf("MFA got beaten by a newbie_fady911x out coded MFA_lalalaaaa_MFA got his bits, bytes, and ints terribly mixed up_hakoona matata_ ");
c++;
}
printf(" %d\n sizeof int= %d bytes\n ", c, sizeof(int));
return 0;
}
hahahaha
Erm. Nope. I code on a Commodore 128. 8 bit machine.
:p
alaa
Originally posted by whirlpool
I think Alaa means PHP and not Perl ? Even If Alaa means perl. CHEAP local PHP hosting is everywhere.
both PHP and Perl mutate all the time.
PHP is even worse, from one the release to another suddenly the language has some static typing !!!!
PHP is the most horible language ever even Basic is more consistant.
it is even configurable, when programming for PHP not only do you have to keep track of the PHP version your host is using but also the compile time options they used and their PHP and Apache configuration (imagine having an option that changes the quoting rules of the language??? even VB is better than PHP).
cheers,
Alaa
angoranimi
Originally posted by alaa
both PHP and Perl mutate all the time.
PHP is even worse, from one the release to another suddenly the language has some static typing !!!!
PHP is the most horible language ever even Basic is more consistant.
it is even configurable, when programming for PHP not only do you have to keep track of the PHP version your host is using but also the compile time options they used and their PHP and Apache configuration (imagine having an option that changes the quoting rules of the language??? even VB is better than PHP).
cheers,
Alaa
If you recall PHP3, there were so many bugs in the code it was not funny. I know alot of people who had to fix the bugs themselves to get some php feature to work. Now, PHP4 is ALOT more stable, and personally, I think it is an extremely powerful and flexible language.
Perhaps this configuration is an issue to you only because you end up having to use the configuration imposed by your hosting service. And I agree, installing it with the required options can be kind of tedious... but thats how it is for the most flexible setups (take the kernel configuration for example). The PHP extensions are also really powerful, take a look at pheonix msession extensions or mailparse functions to see what I mean.
Once its up and running, you have one monster of an engine interpretting payloads of port 80 packets.
alaa
a language is a language it should be used to describe our thoughts and communicate them to other human beings and the machines that run our code.
if a language is configurable to the point where syntax changes then it is no longer a language it is just a tool.
there is hardly an inflexible programming language, PHP is not flexible its chaotic, you said it yourself, they had to change the LANGUAGE between version 3 to 4 (I'm not talking about fixing the interpreter here, its cchanging the language rules, scope rules and syntax), it is not just adding stuff to the language (thats natural) its changing it completley.
as for PHP extensions being powerful whats new, Python libraries and extensions are powerful, perl's libraries are powerful, Java libraries are powerful, the only new thing that PHP brought to the formula is embedding code inside HTML, which is IMO a Very Bad Idea, its the Wrong Thing(TM), it makes hard to seperate logic from interface, it makes reading the code an excercise in bilingual sight reading.
its a lousy language and I hope it doesn't survive much longer (let it go the way of the COBOL, or at least the way of the BASIC where it belongs).
angoranimi
Not really. If you know how to code neatly in PHP, the logic and the HTML clearly stand out. Take this for example:
-----------------------
<table>
<?
$query = db_query ("SELECT * FROM product");
while ($row = db_fetch_assoc($query)) {
?>
<tr>
<td><%= $row[id] %></td>
<td><%= $row[prod_name] %></td>
<td><%= $row[prod_price] %></td>
</tr>
<?
}
?>
</table>
-----------------------
Personally, I don't see how this is not readable (the code should be indented, but this will not be visible on this forum). Ofcourse, if you are not a neat coder it will end up looking like a piece of assembly code, where even the author can find it hard to understand the logic if he leaves the code for some time. In my opinion, coding, say, this particular table in perl or python or whatever will be alot less readable. I really doubt PHP will face its demise anytime soon... atleast not during our lifetime.
habdin
Salamo 3alaikom,
I hope to God we won't see a flamewar concerning PHP and other languages here, I hope that we just enumerate the advantages and disadvantages of each language without getting angry of each other's point of views.
Salam
uniball
Originally posted by angoranimi
Not really. If you know how to code neatly in PHP, the logic and the HTML clearly stand out. Take this for example:
-----------------------
<table>
<?
$query = db_query ("SELECT * FROM product");
while ($row = db_fetch_assoc($query)) {
?>
<tr>
<td><%= $row[id] %></td>
<td><%= $row[prod_name] %></td>
<td><%= $row[prod_price] %></td>
</tr>
<?
}
?>
</table>
-----------------------
Personally, I don't see how this is not readable (the code should be indented, but this will not be visible on this forum). Ofcourse, if you are not a neat coder it will end up looking like a piece of assembly code, where even the author can find it hard to understand the logic if he leaves the code for some time. In my opinion, coding, say, this particular table in perl or python or whatever will be alot less readable. I really doubt PHP will face its demise anytime soon... atleast not during our lifetime.
Execuse me! but i really find it harded to read it than to read the perl code below!
my $query = qq!
SELECT * FROM product
!;
my $sth = $dbh->prepare($query) || die($DBI::errstr);
$sth->execute() || die($DBI::errstr);
my $i = $sth->rows;
my $x = 0;
my $texttoprint = "<table>";
for ($x; $x < $i; $x++)
{
my ($id, $name, $price) = $sth->fetchrow_array;
$texttoprint .= "<tr><td>$id</td><td>$name</td><td>$price</td></tr>";
}
$texttoprint .= "</table>";
alaa
first of all this is mixing interface with logic, they happen to be on the same file.
and this example proves nothing since an SQL query is hardly the application's logic, the logic is algorythms (which may include SQL) and the interaction between seperate components/modules.
it is still a pain to have real seperation between logic, implementation details and interface in PHP.
it can be done, some project follow very good frameworks and guidelines, and have good seperation most notably the PHPGroupware (web application portion of the dotGNU project), however they utilise framworks, glue libraries (libraries to make all DB behave in the same way etc.), most if not all of the rules are not imposed by the language and it is very difficult to maintain.
contrast this with the great work being done in the GNU enterprise project using Python and you'll see what I mean.
as for readability, I didn't say it was not readable, my complaints was that you have to read two languages at the same time (even more if you add SQL, CSS).
with a decent language you can abstract all this away, there is a C++ CGI library that makes producing dynamic web pages feel like writing GUI code.
RE: Habdin
why not I LOVE FLAME WARS
angoranimi
Originally posted by uniball
Execuse me! but i really find it harded to read it than to read the perl code below!
my $query = qq!
SELECT * FROM product
!;
my $sth = $dbh->prepare($query) || die($DBI::errstr);
$sth->execute() || die($DBI::errstr);
my $i = $sth->rows;
my $x = 0;
my $texttoprint = "<table>";
for ($x; $x < $i; $x++)
{
my ($id, $name, $price) = $sth->fetchrow_array;
$texttoprint .= "<tr><td>$id</td><td>$name</td><td>$price</td></tr>";
}
$texttoprint .= "</table>";
uniball, now, if we compare the resulting HTML by, for example, hitting '\\' on lynx (equivilant of view source on browsers), you will see how ugly your html will be. Lack of indentation, etc, etc. You will have to get around those by '\t' or the like, which in this particular case is unnatural. In my example, the HTML code does not even pass through the zend parser. In your case, you have Larry manually echo'ing every HTML line... Dirty. :)
joe_lite
hi dear
i think the question was about cgi in c
so i wanna say if u can compile on ur host then these r short notes
1- consider the input is comming from stdin(standard input) and u write 2 stdout
2- all the server and browser agent attributes r stored in environment variables
example
the type of the client browser is stored by the web server in the environment variable HTTP_USER AGENT so to get it
char* browser = getenv(HTTP_USER_AGENT);
and all the form variables passed from the client browser stored as one string and stored in the environment variable QUERY_STRING so u can access it the same way
these r the main steps and if u really concerned and serious about cgi with c contact me for details and more examples
joe
vBulletin v3.0.1, Copyright ©2000-2004, Jelsoft Enterprises Ltd.