0% found this document useful (0 votes)
15 views

1-9 WebProgramming

Uploaded by

DivyanshuYadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

1-9 WebProgramming

Uploaded by

DivyanshuYadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

PROGRAM-1

a) Program to display various Server Information like- Server Name, Server Software, Server
protocol, CGI Revision etc.

HTML

<html>

<form action="http://localhost/cgi-bin/1a.pl">

<center>

<input type=submit value=Show_Server_Info/>

</center>

</form>

</html>

PERL FILE

#!C:\xampp\perl\bin\perl.exe

print "Content-Type: text/html;charset=iso-8859-1;\n\n";

#this is a here-document

print "<html><table border=1><tr><th>ENV_VARIABLES</th><th>Value</th></tr>";

foreach $i(sort keys %ENV)

print "<tr><td>$i</td><td>$ENV{$i}</td></tr>";

print "</table></html>";
OUTPUT
b) Program to accept UNIX command from a HTML form and to display the output of the
command executed.

HTML

<html>

<form action="http://localhost/cgi-bin/1ba.pl">

<!-- input command name-->

<input type=text name=com>

<input type=submit value=submit />

</form>

</html>

PERL FILE

#!"\xampp\perl\bin\perl.exe"

use CGI':standard';

print "Content-type:text/html\n\n";

$c=param('com');

system($c);

exit(0);
OUTPUT
PROGRAM-2

a) Program to accept the User Name and display a greeting message.

HTML

<html>

<form action="http://localhost/cgi-bin/2a.pl">

<center>

<h2>Enter your name:</h2>

<input type=text name=name>

<input type=submit value=sumbit>

</center>

</form>

</html>

PERL FILE

#!C:\xampp\perl\bin\perl.exe

use strict;

use warnings;

use CGI qw(:standard);

my $cmd=param('name');

my @greet=("Hello","Hi","Nice to meet you");

my $index=int rand scalar@greet;

print"Content-type: text/html;charset=iso-8859-1;\n\n";

print<<"here";

<html>

<center>

<h2>$cmd,$greet[$index]</h2>
</center>

</html>

here

OUTPUT
b) Program to keep track of the number of visitors visited the webpage and display the
counter with proper headings.

HTML

<html>

<body>

<form action="http://localhost/cgi-bin/2b.pl">

<input type="submit" value="Show">

</form>

</body>

</html>

PERL FILE

#!C:\xampp\perl\bin\perl.exe

use CGI':standard';

print "Content-type: text/html;charset=iso-8859-1;\n\n";


open(FILE,'<count.txt');

$count=<FILE>;

close(FILE);

$count++;

open(FILE,'>count.txt');

print FILE "$count";

print "This page has been viewed $count times";

OUTPUT
PROGRAM-3

Program to display a greeting based on the access time of the Web server. Also to verify
whether the webmaster is currently logged in.

HTML

<form action="http://localhost/cgi-bin/3.pl">

<center>

<input type="submit" value="Check status">

</center>

</form>

PERL FILE
#!C:\xampp\perl\bin\perl.exe

use CGI;

my $cgi=CGI->new;

my $cookie_name = 'webmaster_logged_in';

my $status=$cgi->cookie($cookie_name);

print "Content-type:text/html;\n\n";

