-
MySQL connection script using MySQLPP
Code:
#!/usr/bin/perl
print "Content-type: text/html\n\n";
use DBI;
my $database;
$database = 'ecakti';
$option1 = 'localhost';
$option2 = 'ecakti';
$option3 = 3306;
my $password;
$password = 'tjm2244';
my $dbh = eval {
DBI->connect(
"dbi:mysqlPP:database=$database;hostname= 6;option1;port=$option3",
$option2, $password, {
RaiseError => 1, PrintError => 0
});
};
die $DBI::errstr if $@;
print "Connected to database sucessfully";
-
CGI script for testing MySQL connection
Code:
#! /usr/bin/perl
use DBI;
use strict;
my $dbh;
print "Content-type: text/html \n\n";
$dbh = DBI->connect('DBI:mysql:genepilotReg','bre ttm','ba35se'
||print "Cannot Connect" . $dbh->errstr . "&br&";
$dbh->disconnect;
print "done";
-
Test JSP script
Place the following code in the html folder with the permissions 0640 and as hello.jsp:
Requirements
File: /hello.jsp
Permissions: 0640
Code:
<%
String title = "hello.jsp";
%>
<HTML>
<HEAD>
<TITLE><%= title %></TITLE>
</HEAD>
<BODY>
<H1><%= title %></H1>
JSP example
</BODY>
</HTML>
The Output should be as follows:
hello.jsp in the Title bar and body section
and
JSP example in the body section
-
PHP Mail
Mail
(PHP 3, PHP 4 )
Mail -- send mail
Description
bool mail ( string to, string subject, string message [, string additional_headers [, string additional_parameters]])
mail() automatically mails the message specified in message to the receiver specified in to. Multiple recipients can be specified by putting a comma between each address in to. Email with attachments and special types of content can be sent using this function.
mail() returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.
Example 1: Sending mail.
Code:
mail("joecool@example.com", "My Subject", "Line 1\nLine 2\nLine 3");
If a fourth string argument is passed, this string is inserted at the end of the header. This is typically used to add extra headers. Multiple extra headers are separated with a carriage return and newline.
Note: You must use \r\n to seperate headers, although some Unix mail transfer agents may work with just a single newline (\n).
Example 2 : Sending mail with extra headers.
Code:
mail ("nobody@example.com", "the subject", $message,
"From: webmaster@$SERVER_NAME\r\n"
"Reply-To: webmaster@$SERVER_NAME\r\n"
"X-Mailer: PHP/" . phpversion());
The additional_parameters parameter can be used to pass an additional parameter to the program configured to use when sending mail using the sendmail_path configuration setting. For example, this can be used to set the envelope sender address when using sendmail. You may need to add the user that your web server runs as to your sendmail configuration to prevent a 'X-Warning' header from being added to the message when you set the envelope sender using this method.
Example 3 : Sending mail with extra headers and setting an additional command line parameter.
Code:
Mail( "nobody@example.com", "the subject", $message,
"From: webmaster@$SERVER_NAME", "-
fwebmaster@$SERVER_NAME" );
Note: This fifth parameter was added in PHP 4.0.5. Since PHP 4.2.3 this parameter is disabled in safe_mode and the mail() function will expose a warning message and return FALSE if you're trying to use it.
You can also use simple string building techniques to build complex email messages.
Example 4 : Sending complex email.
Code:
/* recipients */
$to = "Mary <mary@example.com>" . ", " ; // note the comma
$to .= "Kelly <kelly@example.com>";
/* subject */
$subject = "Birthday Reminders for August";
/* message */
$message = '
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
Here are the birthdays upcoming in August!</p>
<table>
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Joe</td><td>3rd</td><td>August</td><td>2004</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html> ';
/* To send HTML mail, you can set the Content-type header. */
$headers = "MIME-Version: 1.0\r\n";
$headers = "Content-type: text/html; charset=iso-8859-1\r\n";
/* additional headers */
$headers = "From: Birthday Reminder <birthday@example.com>\r\n";
$headers = "Cc: birthdayarchive@example.com\r\n";
$headers = "Bcc: birthdaycheck@example.com\r\n";
/* and now mail it */
mail($to, $subject, $message, $headers);
Note: Make sure you do not have any newline characters in the to or subject, or the mail may not be sent properly.
Note: The to parameter cannot be an address in the form of "Something <someone@example.com>". The mail command will not parse this properly while talking with the MTA.
-
PHP-Mysql for localhost
Code:
<?php
$host=?localhost?; // You can use IP address instead of localhost
$user=?Your Username?;
$password=?Your Password?;
$database=?Your Databasename?;
$Link_ID=mysql_pconnect($host, $user, $password);
if (!$Link_ID)
{
echo "Failed to connect to Server=".$host;
return 0;
}
else
{
echo "Successfully to connected to Server=".$host;
}
if (!@mysql_select_db($database,$Link_I D))
{
echo "
Cannot use database=".$database;
}
else
{
echo "
Successfully connected to database=".$database;
}
?>
-
To test the perl modules installed on the server you can use the following code and place it in the cgi-bin of the domain.
Code:
#!/usr/bin/perl -w
#
#************************************************* **********************
# PURPOSE: Freeware Perl script to display some server-side information
# including environment variables and installed modules.
#
# DATE: 21 August, 2002
# VERSION: 0.2.1
#
# LICENSE:
# Copyright © Shashank Tripathi (shanx@shanx.com)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
#
#================================================= ======================
# I N S T A L L A T I O N:
#-----------------------------------------------------------------------
#
# 1. Copy and paste all this text into a file, e.g., perldigger.cgi
# 2. Upload this file into your CGI-BIN or an equivalent CGI directory.
# 3. CHMOD the file to 755 so it can be executed.
# 4. It is common for Perl to reside on most servers at "/usr/bin/perl"
# but if it doesn't, please replace the first line of this program
# with the appropriate path (e.g., "/usr/local/bin/perl").
#
# That should be all. If you have any problems, please feel free to
# write to me at shanx@shanx.com.
#
#************************************************* **********************
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
print "Content-type: text/html\n\n";
# You can set the page title here
$PAGE_TITLE = "PERL DIGGER °| Free CGI script to list environment information and installed Perl modules";
#-----------------------------------------------------------------------
# NO NEED TO TOUCH ANYTHING BEYOND THIS POINT
#================================================= ======================
#Location of Perl
$whereperl = join("
", split(/\s+/, qx/whereis perl/));
#Location of Sendmail
$wheresendmail = join("
", split(/\s+/, qx/whereis sendmail/));
#Location of Current Directory
$currentdirectory = `pwd`;
# List of processes
$processes = qx/ps aux/;
$processes =~ s/
/\n/gi;
$processes =~ s/
/\n\n/gi;
$processes =~ s/<(?:[^>'"]*|(['"]).*?\1)*>//gs;
#Perl Variables
$perlversion = $];
$path_tar = join("
", split(/\s+/, qx/whereis tar/));
$path_gzip = join("
", split(/\s+/, qx/whereis gzip/));
$path_apache = join("
", split(/\s+/, qx/whereis apache/));
$path_httpd = join("
", split(/\s+/, qx/whereis httpd/));
$path_php = join("
", split(/\s+/, qx/whereis php/));
$path_mysql = join("
", split(/\s+/, qx/whereis mysql/));
$path_man = join("
", split(/\s+/, qx/whereis man/));
$path_perldoc = join("
", split(/\s+/, qx/whereis perldoc/));
#Perl Os
$perlos = $^O;
$perlos_version = get_server('version'
;
$perlos_version =~ s/#/
#/s;
$perlos_version =~ s/\(/
(/s;
$perlos_cpu = get_server_detail('cpuinfo'
;
$perlos_mem = get_server_detail('meminfo'
;
$perlos_mem =~ s/^.*?\n.*?\n.*?\n//s;
$perlos_dsk = `df`;
sub get_server
{
open PROC, "</proc/$_[0]" || &error("Cannot read proc [/proc/$_[0]]", $!
;
my $res = join("
", <PROC>);
close PROC;
return $res ? $res : undef;
}
sub get_server_detail
{
open PROC, "</proc/$_[0]" || &error("Cannot read proc [/proc/$_[0]]", $!
;
my $res = join("", <PROC>);
close PROC;
return $res ? $res : undef;
}
#Module Paths
foreach $line (@INC)
{
$modulepaths .= "$line
";
}
#Environment Variables
$environment = qq~
<table width="69%" align="center" cellspacing="0" cellpadding="4" bordercolor="#c5c5c5">
<tr>
<td colspan="2" bgcolor="#efefef" class="tabletitle">ENVIRONMENT VARIABLES <font style="font-family: Webdings; font-size: 15px; text-decoration: none">5</font> </td>
</tr>
~;
$PAGE_FOOTER = "Powered by | Copyright © <a href='http://shanx.com'>Shashank Tripathi ";
@allkeys = keys(%ENV);
foreach $key (@allkeys)
{
$value = $ENV{$key};
if ($value eq "") {$value = "-";}
$environment .= qq~
<tr>
<td width="168" class="tableitems">$key</td>
<td class="tablevalue">$value</td>
</tr>
~;
}
$environment .= qq~
</table>
~;
$documentroot = $ENV{'DOCUMENT_ROOT'};
if ($documentroot ne "")
{
@lines = `du -c -k $documentroot`;
$lastline = @lines-1;
($diskusage) = split/[\t| ]/,$lines[$lastline];
}
#Server Software
$serverip = $ENV{'SERVER_ADDR'};
$servername = $ENV{'SERVER_NAME'};
$serverport = $ENV{'SERVER_PORT'};
$serversoftware = $ENV{'SERVER_SOFTWARE'};
$serveruptime =`uptime`;
#Localtime
($sec,$min,$hour,$mday,$m on,$year,$wday,$yday,$isdst) = localtime(time);
@months = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug"," Sep","Oct","Nov","Dec");
$date = sprintf("%02d-%s-%04d",$mday,$months[$mon],$year+1900);
$time = sprintf("%02d:%02d:%02d",$hour,$min, 6;sec);
$localtime = "$date, $time";
#GMTtime
($sec,$min,$hour,$mday,$m on,$year,$wday,$yday,$isdst) = gmtime(time);
@months = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug"," Sep","Oct","Nov","Dec");
$date = sprintf("%02d-%s-%04d",$mday,$months[$mon],$year+1900);
$time = sprintf("%02d:%02d:%02d",$hour,$min, 6;sec);
$gmttime = "$date, $time";
print qq~
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>$PAGE_TITLE</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta http-equiv="PICS-Label" content=' (PICS-1.1 "http://www.gcf.org/v2.5" labels on "2001.11.05T08:15-0500" until "1995.12.31T23:59-0000" for "http://w3.org/PICS/Overview.html" ratings (suds 0.5 density 0 color/hue 1))'>
<meta name="author" lang="en" content="Shashank Tripathi">
<meta name="title" content="$PAGE_TITLE">
<meta name="copyright" content="© Shashank Tripathi, 1998 - 2010. All rights reserved.">
<meta name="revisit-after" content="1 week">
<meta name="description" content="Perl Digger is a freeware Perl script to dig for information about the server-side environment including a list of all installed Perl modules with a handy link to their documentation at CPAN">
<meta name="keywords" content="Perl digger, Perl info, server-side free Perl / CGI script to show environment variables and all installed Perl modules, Shashank Tripathi">
<style type="text/css">
<!--
h1 {font-family: Arial, Helvetica, sans-serif; font-size: 18px; font-weight: bold; }
.tabletitle { font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight: bold; height: 22px; }
.tableitems { font-family: Arial, Helvetica, sans-serif; font-weight: bold; font-size: 11px; border-bottom: 1px #efefef solid;}
.tablevalue { font-family: Arial, Helvetica, sans-serif; font-size: 11px; border-bottom: 1px #efefef solid;}
a { color: #3366ff; }
a:hover { text-decoration: none; color: #ff6600;}
td { font-family: Arial, Helvetica, sans-serif; font-size: 11px }
table { border: 1px #c5c5c5 solid; background-color: #fff }
-->
</style>
</head>
<body bgcolor="#FFFFFF" text="#000000" background="http://sniptools.com/av/bg.gif"><a name="top"></a>
<table width="69%" align="center" cellpadding="4" cellspacing="0">
<tr><td height="51" width="168" valign="top" background="http://sniptools.com/av/bg_grey.gif"><h1 style="margin-bottom: 3px">Perl Digger</h1>
Get Perl code here</td>
<td valign="middle" background="http://sniptools.com/av/bg_grey.gif"><b style="font-size: 12px">Perl Digger is a freeware Perl script to dig for information
about the server-side environment including a list of all installed Perl modules
with a handy link to their documentation at CPAN. This page is based on easy CSS so you can easily change
the look if you wish.
[/b] </tr>
<tr>
<td colspan="2" height="60" align="center" valign="middle">
PERL INFORMATION
| SERVER INFORMATION
| ENVIRONMENT VARIABLES
| INSTALLED PERL MODULES
</td>
</tr>
</table>
<a name="perlinfo"></a>
<table width="69%" align="center" cellpadding="4" cellspacing="0">
<tr bgcolor="#efefef">
<td colspan="2" class="tabletitle">
PERL INFORMATION <font style="font-family: Webdings; font-size: 15px; text-decoration: none">5</font>
</td>
</tr>
<tr>
<td class="tableitems" width="168" valign="top">Perl version</td>
<td class="tablevalue">$perlversion</td>
</tr>
<tr>
<td class="tableitems" width="168" valign="top">Perl</td>
<td class="tablevalue">$whereperl</td>
</tr>
<tr>
<td class="tableitems" width="168" valign="top">Sendmail</td>
<td class="tablevalue">$wheresendmail</td>
</tr>
</table>
<a name="serverinfo"></a>
<table width="69%" align="center" cellspacing="0" cellpadding="4" bordercolor="#c5c5c5">
<tr bgcolor="#efefef">
<td colspan="2" class="tabletitle">
SERVER INFORMATION <font style="font-family: Webdings; font-size: 15px; text-decoration: none">5</font>
</td>
</tr>
<tr>
<td class="tableitems" width="168" valign="top">Name</td>
<td class="tablevalue">$servername</td>
</tr>
<tr>
<td class="tableitems" width="168" valign="top">IP</td>
<td class="tablevalue">$serverip</td>
</tr>
<tr>
<td class="tableitems" width="168" valign="top">Listing Port</td>
<td class="tablevalue">$serverport</td>
</tr>
<tr>
<td class="tableitems" width="168" valign="top">Document Root</td>
<td class="tablevalue">$documentroot</td>
</tr>
<tr>
<td class="tableitems" width="168" valign="top">Disk Usage by Root</td>
<td class="tablevalue">$diskusage KB</td>
</tr>
<tr>
<td class="tableitems" width="168" valign="top">Server stamp</td>
<td class="tablevalue">$serversoftware</td>
</tr>
<tr>
<td class="tableitems" width="168" valign="top">Server Time (Local)</td>
<td class="tablevalue">$localtime</td>
</tr>
<tr>
<td class="tableitems" width="168" valign="top">Server Time (GMT)</td>
<td class="tablevalue">$gmttime</td>
</tr>
<tr>
<td class="tableitems" width="168" valign="top">Server Details </td>
<td class="tablevalue">
OPERATING SYSTEM:
$perlos_version
CPU UTILIZATION:
<textarea rows=10 cols=60>$perlos_cpu</textarea>
MEMORY UTILIZATION:
<textarea rows=10 cols=60>$perlos_mem</textarea>
DISK UTILIZATION:
<textarea rows=10 cols=60 wrap="OFF">TOTAL DISK USAGE:\n$diskusage KB \n\n$perlos_dsk</textarea>
</td>
</tr>
<tr>
<td class="tableitems" width="168" valign="top">Module Paths</td>
<td class="tablevalue">$modulepaths</td>
</tr>
<tr>
<td class="tableitems" width="168" valign="top">Path(s) to TAR</td>
<td class="tablevalue">$path_tar</td>
</tr>
<tr>
<td class="tableitems" width="168" valign="top">Path(s) to GZIP</td>
<td class="tablevalue">$path_gzip</td>
</tr>
<tr>
<td class="tableitems" width="168" valign="top">Path to APACHE/HTTPD</td>
<td class="tablevalue">$path_apache
$path_httpd</td>
</tr>
<tr>
<td class="tableitems" width="168" valign="top">Path to PHP</td>
<td class="tablevalue">$path_php</td>
</tr>
<tr>
<td class="tableitems" width="168" valign="top">Path to MYSQL</td>
<td class="tablevalue">$path_mysql</td>
</tr>
<tr>
<td class="tableitems" width="168" valign="top">Path to MAN (Unix manual)</td>
<td class="tablevalue">$path_man</td>
</tr>
<tr>
<td class="tableitems" width="168" valign="top">Path to PERLDOC</td>
<td class="tablevalue">$path_perldoc</td>
</tr>
<tr>
<td class="tableitems" width="168" valign="top">Processes currently on the server</td>
<td class="tablevalue"><textarea rows=10 cols=60 wrap="OFF">$processes</textarea> </td>
</tr>
</table>
<a name="env"></a>
$environment
<!-- ALL MODULES -->
<a name="modules"></a>
<table width="69%" align="center" cellspacing="0" cellpadding="4" bordercolor="#c5c5c5">
<tr bgcolor="#efefef">
<td colspan="2" class="tabletitle">
LIST OF ALL INSTALLED PERL MODULES <font style="font-family: Webdings; font-size: 15px; text-decoration: none">5</font>
</td>
</tr>
<tr>
<td>
~;
&vars;
find(\&wanted,@INC);
$modcount = 0;
foreach $line(@foundmods)
{
$match = lc($line);
if ($found{$line}[0] >0)
{$found{$line} = [$found{$line}[0]+1,$match]}
else
{$found{$line} = ["1",$match];$modcount++}
}
@foundmods = sort count keys(%found);
sub count
{
return $found{$a}[1] cmp $found{$b}[1]
}
$third = $modcount/3;
$count=0;
$firstroundtotal = 0;
foreach $mod(@foundmods)
{
chomp $mod;
$count++;
if ($count <= $third)
{
$firstroundtotal++;
print qq~
$firstroundtotal. $mod
~;
}
else
{
push (@mod1,$mod)
}
}
$count = 0;
print qq~ </td><td>~;
foreach $mod1(@mod1)
{
chomp $mod1;
$count++;
if ($count <= $third){}
else {
push (@mod2,$mod1)}}$count = 0;
foreach $mod2(@mod2)
{ chomp $mod2; $count++; if ($count <= $third)
{
$firstroundtotal++;
print qq~
$firstroundtotal. $mod2
~; }};
sub vars {use File::Find;}
sub wanted { $count = 0; if ($File::Find::name =~ /\.pm$/) { open(MODFILE,$File::Find::name) || return;
while(<MODFILE>){ if (/^ *package +(\S+);/){
push (@foundmods, $1); last; } } } }
print qq~
</td>
</tr>
</table>
<p class="tablevalue"></p>
<p class="tablevalue" align="center"><font style="font-family: Webdings; font-size: 15px; text-decoration: none">5</font> $PAGE_FOOTER</p>
</body>
</html>
-
php redirect
<?php
header("Location: redirected.php");
exit;
?>
-
PhpInfo.php
<?php
echo phpinfo();
?>
-
File Upload Using Php
<?
//file_upload.php
//The dirctory in which you are uploading files must have 777 permissions
//$archive_dir is the location where you want to upload the files or imgaes
$archive_dir = "/home/username/uploadfiles";
function upload_form() {
global $PHP_SELF;
?>
<FORM METHOD="POST" ENCTYPE="MULTIPART/FORM-DATA" ACTION="<? echo $PHP_SELF ?>">
<INPUT TYPE="HIDDEN" NAME="action" VALUE="upload">
Upload file!
<INPUT TYPE="FILE" NAME="userfile">
<INPUT TYPE="SUBMIT" NAME="SUBMIT" VALUE="upload">
</FORM>
<?
}
function upload_file() {
global $userfile, $userfile_name, $userfile_size,
$userfile_type, $archive_dir, $WINDIR;
if(isset($WINDIR)) $userfile = str_replace("\\\\","\	 2;", $userfile);
$filename = basename($userfile_name);
if($userfile_size <= 0) die ("$filename is empty.");
if(!@copy($userfile, "$archive_dir/$filename"))
die("Can't copy $userfile_name to $filename.");
if(!isset($WINDIR) && !@unlink($userfile))
die ("Can't delete the file $userfile_name.");
echo "$filename has been successfully uploaded.
";
echo "Filesize: " . number_format($userfile_size) . "
";
echo "Filetype: $userfile_type
";
}
?>
<HTML>
<HEAD><TITLE>FILE UPLOAD</TITLE></HEAD>
<BODY>
<?
if($action == 'upload'
upload_file();
else upload_form();
?>
</BODY>
</HTML>
-
Perl Script to send an email
Below is the perl script to send an email.
Save the script as test.pl
Change the permissions of test.pl as 755 and test it.
--------------------------------------------------------------
#!/usr/bin/perl
$fromemail = "sami\@abc.com";
$toemail = "sachin\@test.com";
open(SENDMAIL,"|/usr/sbin/sendmail -t");
print SENDMAIL "From : $fromemail\n";
print SENDMAIL "To : $toemail\n";
print SENDMAIL "Subject : This is a test mail.\n";
print SENDMAIL <<EOM;
Hello,
This is a test mail.
EOM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
Bookmarks