Professional Documents
Culture Documents
# /etc/init.d/httpd start
# /etc/init.d/httpd status
httpd (pid 1937 1935 1933 1932 1930 1929 1928 1927
1922) is running...
# /etc/init.d/httpd stop
# /etc/init.d/httpd start # /etc/init.d/httpd graceful
• The referer
How to Login :
$ mysql -u root
ERROR 2002: Can't connect to local MySQL server through socket
´/var/lib/mysql/mysql.sock´(2)
$ mysql -u root
# ls -l /var/lib/mysql/people
total 10
-rw-rw---- 1 mysql mysql 8618 Dec 13 07:24 age_information.frm
-rw-rw---- 1 mysql mysql 0 Dec 13 07:24 age_information.MYD
-rw-rw---- 1 mysql mysql 1024 Dec 13 07:24 age_information.MYI
Real Type :
FLOAT
DOUBLE
REAL (same as DOUBLE)
DECIMAL
NUMERIC (same as DECIMAL)
Information Science and Engineering 20
Character Data Type :
VARCHAR variable-length string up to 255 characters
DATE YYYY-MM-DD
DATETIME YYYY-MM-DD HH:MM:SS
TIMESTAMP YYYYMMDDHHMMSS or YYMMDDHHMMSS or
YYYYMMDD or YYMMDD
TIME HH:MM:SS
YEAR YYYY or YY
OR
SHOW COLUMNS FROM age_information;
$ mysql -u root -p
Grant Command
mysql> GRANT SELECT,INSERT,UPDATE,DELETE
-> ON people.*
-> TO apache@localhost
-> IDENTIFIED BY ´password´;
#! /usr/bin/perl -w
# file name : hello.pl
print "hello, world!\n ";
$ ./hello.pl
Example 2:
#! /usr/bin/perl -w
# file name : simpleadd.pl
$a = 5;
$b = 6;
$c = $a + $b;
print "\$c is now: $c\n";
#! /usr/bin/perl -w
# file name : useVar.pl
$abc=100;
print “The value of abc : $ab\n ";
use strict;
my $abc
OR
my($a, $b, $c);
Strings :
• Single quotes : ´hello world´
• Double quotes : “hello World”
Floats:
• Standard: 7.1 .8 9.
• Scientific: 3.4E-34 -8.023e43
Array :
• @data = (´Joe´, 39, "Test data", 49.3);
• my @data = (´Joe´, 39, "Test data", 49.3);
Output :
Output :
element 0: Joe
element 1: 39
element 2: Test data
element 3: 49.3
Example 6:
#! /usr/bin/perl -w
# file name: pushpop.pl
use strict;
my @a = (2, 4, 6, 8);
print "before: @a\n";
push(@a, 10, 12); #unshift(@a,10,12);
print "after push: @a\n";
my $element = pop(@a); #my $element=shift(@a);
print "after pop: @a\n";
print "element popped: $element\n";
Output :
before: 2 4 6 8 before: 2 4 6 8
after push: 2 4 6 8 10 12 after push: 10 12 2 4 6 8
after pop: 2 4 6 8 10 after pop: 12 2 4 6 8
element popped: 12 element popped: 10
Information Science and Engineering 32
• short() function sorts lists.
• reverse() function reverses lists .
• unshift() function adds elements to the left.
• shift() removes the leftmost element.
Example 7:
#! /usr/bin/perl -w
# file name : sortreverse.pl
use strict;
my @a = (´hello´, ´world´, ´good´, ´riddance´);
print "before: @a\n";
my @b = sort(@a);
print "sorted: @b\n";
@b = reverse(@a);
print "reversed: @b\n";
Output :
before: hello world good riddance
sorted: good hello riddance world
reversed: riddance good world hello
Output :
Purohit lives in KARNATAKA
Purohit is 30 years old
Information Science and Engineering 34
Hash Functions
my @k = keys(%person);
{ state zip address city phone age name }
Operators
Arithmetic Meaning
+ addition
- subtraction
* multiplication
/ division
% modulus
exponentiation
**
Output :
$i + $j = 14
$i * $j = 40
$i / $j = 2.5
$i % $j = 2
$i ** $j = 10000
Example 11:
if ($i < 10)
{
print "\$i is less than 10\n";
}
if ($j >= 20)
{
print "\$j is greater than or equal to 20\n";
}
Information Science and Engineering 38
String Comparison
le less than or equal to
gt greater than
eq equivalent to
ne not equal to
cmp Compare
lt less than
if ($name gt ´Purohit´)
{ print "$name comes after Purohit alphabetically\n"; }
if ($language ne ´Perl´)
{ print “It is C or JAVA.\n"; }
Example 12:
#! /usr/bin/perl -w
# file name : increment.pl
use strict;
my $i = 10;
my $j = ++$i;
print "\$i = $i, \$j = $j\n";
$i = 10;
$j = $i++;
print "\$i = $i, \$j = $j\n";
if (condition1)
{ statements }
elsif (condition2)
{ statements }
else
{ statements }
while (condition)
{
statements
}
#! /usr/bin/perl -w
# file: whilearray.pl
use strict;
my @names = (´Joe´, ´Charlie´, ´Sue´, ´Mary´);
my $i = 0;
while ($i <= $#names)
{
print "Element $i is $names[$i]\n";
$i++;
}
# body
}
#! /usr/bin/perl -w
# file: function1.pl
sub say_hello
{
print "hello, world!\n";
}
say_hello();
#!/usr/bin/perl -w
# file: return1.pl
sub test1
{
$a = 10; $b = 11;
$a + $b; # return ($a+$b);
}
sub test2
{ @a = (´testing´, ´one´, ´two´, ´three´);
sort(@a);
}
$c = test1();
print "\$c = $c\n";
@b = test2();
print "\@b = @b\n";
my $num = 10;
my $name = ´Purohit´;
print_args($num, $name, 3.14159, ´hello, world!´);
my $num = 10;
my $name = ´Purohit´;
print_args_2($num, $name, 3.14159);
Output :
$a is: 10
$b is: Purohit
$c is: 3.14159
#! /usr/bin/perl -w
# file: file1.pl
use strict;
my $line;
open (FH, ´<test.txt´) or die "Can't open test.txt: $!";
while ($line = <FH>)
{
print "Line is: $line"; #@all_lines = <FH>;
}
close (FH);
Information Science and Engineering 51
my $i = 0;
while ($i <= $#all_lines)
{
print "Line is: $all_lines[$i]";
$i++;
}
Writing to a File
#! /usr/bin/perl -w
# file3.pl
use strict;
my $line;
open (FH, ´ >output.txt´) or die "Can't open output.txt: $!";
while ($line = <STDIN>)
{
print FH "You entered: $line";
}
close (FH);
<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory> # /etc/init.d/httpd graceful
OR
Service httpd restart
CGI Programming :
#!/usr/bin/perl
# first.cgi
print "Content-type: text/plain\n";
print "\n";
print “This is my first CGI Programming…";
/var/www/cgi-bin/first.cgi
chmod a+rx first.cgi
Information Science and Engineering 55
#!/usr/bin/perl
# second.cgi
print "Content-type: text/plain\n";
print "\n";
print "<b>hello</b>, <i>world</i>!";
#!/usr/bin/perl
# second1.cgi
print "Content-type: text/html\n";
print "\n";
print "<b>hello</b>, <i>world</i>!";
<H1> h1()
<P> p()
</BODY></HTML> end_html()
<HR> hr()
lists li(), dl(), ul(), dd() etc.,
<BOLD> </BOLD> b()
Request Information
Client Information :
• remote_host()— the name of the client machine
• user_name()— the name of the user if authenticated
• user_agent()— the browser that the user is surfing with
• referer()— the referer (remember, this is misspelled)
• path_info()— the path information
• query_string()— the query string
#! /usr/bin/perl
# path_info.cgi
use strict;
use CGI ´:standard´;
my $env_path = $ENV{PATH_INFO} || ´ ´;
my $method_path = path_info() || ´ ´;
print header, start_html(-title => ´Path Information´, -bgcolor => ´#ffffff´),
´From %ENV: ´, b($env_path),
br(),
´From path_info(): ´, b($method_path),
end_html();
Information Science and Engineering 64
Processing Posted Form Data
• GET
• POST
<html>
<head> <title>Enter Your Name and Age</title> </head>
<body bgcolor="#ffffff">
<form action="/cgi-bin/nameage.cgi" method="get">
Name: <input type="text" name="yourname">
<br> Age: <input type="text" name="age"> <br>
<input type="submit" value="Click to Submit">
</form>
</body>
</html>
http://localhost/cgi-bin/nameage.cgi?yourname=Purohit&age=30
password_field(-name=>'field_name', -size=>number,
"password" input field
-maxlength=>number)
scrolling_list(-name=>'menu_name', -values=>array or hash reference, select element with size larger than
-default=>initial selection(s), -size=>number, -multiple=>'true') 1
checkbox(-name=>'checkbox_name', -checked=>'checked',
a standalone checkbox
-value=>internal value, -label=>visible label)
while(($f1, $f2)=$sth->fetchrow())
{
print pre("$f1 $f2");
}
print '-' x 80;
print end_html();
Information Science and Engineering 71
#!/usr/bin/perl -w
use DBI;
use strict;
use CGI ':standard';
my $msg;
my $dbh = DBI->connect('DBI:mysql:people', ‘root', ‘')
or die "Can't connect: " . DBI->errstr();
my $fname = param('fname') || ' ';
my $lname = param('lname') || ' ';
my $age = param('age') || ' ';
if( $fname eq ' ' || $age eq ' ‘ )
{
$msg="Incomplete Entry ... Datababse could not be updated !";
}
else
{
my $sth = $dbh->prepare('INSERT INTO ageInfo (lname,fname,age)
VALUES(?,?,?)')
or die "Can't prepare SQL: " . $dbh->errstr();
$sth->execute($lname,$fname,$age)
or die "Can't execute SQL: " . $sth->errstr();
$msg="Data Successfully Inserted into People Database";
}
my $sth = $dbh->prepare('SELECT * FROM ageInfo') or die "Can't prepare SQL: " .
$dbh->errstr();
Information Science and Engineering 72
$sth->execute() or die "Can't execute SQL: " . $sth->errstr();
print header(),
start_html(-title => "Content of People database",-bgcolor => "#1E90FF" ),
h3($msg),
<<EOHTML;
<h2>Current Name/Age Information from People Database</h2>
<hr><br><br>
<table border="1"><tr>
<th>First Name</th><th>Last Name</th><th>Age</th></tr>
EOHTML
while(($lname,$fname,$age)=$sth->fetchrow())
{
print "<tr><td>$fname</td><td>$lname</td><td>$age</td></tr>\n";
}
$sth->finish();
$dbh->disconnect();
Mysql> create table ageInfo (fname char(30), lname char(30), age int );
2. <?php … … ?>
Example : <?php echo “Hello World!...."; ?>
<html>
<head>
<title>Hello, world! with PHP</title>
</head>
<body bgcolor="#ffffff">
<? echo "hello, world!" ?> #<? phpinfo() ?>
</body>
</html>
if ($name == “Purohit")
{
echo "You say your name is Purohit";
echo "Are you sure that is your name?";
}
?>
<?
$a = array(2, 4, 6, 8);
echo $a[0]; // echoes 2
$b = array(“CSE", “ISE", “MCA");
echo $b[1]; // echos ISE
?>
<?
$capital["Illinois"] = "Springfield";
$capital["California"] = "Sacramento";
$capital["Texas"] = "Austin";
$capital["Wisconsin"] = "Madison";
$state = "Illinois";
echo "$state: $capital[$state]"; // echoes "Illinois: Springfield"
?>
Web Variables
<Directory /var/www/html/php>
php_flag register_globals Off
</Directory>
Post–Version 4.1.0
•$_GET — an array of the data sent with GET (replaces $HTTP_GET_VARS)
•$_POST — an array of the data sent with POST (replaces $HTTP_POST_VARS)
•$_COOKIE — HTTP cookie variables
•$_REQUEST — a merge of the GET, POST, and cookie data
Version 4.1.0 introduces some other helpful variables:
•$_SERVER — the Apache server environment variables, such as REMOTE_ADDR
•$_ENV — the environment variables
•$_SESSION — data registered by the session module
<?
foreach ($a as $k => $v)
{
echo "<tr><th>$k</th><td>$v</td></tr>";
}
?>
</table>
Example :
<?
function print_hello ()
{ echo "hello, world!"; }
?>
<html>
<head> <title>PHP Functions - Part 1</title> </head>
<body bgcolor="#ffffff">
<? print_hello(); ?>
</body>
</html> Information Science and Engineering 85
function no_default_args($a, $b, $c)
{ echo "<hr>";
echo "no_default_args(): \$a : $a <br>";
echo "no_default_args(): \$b : $b <br>";
echo "no_default_args(): \$c : $c <br>";
}
Calling :
no_default_args("foo", $variable_name, 4.9485);
function default_args($a = 2, $b = 4, $c = 6)
{
echo "<hr>"; echo "default_args(): \$a : $a <br>";
echo "default_args(): \$b : $b <br>";
echo "default_args(): \$c : $c <br>";
}
Calling :
default_args(10);
Array Functions
array_push() array_unshift()
array_pop() array_shift()
sort() rsort()
array_reverse()
asort(), arsort(), ksort(), and krsort()
count() -- array size