if($status){

print "Logged in";

}else{

print "Logged out";

($s,$m,$h)=localtime(time);

print "<br>";

if($h<12){

print "Good Morning";

}else{

print "Good Evening";

OUTPUT
4. Program to display a digital clock which displays the
current time of the server.
<!DOCTYPE html>
<html>

<head>
<meta http-equiv="refresh" content="1">
<style>
body {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: black;
color: white;
font-size: 50px;
}
</style>
</head>

<body>
<div>
<?php
date_default_timezone_set("Asia/Calcutta");
echo date("h:i:s a");
?>
</div>
</body>

</html>

Output:
02 : 25 : 31 pm

5. Program to display the current contents of the table in


a database.
<!DOCTYPE html>
<html>
<head>
<title>Display Table Contents</title>
<style>
table {
border-collapse: collapse;
width: 80%;
margin: 20px auto;
}
table, th, td {
border: 1px solid black;
padding: 10px;
text-align: center;
}
th {
background-color: #4CAF50;
color: white;
}
td {
background-color: #f2f2f2;
}
</style>
</head>
<body>

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "books";
$table = "book";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * FROM $table";


$result = $conn->query($sql);

if ($result->num_rows > 0) {
echo "<table><tr>";

while ($fieldinfo = $result->fetch_field()) {


echo "<th>" . $fieldinfo->name . "</th>";
}
echo "</tr>";

while ($row = $result->fetch_assoc()) {


echo "<tr>";
foreach ($row as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>";
}
echo "</table>";
} else {
echo "No records found in the table.";
}

$conn->close();
?>

</body>
</html>

(NOTE: Create a database and change the $books to the database name accordingly. Same
for the table within the database)

Output:

6. Program, to insert new name and age information


entered by the user into the database
<?php
// Database credentials
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "student";

// Create a connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Handle form submission


if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve name and age from the form
$name = $_POST['name'];
$age = $_POST['age'];

// Prepare and bind


$stmt = $conn->prepare("INSERT INTO student (name, age) VALUES (?, ?)");
$stmt->bind_param("si", $name, $age); // "si" indicates the data types
(string, integer)

// Execute the query


if ($stmt->execute()) {
echo "<p>New record created successfully</p>";
} else {
echo "<p>Error: " . $stmt->error . "</p>";
}

// Close the statement


$stmt->close();
}

// Close the connection


$conn->close();
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Student Form</title>
</head>
<body>
<h2>Enter Student Details</h2>
<form action="" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<br><br>
<label for="age">Age:</label>
<input type="number" id="age" name="age" required>
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

Output:

(Check the database to see the changes made after submitting)

7. Program to query the data base and to display the


result on a webpage.
<?php
// Database credentials
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "student";

// Create a connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Initialize an empty variable for results


$resultData = [];

// Handle form submission


if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve the SQL query from the form
$query = $_POST['query'];

// Execute the query


if ($result = $conn->query($query)) {
// Fetch the results into an associative array
while ($row = $result->fetch_assoc()) {
$resultData[] = $row;
}
// Free result set
$result->free();
} else {
echo "<p>Error: " . $conn->error . "</p>";
}
}

// Close the connection


$conn->close();
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Query Student Table</title>
</head>
<body>
<h2>Execute SQL Query</h2>
<form action="" method="post">
<label for="query">SQL Query:</label><br>
<textarea id="query" name="query" rows="4" cols="50" required>
</textarea>
<br><br>
<input type="submit" value="Execute">
</form>

<?php if (!empty($resultData)): ?>


<h2>Results</h2>
<table border="1">
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<?php foreach ($resultData as $row): ?>
<tr>
<td><?php echo htmlspecialchars($row['name']); ?></td>
<td><?php echo htmlspecialchars($row['age']); ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
</body>
</html>

Output:

8. Program to accept book information viz Accession


number, title, authors, edition and publication from a
webpage and to store those in a database.
<?php
// Database credentials
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "books";
// Create a connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Handle form submission


if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve book details from the form
$access_number = $_POST['access_number'];
$title = $_POST['title'];
$authors = $_POST['authors'];
$edition = $_POST['edition'];
$publication = $_POST['publication'];

// Prepare and bind


$stmt = $conn->prepare("INSERT INTO book (access_number, title, authors,
edition, publication) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param("issis", $access_number, $title, $authors, $edition,
$publication); // "issis" indicates the data types (int, string, string,
int, string)

// Execute the query


if ($stmt->execute()) {
echo "<p>New book record created successfully</p>";
} else {
echo "<p>Error: " . $stmt->error . "</p>";
}

// Close the statement


$stmt->close();
}

// Close the connection


$conn->close();
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Insert Book</title>
</head>
<body>
<h2>Enter Book Details</h2>
<form action="" method="post">
<label for="access_number">Access Number:</label>
<input type="number" id="access_number" name="access_number"
required>
<br><br>

<label for="title">Title:</label>
<input type="text" id="title" name="title" required>
<br><br>

<label for="authors">Authors:</label>
<input type="text" id="authors" name="authors" required>
<br><br>

<label for="edition">Edition:</label>
<input type="number" id="edition" name="edition" required>
<br><br>

<label for="publication">Publication:</label>
<input type="text" id="publication" name="publication" required>
<br><br>

<input type="submit" value="Submit">


</form>
</body>
</html>

Output:
9. Program to search a book for a title given by the user
on a webpage and display the search results with proper
headings.
<?php
// Database credentials
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "books";

// Create a connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Initialize variables
$bookDetails = null;

// Handle form submission


if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve the book title from the form
$title = $_POST['title'];

// Prepare and execute the SQL query


$stmt = $conn->prepare("SELECT * FROM book WHERE title = ?");
$stmt->bind_param("s", $title);
$stmt->execute();
$result = $stmt->get_result();

// Check if any results were returned


if ($result->num_rows > 0) {
// Fetch the book details
$bookDetails = $result->fetch_assoc();
} else {
$bookDetails = false; // Book not found
}

// Close the statement


$stmt->close();
}

// Close the connection


$conn->close();
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Search Book</title>
</head>
<body>
<h2>Search for a Book</h2>
<form action="" method="post">
<label for="title">Book Title:</label>
<input type="text" id="title" name="title" required>
<br><br>
<input type="submit" value="Search">
</form>

<?php if ($bookDetails !== null): ?>


<?php if ($bookDetails): ?>
<h3>Book Details:</h3>
<p><strong>Access Number:</strong> <?php echo
htmlspecialchars($bookDetails['access_number']); ?></p>
<p><strong>Title:</strong> <?php echo
htmlspecialchars($bookDetails['title']); ?></p>
<p><strong>Authors:</strong> <?php echo
htmlspecialchars($bookDetails['authors']); ?></p>
<p><strong>Edition:</strong> <?php echo
htmlspecialchars($bookDetails['edition']); ?></p>
<p><strong>Publication:</strong> <?php echo
htmlspecialchars($bookDetails['publication']); ?></p>
<?php else: ?>
<p>The book does not exist in the database.</p>
<?php endif; ?>
<?php endif; ?>
</body>
</html>

Output:

You might also like