R16 WT Manual
R16 WT Manual
R16 WT Manual
AND SCIENCES,
VISAKHAPATNAM.
WEB TECHNOGIES
R16
LAB MANUAL
1 Design the following static web pages required for an online book
store web site.
1 HOME PAGE:
The static home page mustcontain three frames.
Top frame:Logo and the college and links to home page,Login
page,Registratin page,catlogue page and cart page (the description of
these pages will be give below).
Left page: At laest four links for navigation ,which will display the
catlogue of respective links.
For example:when you click the “CSE” the catlogue for CSE Books
should be displayed in the Right Frame.
Right frame:the pages to links in thelaft frame must be loaded
here .Initially this page contains description of the web site.
2.LOGIN PAGE
3. The catlogue page should contain the details of all the books
available in the web site in a table.
The details should contain the following:
1 snap shot of cover page.
2 Author name
3 Publisher.
4 Price
5 Add to cart button.
5. Design a web page using CSS (cascading Style sheets) which
includes the following:
1 Use different font , styles:
In the style definition you define how each selector should work (font
, color etc.).
Then in the body of your pages, you refer to these selectors to activate
the styles.
6. Write an XML file which will display the book information which
include the following:
1 Title of the book
2 Author name
3 publisher name
4 Edition
5 price
Write a Document Type Definition (DTD) to validate the above
XML file.
7. Write a Ruby program reads a number and calculates the factorial
value of it and prints the same.
8. Write a Ruby program which counts number of lines in the text
files using regular expression facility.
9. Write a Ruby program that uses iterator to find out the length of a
string
10. Write a Ruby program that uses arrays in Ruby.
11. Write a Ruby program which uses associative arrays in Ruby.
12. Write a Ruby program which uses math module to find area of
triangle.
13. Write Ruby program which uses tk module to display a window
14. Define complex class in Ruby and do write methods to carry
operations on complex objects.
15. Write a program which illustrates the use of associative arrays in
perl.
16.Write perl program takes set names along the command line and
prints whether they are regular files or special files
17. Write a perl program to implement UNIX `passed' program
18. An example perl program to connect to a MySQl database table
and executing simple commands.
19. Example PHP program for cotactus page.
20. User Authentication: Assume four users user1, user2, user3 and
user4 having the passwords pwd1, pwd2, pwd3 and pwd4
respectively. Write a PHP for doing the following.
1. Create a Cookie and add these four user id’s and passwords to this
Cookie.
2. Read the user id and passwords entered in the Login form (week1)
and authenticate with the values (user id and passwords) available in
the cookies.
If he is a valid user (i.e., user-name and password match) you should
welcome him by name (user-name) else you should display “You are
not an authenticated user ’’.
Use init-parameters to do this.
21. Example PHP program for registering users of a website and
login.
23. Write a PHP which does the following job: Insert the details of the
3 or 4 users who register with the web site (week9) by using
registration form. Authenticate the user when he submits the login
form using the user name and password from the database ( similar to
week8 instead of cookies).
1.AIM: Design the following static web pages required for an online
book store web site.
1 HOME PAGE:
The static home page mustcontain three frames.
Top frame:Logo and the college and links to home page,Login
page,Registratin page,catlogue page and cart page (the description of
these pages will be give below).
Left page: At laest four links for navigation ,which will display the
catlogue of respective links.
For example:when you click the “CSE” the catlogue for CSE Books
should be displayed in the Right Frame.
Right frame:the pages to links in thelaft frame must be loaded
here .Initially this page contains description of the web site.
PROGRAM:
<html>
<head>
<title>FRAMES</title>
</head>
<frameset rows="36%,64%">
<frameset rows="60%,40%">
<frameset cols="20%,80%">
<frame src="logo.html" align="center"/>
<frame src="website.html"/>
</frameset>
<frameset cols="20%,20%,20%,20%,20%">
<frame src="home.html"/>
<frame src="login.html"/>
<frame src="register.html"/>
<frame src="catlog.html"/>
<frame src="cartlog.html"/>
</frameset>
</frameset>
<frameset cols="20%,80%">
<frame src="data.html"/>
<frame src="description.html" name="display"/>
</frameset></frameset></html>
LOGO.HTML:
<html>
<head>
<title>logo</title>
</head>
<body>
<img src="gonna.jpg" width="175" height="75"></font>
</img>
</body>
</html>
WEBSITE.HTML:
<html>
<head>
<title>website</title>
</head>
<body>
<img src="gonna.jpg" height="75" width="100">
<font color="red" size="12">GONNA COLLEGE OF
ENGINEERING
</font>
</img>
</body>
</html>
HOME.HTML:
<html>
<head>
<title>HOME</title>
</head>
<body vlink="blue" alink="green">
<a href="home1.html" target="display">
<h1 align=center>HOME<h1></a>
</body>
</html>
LOGIN.HTML:
<html>
<head>
<title>LOGIN</title>
</head>
<body vlink="blue" alink="green">
<a href="login1.html" target="display">
<h1 align="center">LOG IN</h1>
</a>
</body>
</html>
REGISTER.HTML:
<html>
<head>
<title>REGISTER</title>
</head>
<body vlink="blue" alink="green">
<a href="register1.html" target="display">
<h1 align="center">REGISTER</h1>
</a>
</body>
</html>
CATLOG.HTML:
<html>
<head>
<title>CATLOG</title>
</head>
<body vlink="blue" alink="green">
<a href="catlog1.html" target="display">
<h1 align="center">CATLOG</h1>
</a>
</body>
</html>
CART.HTML:
<html>
<head>
<title>CART</title>
</head>
<body vlink="blue" alink="green">
<a href="cartlog1.html" target="display">
<h1 align="center">CART</h1> </a>
</body>
</html>
DATA.HTML:
<html>
<head>
<title>DATA</title>
</head>
<body vlink="blue" alink="green">
<br><br>
</html><a href="cse123.html" target="display">
<h1 align="center">CSE</h1></a><br>
<a href="ece123.html" target="display">
<h1 align="center">ECE</h1></a><br>
<a href="eee123.html" target="display">
<h1 align="center">EEE</h1></a><br>
<a href="civil123.html" target="display">
<h1 align="center">CIVIL</h1></a>
</body>
</html>
DESCRIPTION.HTML:
<html>
<head>
<title>DESCRIPRTION</title>
</head>
<body><br><br><br><br><br><br>
<h1 align="center"><font color="green">DISPLAY</font>
</h1>
</body>
</html>
OUTPUT:
OUTPUT:
OUTPUT:
OUTPUT:
OUTPUT:
AIM: the catlogue page should contain the details of all the books
available in the web site in a table.
The details should contain the following:
1 snap shot of cover page.
2 Author name
3 Publisher.
4 Price
5 Add to cart button.
PROGRAM:
CSE.HTML
<html>
<head>
<title>CSE</title>
</head>
<frameset cols="25%,25%,25%,25%">
<frame src="book.html"/>
<frame src="author1.html"/>
<frame src="price.html"/>
<frame src="submit.html"/>
</frameset>
</html>
book.html:
<html>
<head>
<title>book</title>
</head>
<table align="center">
<tr>
<td><img src="ds.jpg" width="130" height="80">
</img><br><br><br></td></tr>
<tr>
<td><img src="java.jpg" width="130" height="80">
</img><br><br><br></td></tr>
<tr>
<td><img src="web.jpg" width="130" height="80">
</img></td></tr>
</table>
</body>
</html>
author1.html:
<html>
<head>
<title>author</title>
</head>
<body>
<table align="center">
<tr> <td>B.Name:Data Structure</td> <br></tr>
<tr> <td>Author:Michael main</td> </tr>
<tr> <td>Eddition:7<sup>Th</sup> <br><br> <</td> </tr>
<tr><td>B.Name:JAVA</td></tr>
<tr><td>Author:John Dean</td></tr>
<tr><td>Eddition:9<sup>Th</sup> <br><br></td></tr>
<tr><td>B.Name:WEb Technology</td></tr>
<tr><td>Author:R.K Soman</td></tr>
<tr><td>Eddition:5<sup>Th</sup></td></tr>
</table>
</body>
</html>
price.html:
<html>
<head>
<title>price</title>
</head>
<body>
<table>
<tr>
<td><h3 align="center">Price:550</h3><br><br><br></td>
</tr>
<tr>
<td><h3 align="center">Price:700</h3><br><br><br></td>
</tr>
<tr>
<td><h3 align="center">Price:999</h3></td><br>
</tr>
</table>
</body>
</html>
SUBMIT.HTML:
<html>
<head>
<title>SUBMIT</title>
</head>
<body>
<form><br><br><br>
<input type="submit" value="Add To Cart">
<br><br><br><br><br>
<input type="submit" value="Add To Cart">
<br><br><br><br><br>
<input type="submit" value="Add To Cart">
</form>
</body>
</html>
OUTPUT:
5. AIM: Design a web page using CSS (cascading Style sheets)
which includes the following:
1 Use different font , styles:
In the style definition you define how each selector should work (font
, color etc.).
Then in the body of your pages, you refer to these selectors to activate
the styles.
PROGRAM:
<html>
<head>
<title>INTERNAL</title>
<style type="text/css">
body {background-color:skyblue;Font-family:Arial}
th {background-color:green;Font-family:Algerian}
td {background-color:red;Font-family:Algerian}
</style>
</head>
<body>
<h1 align="center">INTERNAL STYLESHEET</h1>
<table align="center">
<tr>
<th>SID</th>
<th>SNMAE</th>
<th>SAGE</th>
<th>SADDRESS</th>
</tr>
<tr>
<td>501</td>
<td>Vivek</td>
<td>19</td>
<td>MVP</td>
</tr>
<tr>
<td>502</td>
<td>Charisma</td>
<td>19</td>
<td>janathacolony</td>
</tr>
<tr>
<td>503</td>
<td>NARENDRA</td>
<td>19</td>
<td>Kurmanapalem </td>
</tr>
<tr>
<td>504</td>
<td>Ramya</td>
<td>20</td>
<td>NAD</td>
</tr>
</table>
</body>
</html>
OUTPUT:
OUTPUT:
OUTPUT:
AIM:STYLE.CSS
PROGRAM:
<html>
<head>
<title>frame 6</title>
</head>
<body bgcolor= "PINK">
<marquee behavior="alternate"><font size="10px"
color="RED">WELCOME TO GONNA INSTITUTEOF
ENGINEERING TECHNOLOGY & SCIENCES
</font></marquee><br><br>
<marquee direction="up"> <font size="5px" >GIITS </font>
</marquee><br>
<font color="WHITE" size="5px"><marquee direction="left
style="background:#800000">GIITS</marquee></font>
<marquee direction="right" behavior="alternate"
scrollamount="15"><font color="GREEN"
size="5px">GIITS</font></marquee>
<marquee style="border:green 5px SOLID"><font color="BLUE"
size="5px">GIITS</font></marquee>
<marquee behavior="alternate"><font color="ORANGE"
size="5px">GIITS </font><img src="a.gif" width="400"
height="230" border="3" ></marquee>
</body>
</html>
OUTPUT:
6. AIM: write an XML file which will display the book
information which include the following:
1 Title of the book
2 Author name
3 publisher name
4 edition
5 price
Write a Document Type Definition (DTD) to validate the above
XML file.
PROGRAM:
Bookstore.dtd
Demonstrating XML
bookstore.xml
<?xml
version="1.0"?>
<bookstore>
<book>
<title>C</title>
<author> Balaguruswamy</author>
<isbn>81-695</isbn>
<publisher>Tata</publisher>
</book>
<book>
<author> Hansbergsten</author>
<isbn>81-265</isbn>
<publisher>O'relilly</publisher>
<edition> Fourth </edition>
<price>500</price>
</book>
<book>
<title> Web Programming </title>
<author>Chirsbates </author>
<isbn>81-265</isbn>
<publisher>Wiley</publisher>
<edition>Second</edition>
<price>300</price>
</book>
</bookstore>
Demonstrating .css
bookstore.css
bookstore
{
color:orange;
}
book
{
color:Red;
}
title
{
color:blue;
font-weight:bold;margin-left:10pt;display:block;
}
author
{
color:red;
font- weight:bold;margin-left:10pt;
}
isbn
{
color:green;
font-weight:bold;margin-left:10pt;
}
edition
{
color:red;
font-weight:bold;margin-left:10pt;
}
publisher
{
color:green;
font-weight:bold;margin-left:10pt;
}
DemonstratingXSL
bookstore.xsl
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2> My Books collection</h2>
<table border="1">
<tr bgcolor="red">
<th align="left">TITLE</th>
<th align="left">AUTHOR</th>
<th align="left">ISBN</th>
<th align="left">PUBLISHER</th>
<th align="left">EDITION</th>
<th align="left">PRICE</th></tr>
<xsl:for-each select="bookstore/book">
<tr><td bgcolor="yellow"><xsl:value-of select="title"/></td>
<td bgcolor="orange"><xsl:value-of select="author"/></td>
<td bgcolor="yellow"><xsl:value-of select="isbn"/></td>
<td bgcolor="yellow"><xsl:value-of select="publisher"/></td>
<td bgcolor="pink"><xsl:value-of select="edition"/></td>
<td bgcolor="green"><xsl:value-of select="price"/></td></tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
OUTPUT:
Demonstrating DTD file:
Demonstrating XML
Online Screen:
Offline Screen:
Demonstrating
CSS
Online Screen:
Offline screen:
Demonstrating
XSL
Online Screen:
Offline Screen:
7. AIM: Write a Ruby program reads a number and calculates the
factorial value of it and prints the same.
PROGRAM:
puts "Enter a number>>"
n = gets.to_i
f=1
i=1
while i<=n do
f = f*i
i= i+1
end
puts "factorial of #{n} is #{f}"
OUTPUT:
8.AIM: Write a Ruby program which counts number of line in a text
file using its regular expression facility.
PROGRAM:
print "Enter file name:";
fname = gets.chomp;
count = IO.readlines(fname).size
puts "There are #{count} lines in #{fname}";
OUTPUT:
9. AIM: Write a Ruby program that uses iterator to find out the
length of a string.
PROGRAM:
str="web technology"
len=0
str.each_char do |ch|
puts ch
len+=1;
end
puts "length of string is #{len}"
OUTPUT:
OFFLINE SCREEN:
10. AIM: Write simple Ruby program that uses arrays in Ruby .
PROGRAM:
puts "Bubble Sort"
puts "==========="
print "Enter the size of the array:"
n=gets.to_i
a=Array.new(n)
puts "Enter the elements :"
for i in 0..n-1
a[i]=gets.to_i
end
#Bubble sort Algorithm
t=0
for i in 0..n-1
for j in i..n-1
if (a[i] > a[j])
t=a[j]
a[j]=a[i]
a[i]=t
end
end
end
puts "After Sorting :"
for i in 0..n-1
print "#{a[i]} "
end
OUTPUT:
11. AIM: Write programs which uses associative arrays (HASHES)
concept of Ruby.
PROGRAM:
creators = Hash.new;
creators={"Vaccine"=>"Paschar","Dynamite"=>"Nobel","Toilet"=>"
Creeper"};
size = creators.length;
puts "Size of hash is: #{size}";
creators.each do |key,val|
puts "#{key} invented by #{val}"
end
OUTPUT:
12. AIM: Write ruby program which uses Math module to find area of
a triangle.
PROGRAM:
print "Enter a,b,c values:"
a=gets.to_i
b=gets.to_i
c=gets.to_i
s = (a+b+c)/2
area = Math.sqrt(s*(s-a)*(s-b)*(s-c)).round(2)
print "Area of Triangle=#{area}"
OUTPUT:
OUTPUT:
16. AIM: Write Perl program takes a set names along the command
line and prints whether they are regular files or special files
PROGRAM:
fileTest.pl
$len = @ARGV;
for ($i=0;$i<$len;$i++)
{
if(-e $ARGV[$i])
{
if(-T $ARGV[$i])
{
print "$ARGV[$i] is a text file\n";
}
else
{
print "$ARGV[$i] is a special file\n";
}
}
else
{
print "$ARGV[$i] does not exists";
}
}
OUTPUT:
17. AIM:Write a Perl program to implement UNIX „passwd’
program
PROGRAM:
unixPw.pl
my $salt='';
my $encrypted='';
my $password='';
my $use = 'Usage: Please provide password for encrypt';
my @saltchars=('.', '/', 0..9, 'A'..'Z', 'a'..'z');
my $args=@ARGV;
if ( $args < 1 || $args > 2 )
{
print "$use\n";
exit;
}
$password=$ARGV[0];
if( $args == 1 )
{
$salt = join('',@saltchars[rand(64),rand(64)]);
}
else
{
$salt=$ARGV[1];
}
$encrypted=crypt($password,$salt);
print "$password --> $encrypted\n";
OUTPUT:
18. AIM: An example Perl program to connect to a MySQL
Database table and executing simple commands.
PROGRAM:
Note: Create the following table in MySQL and insert some data in to
the table.
students table
create table students
(
roll varchar(3) not null,
name varchar(3) not null,
branch varchar(3) not null
);
dbConn.pl
use DBI;
#definition of variables
$db="test";
$host="localhost";
$user="root";
$password="";
#connect to MySQL database
my $dbh = DBI->connect ("DBI:mysql:database=$db:host=$host",
$user,$password) or die “Can't connect to database: $DBI::errstr\n";
#prepare the query
my $sth = $dbh->prepare( "SELECT *FROM students");
#execute the query
$sth->execute();
## Retrieve the results of a row of data and print
print "\tQuery results:\n=====================\n";
print("ROLL\tNAME\tBRANCH\n=====================\n");
while (my @row = $sth->fetchrow_array())
{
print "@row\n";
}
warn "Problem in retrieving results", $sth->errstr( ), "\n" if $sth->err();
exit;
OUTPUT:
19. AIM: Example PHP program for contacts page.
cotactus.php
<?php
if(isset($_REQUEST['vname']))
{
$vname=$_REQUEST['vname'];
$email=$_REQUEST['email'];
$message=$_REQUEST['message'];
$from="From: $vname<$email>\r\nReturn-path: $email";
$subject="Message sent using your contact form";
$m = mail("youremail@yoursite.com", $subject, $message, $from);
if($m)
echo "<h1>Email sent!";
else
echo "Send Failed:".mysql_error();
echo " <a href='contacts.php'>[Back to
Contacts]</a>";
}
else { ?>
<html>
<head>
<title>Contacts Page</title>
</head>
<body>
<font face="Trebuchet ms" size="6">
<table border="1">
<tr><th>Contact Us</th></tr>
<tr><td>
<form action="contacts.php" method="POST"
enctype="multipart/form-data">
Your name<br/>
<input type="text" name="vname" value="" size="30"/><br>
Your email<br>
<input type="text" name="email" value="" size="30"/><br>
Your message<br>
<textarea name="message" rows="7" cols="30"></textarea><br>
<input type="submit" value="Send email"/>
</form>
</td></tr>
</table>
</body>
</html>
<?php } ?>
OUTPUT:
PROGRAM:
login.html
<html>
<head>
<title>Login Page</title>
</head>
<body>
<center>
<h3> Login Page</h3>
<form name="f1" action="setcookie.php" method="post">
<table frame="border">
<tr>
<td>User</td>
<td><input type="text" name="user" size="20" value=""/></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pwd" size="20" value=""/></td>
</tr>
<tr>
<td align="center" colspan="2"><input type="submit" value="Sign
in"/>
<input type="reset" /></td>
</tr>
</table>
</form>
</body></center>
</html>
setcookie.php
<html>
<head>
<title>Set Cookie</title>
</head>
<body>
<?php
$user = $_POST['user'];
$pwd = $_POST['pwd'];
setcookie("user",$user,time()+3600*2);
setcookie("pwd",$pwd,time()+3600*2);
echo "<h3>The Cookie Added...</h3>";
?>
</body>
</html>
getcookie.php
<html>
<head>
<title>Get Cookie</title>
</head>
<body>
<?php
$user = $_COOKIE['user'];
$pwd = $_COOKIE['pwd'];
if($user=="user1")
{
if($pwd == "pwd1")
echo "<h2>Welcome User1 </h2>";
else
echo "<h2>You are not an authenticated user.</h2>";
}
elseif($user=="user2")
{
if($pwd == "pwd2")
echo "<h2>Welcome User2</h2>";
else
echo "<h2>You are not an authenticated user.</h2>";
}
elseif($user=="user3")
{
if($pwd == "pwd3")
echo "<h2>Welcome User3</h2>";
else
echo "<h2>You are not an authenticated user.</h2>";
}
elseif($user=="user4")
{
if($pwd == "pwd4")
echo "<h2>Welcome User4</h2>";
else
echo "<h2>You are not an authenticated user.</h2>";
}
else
echo "<h2>Invalid Username/Password</h2>";
?>
</body>
</html>
OUTPUT:
OUTPUT 1 for Invalid User:
OUTPUT 2 for Invalid User:
OUTPUT for Valid user:
OUTPUT for Invalid user:
b. AIM: Use init-parameters to do the above task. Store the user-
names and passwords in the “users.ini” file and access them in
php page by using the “parse_ini_file()” method.
mylogin.html
<html>
<head>
</head>
<title>Login Page</title>
<body>
<center>
<h3> Login Page</h3>
<form name="f1" action="initparam.php" method="post">
<table frame="border">
<tr>
<td>User</td>
<td><input type="text" name="user" size="20" value=""/></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pwd" size="20" value=""/></td>
</tr>
<tr>
<td align="center" colspan="2"><input type="submit" value="Sign
in"/>
<input type="reset" /></td>
</tr>
</table>
</form>
</body></center>
</html>
users.ini
This is a users .ini file
[first_section] u1=user1 p1=pwd1
initparam.php
<html>
<head>
</head>
<title>Init Parameters</title>
<body>
<?php
$user = $_POST['user'];
$pwd = $_POST['pwd'];
$temp = parse_ini_file("users.ini"); foreach($temp as $value)
{
$users[] = $value;
}
if($user==$users[$i])
{
}
else
}
?>
if($pwd==$users[$i+1])
echo "<h3>Welcome $users[$i]</h3>"; else
echo "<h3>You are not an authenticated user</h3>"; echo
"<h3>Invalid Username/Password</h3>";
</body>
</html>
OUTPUT for Valid user:
OUTPUT for Invalid user:
21. AIM: Example PHP program for registering users of a website
and login.
PROGRAM:
registration1 table:
CREATE TABLE registration1
(
uname varchar(25) NOT NULL,
pass varchar(25) NOT NULL,
email varchar(25) NOT NULL,
phno bigint(10) NOT NULL,
gender varchar(6) NOT NULL,
dob char(10) NOT NULL,
languages varchar(25) NOT NULL,
address varchar(75) NOT NULL,
UNIQUE KEY (uname)
);
A) Registration: Add the following code to registration.html in
<head> section:
<script language="javascript">
function validate()
{
var nam = document.f1.uname.value;
if(nam=="")
{
alert("Please enter name");
document.f1.uname.focus();
return false;
}
var pwd = document.f1.pass.value;
if(pwd=="")
{
alert("Please enter Password");
document.f1.pass.focus();
return false;
}
var email = document.f1.email.value;
if(email=="")
{
alert("Please enter youe email");
document.f1.email.focus();
return false;
}
var phno = document.f1.phone.value;
len=phno.length
if(phno=="" || len != 10)
{
alert("Please enter phno or should be strictly 10 digits");
document.f1.phone.focus();
return false;
}
var genders = document.getElementsByName("gen");
var selectedGender="";
for(var i = 0; i < genders.length; i++)
{
if(genders[i].checked == true)
selectedGender = genders[i].value;
}
if(selectedGender=="")
{
alert("Please select your gender");
document.f1.gen.focus();
return false;
}
var day = document.f1.day.value;
var month = document.f1.month.value;
var year = document.f1.year.value;
if(day=="day" || month=="month" || year=="year")
{
alert("Please select your DOB");
document.f1.day.focus();
return false;
}
var languages = document.getElementsByName("lang[]");
var selected = new Array();
var j=0;
for(var i = 0; i < languages.length; i++)
{
if(languages[i].checked == true)
selected[j++] = languages[i].value;
}
var len = selected.length;
if(len==0)
{
alert("Please Choose atleast one language");
document.f1.lang.focus();
return false;
}
var addr = document.f1.addr.value;
if(addr=="")
{
alert("Please enter your address");
document.f1.addr.focus();
return false;
}
}
</script>
Then, Modify the <form> tag attributes:
<form name="f1" action="insert.php" method="post"
onsubmit="javascript:return validate()">
insert.php
<?php
$conn = mysql_connect("localhost","root","") or die("Failed to
Connect:".mysql_error());
mysql_select_db("test",$conn) or die("No Database
existing:".mysql_error());
if(isset($_POST['uname']))
{
$uname=$_POST['uname'];
$pass=$_POST['pass'];
$email=$_POST['email'];
$phno=(float)$_POST['phone'];
$gender=$_POST['gen'];
$day=(int)$_POST['day'];
$month=(int)$_POST['month'];
$year=(int)$_POST['year'];
$dob = "$day-$month-$year";
$str=$_POST['lang'];
$lang="";
foreach ($str as $val)
{ $lang = $lang." ".$val;}
$text=$_POST['addr'];
$addr = str_replace("\n","<br>",$text);
$query = "INSERT INTO registration1 VALUES ('$uname', '$pass',
'$email', '$phno', '$gender',
'$dob', '$lang', '$addr')";
$res = mysql_query($query);
if($res)
{
$script = "<script>alert('User Registration Successful');";
$script .= "document.writeln('<center><h4><u>User
Details</u></h1>');";
$script .= "document.writeln('Your username is:<b>
$uname</b><br/>');";
$script .= "document.writeln('Your password is:<b>
$pass</b></center>');";
$script .= "document.writeln('<h4 align=right><a
href=catalogue.html>[View Books]
</a> </h4>');</script>";
echo $script;
}
else
echo "<script>alert('User Registration Failed'); window.location =
'registration.html';
</script>";
}
?>
OUTPUT:
b)Login Verification login.html
login.html
<html>
<head>
<title> Sign-in Page </title>
<script language="javascript"> function validate()
{
var nam = document.f1.uname.value; if(nam=="")
{
alert("Please enter name"); document.f1.uname.focus; return false;
}
var pwd = document.f1.pass.value; if(pwd=="")
{
}
}
</script>
</head>
<body>
<br/><br/><br/>
<center>
<form name="f1" action="validation.php" method="post"
onsubmit="javascript:return
validate()">
<table border="3" cellpadding="0" cellspacing="0">
<tr>
<td>
<table cellspacing="10">
<tr>
<td colspan="2" align="center"><h2><u>Login Page<u></h2></td>
</tr>
<tr>
<td>User Name</td>
<td><input type="text" name="uname" size="15"></td>
</body>
</html>
</tr>
<tr>
<td> Password</td>
<td><input type="password" name="pass" size="15"></td>
</tr>
<tr>
<td align="center"><input type="submit" value="Sign in"></td>
<td align="center"><input type="reset" value="Clear"></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</center>
validation.php
<?php
$conn = mysql_connect("localhost","root","") or die("Failed to
Connect:".mysql_error()); mysql_select_db("test",$conn) or die("No
Database existing:".mysql_error()); if(isset($_POST['uname']))
{
$uname=$_POST['uname'];
$pass=$_POST['pass'];
$res = mysql_query("select * from registration1 where uname='$uname'
and pass ='$pass'");
$count=mysql_num_rows($res);
if($count)
echo
"<script>alert('Welcome $uname');
window. Location='catalogue.html';
</script>";
again!!!!!</h2>";
}
else
echo "<br/><br/><br/><h2 align='center'>Invalid
Username/Password/Try
?>
OUTPUT:
23.Install a database (MySQL or Oracle).
Create a table which should contain at least the following fields:
name, password, email-id, phone Number (these should hold the
data from the registration form). Write a PHP program to connect to
that database and extract data from the tables and display them.
Experiment with various SQL queries. Insert the details of the users
who register with the web site, whenever a new user clicks the
submit button in the registration page.
registration table:
registration.html
<html>
<head>
<title> User Registration Page</title>
<script language="javascript">
function validate()
{
var nam = document.f1.uname.value;
if(nam=="")
{
alert("Please enter name");
document.f1.uname.focus;
return false;
}
var pwd = document.f1.pass.value;
if(pwd=="")
{
alert("Please enter Password");
document.f1.pass.focus;
return false;
}
var email = document.f1.email.value; if(email=="")
{
alert("Please enter youe email");
document.f1.email.focus;
return false;
}
var phno = document.f1.phone.value;
len=phno.length
if(phno=="" || len != 10)
{
alert("Please enter phno or should be strictly 10 digits");
document.f1.phone.focus;
return false;
}
}
</script>
</head>
<body>
<br/><br/><br/>
<center>
<form name="f1" action="insertData.php" method="post"
onsubmit="javascript:return validate()">
<table border="3" cellpadding="0" cellspacing="0">
<tr>
<td>
<table cellspacing="10">
<tr>
<td colspan="2"align="center"><h2><u>User Registration
Form</u></h2></td>
</tr>
<tr>
<td>User Name</td>
<td><input type="text" name="uname" size="50"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pass" size="50"></td>
</tr>
<tr>
<td>E-mail</td>
<td><input type="text" name="email" size="50"></td>
</tr>
<tr>
<td>Phone</td>
<td><input type="text" name="phone" size="15"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit"
value="submit"></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
insertData.php
<?php
$conn = mysql_connect("localhost","root","");
if($conn)
echo "Connected to database!!!";
else
echo "Failed to Connect:".mysql_error();
mysql_select_db("test",$conn) or die("No Database
existing:".mysql_error());
if(isset($_REQUEST['uname']))
{
$uname=$_REQUEST['uname'];
$pass=$_REQUEST['pass'];
$email=$_REQUEST['email'];
$phno=(float)$_REQUEST['phone'];
VALUES('$uname','$pass','$email','$phno')";
mysql_query($query);
$result = mysql_query("select * from registration");
$query = "INSERT INTO registration
?>
<html>
<body>
<br/><br/><br/>
<p align="right"><a href="registration.html">
[Registration Page]</a>
</p>
<center>
<font face="verdana" size="4">
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<th colspan="4" align="center">User List</td>
</tr>
<tr>
<th> S.No.</th>
<th>User Name</th>
<th>Email</th>
<th>Phone</th>
</tr>
<?php $num=1; while($row = mysql_fetch_array($result))
{ ?>
<tr>
<td><?php echo $num++; ?> </td>
<td><?php echo $row['uname']; ?> </td>
<td><?php echo $row['email']; ?> </td>
<td><?php echo $row['phno']; ?> </td>
</tr>
<?php }?>
</table>
</center>
/body>
</html>
<?php } ?>
OUTPUT:
24. Write a PHP which does the following job: Insert the details of the 3
or 4 users who register with the web site by using registration form.
Authenticate the user when he/she submits the login form using the user
name and password from the database.
login.html
<html>
<head>
<title> Sign-in Page </title>
<script language="javascript">
function validate()
{
var nam = document.f1.uname.value; if(nam=="")
{
alert("Please enter name");
document.f1.uname.focus; return false;
}
var pwd = document.f1.pass.value;
if(pwd=="")
{
alert("Please enter Password");
document.f1.pass.focus;
return false;
}
}
</script>
</head>
<body>
<br/><br/><br/>
<center>
<form name="f1" action="validation.php" method="post"
onsubmit="javascript:return
validate()">
<table border="3" cellpadding="0" cellspacing="0">
<tr>
<td>
<table cellspacing="10">
<tr>
<td colspan="2" align="center"><h2><u>Login Page<u></h2></td>
</tr>
<tr>
<td>User Name</td>
<td><input type="text" name="uname" size="15"></td>
</tr><tr>
<td>Password</td>
<td><input type="password" name="pass" size="15"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="submit"></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
validation.php
<?php
$conn = mysql_connect("localhost","root","");
if($conn)
echo "<h4>Connected to Database !!";
else
echo "Failed to Connect:".mysql_error()."</h4>";
mysql_select_db("test",$conn) or die("No Database
existing:".mysql_error());
if(isset($_POST['uname']))
{
$uname=$_POST['uname'];
$pass=$_POST['pass'];
echo"<br/><br/><br/>
<p align='right'><a href='login.html'>[Login Page]</a></p>";
$res = mysql_query("select * from registration where uname='$uname'
and pass ='$pass'");
$count=mysql_num_rows($res);
if($count)
echo "<br/><br/><br/><h2 align='center'>Welcome $uname!!!!</h2>";
else
echo "<br/><br/><br/><h2 align='center'>Invalid
Username/Password/Try again!!!!!</h2>";
}
?>
OUTPUT:
25. Create tables in the database which contain the details of items
(books in our case like Book name, Price, Quantity, Amount) of each
category. Modify your catalogue page (week 2)in such a way that you
should connect to the database and extract data from the tables and
display them in the catalogue page using PHP
Catalogue table
bkEntryForm.html
<html>
<head>
<title> Catalogue Page</title>
<script language="javascript">
function validate()
{
var nam = document.f1.bname.value;
if(nam=="")
{
alert("Please enter Book name");
document.f1.bname.focus();
return false;
}
var auth = document.f1.auth.value;
if(auth=="")
{
alert("Please enter Author Name");
document.f1.auth.focus();
return false;
}
var publ = document.f1.publ.value;
if(publ=="")
{
alert("Please enter Publisher name");
document.f1.publ.focus();
return false;
}
var isbn = document.f1.isbn.value;
if(isbn=="")
{
alert("Please enter ISBN Number");
document.f1.isbn.focus();
return false;
}
var ed = document.f1.edi.value;
if(ed=="")
{
alert("Please enter book edition");
document.f1.edi.focus();
return false;
}
var cost = document.f1.cost.value;
if(cost=="")
{
alert("Please enter cost of the book");
document.f1.cost.focus();
return false;
}
var qty = document.f1.qty.value;
if(qty=="")
{
alert("Please enter number of books you want!!!");
document.f1.qty.focus();
return false;
}
}
</script>
</head>
<body>
<br/><br/><br/>
<center>
<form name="f1" action="Catalogue.php" method="post"
onsubmit="javascript:return validate()">
<table border="3" cellpadding="0" cellspacing="0">
<tr>
<td>
<table cellspacing="5">
<tr>
<td colspan="2" align="center"><h2>
<u>Book Details Entry Form</u></h2></td>
</tr>
<tr>
<td>Book Name</td>
<td><input type="text" name="bname" size="50"></td>
</form>
</tr>
<tr>
<td>Author</td>
<td><input type="text" name="auth" size="50"></td>
</tr>
<tr>
<td>Publication</td>
<td><input type="text" name="publ" size="50"></td>
</tr>
<tr>
<td> ISBN</td>
<td><input type="text" name="isbn" size="15"></td>
</tr>
<tr>
<td> Edition</td>
<td><input type="text" name="edi" size="15"></td>
</tr>
<tr>
<td> Cost</td>
<td>$<input type="text" name="cost" size="5"></td>
</tr>
<tr>
<td> Quantity</td>
<td><input type="text" name="qty" size="3"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit"
value="submit"></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
Catalogue.php
<?php
$conn = mysql_connect("localhost","root","");
if($conn)
echo "Connected to database!!!";
else
echo "Failed to Connect:".mysql_error();
mysql_select_db("test",$conn) or die("No Database
existing:".mysql_error());
if(isset($_POST['bname']))
{
$bname=$_POST['bname'];
$auth=$_POST['auth'];
$publ=$_POST['publ'];
$isbn=$_POST['isbn'];
$edi=$_POST['edi'];
$cost=(float)$_POST['cost'];
$qty=(int)$_POST['qty'];
echo"<br/><br/><br/><p align='right'><a href='bkEntryForm.html'>
[Book Entry Page]</a></p>";
$query = "INSERT INTO catalogue
VALUES('$bname','$auth','$publ','$isbn','$edi','$cost','$qty')";
mysql_query($query);
$result = mysql_query("select * from catalogue");
?>
<html>
<body><center>
<font face="verdana" size="4">
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<th colspan="8" align="center">Books List</td>
</tr>
<tr>
<th>S.No.</th>
<th>Title</th>
<th>Author</th>
<th> Publication</th>
<th>ISBN</th>
<th>Edition</th>
<th>Cost</th>
<th>Quantity</th>
</tr>
<?php $num=1; while($row = mysql_fetch_array($result))
{ ?>
<tr>
<td align="center"><?php echo $num++; ?> </td>
<td align="left"><?php echo $row['bname']; ?> </td>
<td align="left"><?php echo $row['auth']; ?> </td>
<td align="left"><?php echo $row['publ']; ?> </td>
<td align="center"><?php echo $row['isbn']; ?> </td>
<td align="center"><?php echo $row['edi']; ?> </td>
<td align="center"><?php echo $row['cost']; ?> </td>
<td align="center"><?php echo $row['qty']; ?> </td>
</tr>
<?php }?>
</table>
</center>
</body>
</html>
<?php } ?>
OUTPUT:
26. HTTP is a stateless protocol. Session is required to maintain the
state.
The user may add some items to cart from the catalog page. He can
check the cart page for the selected items. He may visit the catalogue
again and select some more items. Here our interest is the selected items
should be added to the old cart rather than a new cart. Multiple users can
do the same thing at a time (i.e., from different systems in the LAN
using the ip-address instead of local host). This can be achieved through
the use of sessions. Every user will have his own session which will be
created after his successful login to the website. When the user logs out
his session should get invalidated (by using the method session.
invalidate () ).
Modify your catalogue and cart PHP pages to achieve the above
mentioned functionality using sessions.
style.css
body
{
width:800px;
}
.txt-heading
{
padding: 5px 10px;
font-size:1.1em;
font-weight:bold;
color:#999;
}
.btnRemoveAction
{
color:#D60202;
border:0;
padding:2px 10px;
font-size:0.9em;
}
#btnEmpty
{
background-color:#D60202; border:0;
padding:1px 10px; color:#FFF;
font-size:0.8em; font-weight:normal; float:right;
text-decoration:none;
}
.btnAddAction
{
background-color:#79b946;
border:0;
padding:3px 10px;
margin-left:1px;
}
#shopping-cart
{
border-top: #79b946 2px solid;
margin-bottom:30px;
}
#shopping-cart .txt-heading
{
background-color: #D3F5B8;
}
#shopping-cart table
{
width:100%;
background-color:#F0F0F0;
}
#shopping-cart table td
{
background-color:#FFFFFF;
}
.cart-item
{
border-bottom: #79b946 1px dotted;
padding: 10px;
}
#product-grid
{
border-top: #F08426 2px solid;
margin-bottom:30px;
}
#product-grid .txt-heading
{
background-color: #FFD0A6;
}
.product-item {
float:left; background:#F0F0F0;
margin:15px; padding:5px;
}
.product-item div
{
text-align:center;
margin:10px;
}
.product-price
{
color:#F08426;
}
.product-image {
height:100px;
background-color:#FFF;
}
dbController.php
<?php
class DBController {
private $host = "localhost";
private $user = "root";
private $password = "";
private $database = "test";
function construct() {
$conn = $this->connectDB();
if(!empty($conn)) {
$this->selectDB($conn);
}
}
function connectDB() {
$conn = mysql_connect($this->host,$this->user,$this->password);
return $conn;
}
function selectDB($conn) {
mysql_select_db($this->database,$conn);
}
function runQuery($query) {
$result = mysql_query($query);
while($row=mysql_fetch_assoc($result)) {
$resultset[] = $row;
}
if(!empty($resultset))
return $resultset;
}
index.php
<?php session_start();
require_once("dbcontroller.php");
$db_handle = new DBController();
if(!empty($_GET["action"]))
{
switch($_GET["action"]) {
case "add":
if(!empty($_POST["quantity"])) {
$productByCode = $db_handle->runQuery("SELECT * FROM
tblproduct WHERE code='" . $_GET["code"] . "'");
$itemArray = array($productByCode[0]
["code"]=>array('name'=>$productByCode[0]["name"],
'code'=>$productByCode[0]["code"], 'quantity'=>$_POST["quantity"],
'price'=>$productByCode[0]["price"]));
if(!empty($_SESSION["cart_item"])) {
if(in_array($productByCode[0]["code"],$_SESSION["cart_item"])) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($productByCode[0]["code"] == $k)
$_SESSION["cart_item"][$k]["quantity"] = $_POST["quantity"];
} else {
$_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],
$itemArray);
}
else{
$_SESSION["cart_item"] = $itemArray;
}
}
break;
case "remove":
if(!empty($_SESSION["cart_item"]))
{ foreach($_SESSION["cart_item"] as $k => $v) {
if($_GET["code"] == $k)
unset($_SESSION["cart_item"][$k]);
if(empty($_SESSION["cart_item"])) unset($_SESSION["cart_item"]);
}
}
break;
case "empty":
unset($_SESSION["cart_item"]);
break;
}
}
?>
<HTML>
<HEAD>
<TITLE>Simple PHP Shopping Cart</TITLE>
<link href="style.css" type="text/css" rel="stylesheet" />
</HEAD>
<BODY>
<div id="shopping-cart">
<div class="txt-heading">Shopping Cart <a id="btnEmpty"
href="index.php?action=empty">Empty Cart</a></div>
<?php
if(isset($_SESSION["cart_item"])){
$item_total = 0;
?>
<table cellpadding="10" cellspacing="1">
<tbody>
<tr>
<th><strong>Name</strong></th>
<th><strong>Code</strong></th>
<th><strong>Quantity</strong></th>
<th><strong>Price</strong></th>
<th><strong>Action</strong></th>
</tr>
<?php
foreach ($_SESSION["cart_item"] as $item){
?>
<tr>
<td><strong><?php echo $item["name"]; ?></strong></td>
<td><?php echo $item["code"]; ?></td>
<td align=center><?php echo $item["quantity"]; ?></td>
<td align=right><?php echo "$".$item["price"]; ?></td>
<td><a href="index.php?action=remove&code=<?php echo
$item["code"];
?>" class="btnRemoveAction">Remove Item</a></td>
</tr>
<?php
$item_total += ($item["price"]*$item["quantity"]);
}
?>
<tr>
<td colspan="5" align=right><strong>Total:</strong> <?php echo "$".
$item_total; ?></td>
</tr>
</tbody>
</table>
<?php
}
?>
</div>
<div id="product-grid">
<div class="txt-heading">Products</div>
<?php
$product_array = $db_handle->runQuery("SELECT * FROM tblproduct
ORDER BY id ASC"); if (!empty($product_array)) {
foreach($product_array as $key=>$value){
?>
<div class="product-item">
<form method="post" action="index.php?action=add&code=<?php echo
$product_array[$key]["code"]; ?>">
<div class="product-image"><img src="<?php echo
$product_array[$key]["image"];
?>" height="100" width="75"></div>
<div><strong><?php echo $product_array[$key]["name"];
?></strong></div>
<div class="product-price"><?php echo "$".$product_array[$key]
["price"]; ?></div>
<div><input type="text" name="quantity" value="1" size="2" /><input
type="submit" value="Add to cart" class="btnAddAction" /></div>
</form>
</div>
<?php
}
}
?>
</div>
</BODY>
</HTML>
OUTPUT:
Empty Cart
After adding items to cart
After modification