Archive for the ‘CGI’ Category
Sometimes we have to work with servers which do not support PHP or other languages other then CGI. And we know how much email facility is important for a website!
So for this case here is the CGI emailing script which will send a detailed email.
This example uses three files.
1- One html form page that contains path to cgi script and also a hidden mail field to which mail is to be sent.
2- Configuration file which contain email address and cgi script.
3- Cgi mail script will also contain these lines which is path to sendmail and email.conf file path.
my $sendmail = “/usr/sbin/sendmail”;
my $emailConfPath = “/home/javedk/public_html/cgi-bin/email.conf”;
In emailconfpath javedk is user of server rest of thing will remain same and email.conf is the file name which is located in the cgi-bin.
Upload html in root and conf and sendMail files in cgi-bin directory. Also keep CHMOD 755 to ensure keep it excutable.
Download full example of CGI Mail Form
Here is the CGI script for complete website search. It searches all static pages and text in these pages. Great script it you have static website and just want to add a ready to use thorough search into your website. Author information of this script is in downloadable files.
Installation
Just edit these variables in pl file, upload it and make sure to set its CHMOD to 755.
As below
@ files contains ‘*.html’ and
additional old/ and javed/ tells script to search from those directories too.
This script searches text from html files.
$basedir = ‘/home/javedkh/public_html/’;
$baseurl = ‘http://www.techmynd.com/’;
@files = (’old/’,'javed/’,'*.html’);
$title = “Tech Mynd”;
$title_url = ‘http://www.techmynd.com/’;
$search_url = ‘http://www.serverpath.com/cgisearch.html’;
Download Full Script With Instructions
Here are two CGI scripts ready to use. Users online and Today’s Hits.
Always Remember with CGI
775 and 777. File executable permissions and folder data storage permissions.
Upload cgi files in ascii mode of ftp. (Transfer mode–>ASCII)
Always give files and folders executable permissions via CHMOD.
Users Online CGI Script
We use in it a file called online.cgi and an empty folder called data. Both are in cgi-bin/online/. Make a folder online in cgi-bin folder of your root directory. Place online.cgi in it with CHMOD 775. Make a folder in online dir named as data and CHMOD it to 777.
Call online.cgi by java script by this method in any webpage where you want counter to be displayed.
<script language=”JavaScript” src=”cgi-bin/online/online.cgi?output=javascript”></script>
Download online.cgi file and full example
Today Hits CGI Script
We use in it a file called today.cgi and an empty folder called data. Both are in cgi-bin/today/. Make a folder named as today in cgi-bin. Place today.cgi in it with CHMOD 775. Make a folder in today dir named as data and CHMOD it to 777.
Call today.cgi by java script by this method in any webpage where you want counter to be displayed.
<script language=”JavaScript” src=”cgi-bin/today/today.cgi?output=javascript” mce_src=”cgi-bin/today/today.cgi?output=javascript”></script>
Download today.cgi file and full example
Basics of CGI
Some basics of CGI which you must know before writing your first CGI code.
- Always Remember CHMOD that is the file permissions. Set it using your favorite ftp software. CGI script file it must be 755 and at folder which contains CGI scripts also 755 but for the folder which stores data it must be 777.
- Cgi pages use cgi-bin directory.
- The first line of many CGI scripts is: #!/usr/local/bin/perl or #!usr/bin/perl (if the script is written in Perl).
#!/usr/bin/perl -wt
or simply
#!/usr/bin/perl
- Path to sendmail is
/usr/sbin/sendmail
Use it while sending mails like this in a variable
my $sendmail = “/usr/sbin/sendmail”;
Path to perl
/usr/bin/perl
- When error occurs Cgi won’t guide you about exact error. You will have to see the error log in your hosting account.
CHMOD for cgi pages.. Must be 755.
Scripts pages permissions must be executable.
- 500 error occurs in cases of *File execution wrong permissions *First wrong interpreter line *Syntax error *Missing required file *Invalid HTTP headers sent as referred in point 5. *Wrong version of perl *Server configuration error
- The first output sent by a script must always be a valid HTTP header, i.e.
Content-type: text/html or Content-type: text/plain\n\n
The header must always be followed by two linefeeds.
Some Useful resources Links
http://www.cgi101.com/book/ch1/text.html
http://www.usersonline.net/onlinecount.shtml
http://www.tele-pro.co.uk/scripts/contact_form/contact_form.asp
http://simplythebest.net/scripts/perl_scripts/
Variable and Standard
#!/usr/bin/perl -wt
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use strict;
my $email = “someone\@server.com”;
my $url = “http://www.someurl.com”;
print header;
print start_html(”Scalars”);
print <<EndHTML;
<h2>Hello</h2><p>My e-mail address is $email, and my web url is
<a href=”$url”>$url</a>.</p>
EndHTML
print end_html;
While here is how we define strict syntax for CGI script.
#!/usr/bin/perl -w
# test.cgi by Bill Weinman [http://bw.org/]
# Copyright 1995-2003 William E. Weinman
# Free Softare: Use and distribution under the same terms as perl.
use strict;
use CGI;