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

php - II

The document outlines a practical list for PHP programming, detailing various tasks such as using server variables, getting browser types, dumping form data, and creating classes and objects. It includes code examples for each task, demonstrating concepts like data validation, method overriding, and database operations. The practical exercises are aimed at enhancing PHP programming skills for the summer of 2023.

Uploaded by

Paras Wadher
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

php - II

The document outlines a practical list for PHP programming, detailing various tasks such as using server variables, getting browser types, dumping form data, and creating classes and objects. It includes code examples for each task, demonstrating concepts like data validation, method overriding, and database operations. The practical exercises are aimed at enhancing PHP programming skills for the summer of 2023.

Uploaded by

Paras Wadher
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

PHP – II PRACTICAL LIST – SUMMER 2023

1. WAP TO DEMONSTRATE USE OF SERVER VARIABLES IN PHP

<?php #Practical 1 : Use of Server Variables in php?>


Php_self : <?PHP Echo $_SERVER['PHP_SELF'];?></br>
Auth_type:<?PHP Echo $_SERVER['AUTH_TYPE'];?></br>
Document_root :<?PHP Echo $_SERVER['DOCUMENT_ROOT'];?></br>
Gateway_interface : <?PHP Echo $_SERVER['GATEWAY_INTERFACE'];?></br>
<?PHP Echo $_SERVER['SERVER_ADDR'];?></br>
<?PHP Echo $_SERVER['SERVER_NAME'];?></br>
<?PHP Echo $_SERVER['SERVER_SOFTWARE'];?></br>
<?PHP Echo $_SERVER['SERVER_PROTOCOL'];?></br>
<?PHP Echo $_SERVER['REQUEST_TIME'];?></br>
<?PHP Echo $_SERVER['HTTP_USER_AGENT'];?></br>
<?PHP Echo $_SERVER['REMOTE_HOST'];?></br>
<?PHP Echo $_SERVER['REMOTE_ADDR'];?></br>
<?PHP Echo $_SERVER['REMOTE_PORT'];?></br>
<?PHP Echo $_SERVER['SERVER_ADMIN'];?></br>
<?PHP Echo $_SERVER['SERVER_PORT'];?></br>
<?PHP Echo $_SERVER['SERVER_SIGNATURE'];?></br>
<?PHP Echo $_SERVER['SCRIPT_NAME'];?></br>
<?PHP Echo $_SERVER['REQUEST_METHOD']; ?></br>

2. WAP TO GET BROWSER TYPE IN PHP

<html>
<head>
<title>Practical 2 : Getting Browser Type</title>
</head>
<body>
<h1>Practical 2 : Getting Browser Type</h1>
<br>
<?php
if(strpos($_SERVER["HTTP_USER_AGENT"], "MSIE")){
echo("<h1>You are using Miscrosoft Internet Explorer</h1>");
}
elseif(strpos($_SERVER["HTTP_USER_AGENT"], "Firefox")){
echo("<h1>You are using Mozella Firefox Browser</h1>");
}
elseif(strpos($_SERVER["HTTP_USER_AGENT"], "Safari")){
echo("<h1>You are using Apple Safari Browser</h1>");
}
elseif(strpos($_SERVER["HTTP_USER_AGENT"], "Chrome")){
echo("<h1>You are using Google Chrome Browser</h1>");
}
else{
echo("We are not able to verify your browser type? Please try again with other browser!!");
}
?>
</body>
</html>

3. WAP TO DUMP FORM DATA ALL AT ONCE IN PHP

<html>

<head>
<title>
Practical 3: Dumping form data all at once
</title>
</head>

<body>
<form method="POST" action="dump.php">
Enter your name = <input name="name" type="text"><br> Enter your Mobile number = <input
name="mobile" type="number"><br> Enter your BCA Semester = <input name="sem" type="number"><br>
Select your Favorite Sub'ject's<br>
<select name="subjects[]" multiple>
<option>CG-II</option>
<option>Java</option>
<option>ASP.net</option>
<option>ST</option>
<option>PHP-II</option>
<option>DCN-II</option>
</select><br>
<input type="submit" value="submit">
</form>
</body>

</html>

4. WAP TO PERSIST USER DATA AND CHECKING REQUIRED DATA

<html>
<head>
<title>
Program 4 :Persisting user data and checking Required data
</title>
</head>
<body>
<h1>Program4: Persisting user data and checking Required data</h1>
<?php
$errors_array = array();
if(isset($_REQUEST["welcome_already_seen"])){
check_data();
if(count($errors_array) != 0){
show_errors();
show_welcome();
} else {
handle_data();
}
}
else {
show_welcome();
}
function check_data()
{
global $errors_array;

if($_REQUEST["first"] == "") {
$errors_array[] = "<font color='red'>Enter your first name</font>";
}
if($_REQUEST["last"] == "") {
$errors_array[] = "<font color='red'>Enter your last name</font>";
}
}
function show_errors()
{
global $errors_array;

foreach ($errors_array as $err){


echo $err, "<br>";
}
}
function handle_data()
{
echo "Here is your first name: ";
echo $_REQUEST["first"];
echo "<br>Here is your last name: ";
echo $_REQUEST["last"];
}
function show_welcome()
{
$first_name = isset($_REQUEST["first"]) ? $_REQUEST["first"] : "";
$last_name = isset($_REQUEST["last"]) ? $_REQUEST["last"] : "";
echo "<form method='post'>";
echo "Enter your first name: ";
echo "<input name='first' type='text' value='", $first_name, "'>";
echo "<br>";
echo "<br>";
echo "Enter your last name: ";
echo "<input name='last' type='text' value='", $last_name, "'>";
echo "<br>";
echo "<br>";
echo "<input type='submit' value='Submit'>";
echo "<input type=hidden name='welcome_already_seen'
value='already_seen'>";
echo "</form>";
}
?>
</body>
</html>

5. WAP TO PERFORM DATA VALIDATION.

6. WAP TO CREATE CLASS & AN OBJECT IN PHP

<html>
<head>
<title>
Practical 6: Creating Class & an object
</title>
</head>

<body>
<h1>Practical 6: Creating Class & an object</h1>
<?php
class Person
{
var $name;
function set_name($data)
{
$this->name = $data;
}

function get_name()
{
return $name;
}
}

$ralph = new Person;


$ralph->set_name("VMV_college");

echo "The name of your College is ", $ralph->get_name(), ".";


?>
</body>
</html>
7. WAP TO DEMONSTRATE USE OF A CONSTRUCTOR & DESTRUCTOR IN PHP

<html>
<head>
<title>
Practical 7 : Using a Constuctor & Destructor
</title>
</head>

<body>
<h1>Practical 7 : Using a Constuctor & Destructor</h1>
<?php

class Person
{
var $name;

function __construct($data)
{
echo "Constructing ", $data, "...<br>";
$this->name = $data;
}

function set_name($data)
{
$this->name = $data;
}

function get_name()
{
return $this->name;
}

function __destruct()
{
echo "Destructing ", $this->name, "...<br>";
}
}

$dan = new Person("VMV_college");

echo "The name of your College is ", $dan->get_name(), ".<br>";


?>
</body>
</html>

8. WAP TO USE AUTOLOADING CLASSES IN PHP

<html>
<head>
<title>
Practical 8 : Autoloading classes in PHP
</title>
</head>

<body>
<h1>
Practical 8 : Autoloading classes in PHP
</h1>

<?php
function __autoload($class_name)
{
require $class_name . '.php';
}

$Var1 = new Friend;


$Var1->set_name("VMV_college");
$Var1->set_message("Hello from VMV_college.");

echo "The name of your College is ", $Var1->get_name(), ".<br>";


echo $Var1->get_name(), " says \"", $Var1->speak(), "\"<br>";
?>
</body>
</html>

FRIEND.PHP
<?php
class Friend extends Person
{
var $message;

function set_message($msg)
{
$this->message = $msg;
}

function speak()
{
echo $this->message;
}
}
?>
PERSON.PHP
<?php
class Person
{
var $name;

function set_name($data)
{
$this->name = $data;
}

function get_name()
{
return $this->name;
}
}
?>

9. WAP TO DEMONSTRATE METHOD OVERRIDING IN PHP

<html>
<head>
<title>
Practical 9 : Overriding methods
</title>
</head>

<body>
<h1>
Practical 9 : Overriding methods
</h1>
<?php
class Person
{
var $name;

function set_name($data)
{
$this->name = $data;
}

function get_name()
{
return $this->name;
}
}

class College extends Person


{
var $name;

function speak()
{
echo $this->name, " is speaking<br>";
}

function set_name($data)
{
$this->name = strtoupper($data);
}
}
echo "Creating your new friend...<BR>";
$friend = new College;
$friend->set_name("vmv_college");
$friend->speak();

?>
</body>
</html>

10. WAP TO DEMONSTRATE METHOD OVERLOADING IN PHP


 <html>
 <head>
 <title>
 Practical 10 : Overloading methods
 </title>
 </head>

 <body>
 <h1>
 Practical 10 : Overloading methods
 </h1>
 <?php
 class Friend
 {
 var $name;
 var $message;

 function speak()
 {
 echo $this->name, " says \"", $this->message, "\"<br>";
 }

 function set_message($msg)
 {
 $this->message = $msg;
 }

 function __call($method, $arguments)
 {
 if($method == "set_name"){
 if(count($arguments) == 1){
 $this->name = $arguments[0];
 }
 if(count($arguments) == 2){
 $this->name = $arguments[0];
 $this->message = $arguments[1];
 }
 }
 }
 }

 echo "Creating your new friend...<br>";
 $friend = new Friend;
 $friend->set_name("VMV");
 $friend->set_message("I am a College");
 $friend->speak();

 $friend->set_name("College", "You are here!");
 $friend->speak();
 ?>
 </body>
 </html>

11. WAP TO CREAT A NEW DATABASE, NEW TABLE AND INSERTING VALUES

 <html>
 <head>
 <title>
 Practical NO 11 : Creating a new database, new table and inserting values.
 </title>
 </head>

 <body>
 <h1>
 Practical NO 11 : Creating a new database, new table and inserting values.
 </h1>

 <?php

 $connection = mysql_connect("localhost","demo","")
 or die ("Couldn't connect to server");

 $query = "CREATE DATABASE IF NOT EXISTS canteen";

 $result = mysql_query($query)
 or die("Query failed: " . mysql_error());

 $db = mysql_select_db("canteen", $connection)
 or die ("Couldn't select database");

 $query = "CREATE TABLE snacks (name VARCHAR(20), number
 VARCHAR(20))";

 $result = mysql_query($query)
 or die("Query failed: " . mysql_error());

 $query = "INSERT INTO snacks (name, number)
 VALUES('Samosa', '25')";

 $result = mysql_query($query)
 or die("Query failed: " . mysql_error());

 $query = "INSERT INTO snacks (name, number)
 VALUES('Kachori', '25')";

 $result = mysql_query($query)
 or die("Query failed: " . mysql_error());

 $query = "INSERT INTO snacks (name, number)
 VALUES('Dhokla', '20')";

 $result = mysql_query($query)
 or die("Query failed: " . mysql_error());

 $query = "INSERT INTO snacks (name, number)
 VALUES('Sambar_wada', '30')";

 $result = mysql_query($query)
 or die("Query failed: " . mysql_error());

 $query = "INSERT INTO snacks (name, number)
 VALUES('Idli', '15')";

 $result = mysql_query($query)
 or die("Query failed: " . mysql_error());

 $query = "SELECT * FROM snacks";

 $result = mysql_query($query)
 or die("Query failed: " . mysql_error());

 echo "<table border='1'>";
 echo "<tr>";
 echo "<th>Name</th><th>Number</th>";
 echo "</tr>";

 while ($row = mysql_fetch_array($result))
 {
 echo "<tr>";
 echo "<td>", $row['name'], "</td><td>",
 $row['number'], "</td>";
 echo "</tr>";
 }
 echo "</table>";

 mysql_close($connection);
 ?>
 </body>
 </html>

12. WAP TO CREAT A NEW TABLE AND DELETING ENTRIES.

 <html>
 <head>
 <title>
 Practical 12: Creating a new table and deleting entries.
 </title>
 </head>
 <body>
 <h1>
 Practical 12: Creating a new table and deleting entries.
 </h1>
 <?php
 $connection = mysql_connect("localhost","demo","")
 or die ("Couldn't connect to server");

 $db = mysql_select_db("canteen",$connection)
 or die ("Couldn't select database");
 Echo "<h1>Creating Table vegetables</h1>";
 $query = "CREATE TABLE vegetables (name VARCHAR(20),
 number VARCHAR(20))";

 $result = mysql_query($query)
 or die("Query failed: " . mysql_error());

 $query = "INSERT INTO vegetables (name, number) VALUES(
 'Potato', '20')";

 $result = mysql_query($query)
 or die("Query failed: " . mysql_error());

 $query = "INSERT INTO vegetables (name, number)
 VALUES('Tomato', '40')";

 $result = mysql_query($query)
 or die("Query failed: " . mysql_error());

 $query = "INSERT INTO vegetables (name, number)
 VALUES('Chilli', '100')";

 $result = mysql_query($query)
 or die("Query failed: " . mysql_error());

 $query = "SELECT * FROM vegetables";

 $result = mysql_query($query)
 or die("Query failed: " . mysql_error());

 echo "<table border='1'>";
 echo "<tr>";
 echo "<th>Name</th><th>Number</th>";
 echo "</tr>";

 while ($row = mysql_fetch_array($result))
 {
 echo "<tr>";
 echo "<td>", $row['name'], "</td><td>",
 $row['number'], "</td>";
 echo "</tr>";
 }

 echo "</table>";
 Echo "<h1>Deleting Tomato from Table vegetables</h1>";
 $query = "DELETE FROM vegetables WHERE name = 'Tomato'";
 $result = mysql_query($query)
 or die("Query failed: " . mysql_error());

 $query = "SELECT * FROM vegetables";
 $result = mysql_query($query)
 or die("Query failed: " . mysql_error());

 echo "<TABLE BORDER='1'>";
 echo "<TR>";
 echo "<TH>Name</TH><TH>Number</TH>";
 echo "</TR>";

 while ($row = mysql_fetch_array($result))
 {
 echo "<TR>";
 echo "<TD>", $row['name'], "</TD><TD>", $row['number'], "</TD>";
 echo "</TR>";
 }

 echo "</TABLE>";
 Echo "<h1>Updating Potato price from Table vegetables</h1>";
 $query = "UPDATE vegetables SET number = 30 WHERE name = 'Potato'";
 $result = mysql_query($query)
 or die("Query failed: ".mysql_error());

 $query = "SELECT * FROM vegetables";
 $result = mysql_query($query)
 or die("Query failed: " . mysql_error());

 echo "<table border='1'>";
 echo "<tr>";
 echo "<th>Name</TH><TH>Number</th>";
 echo "</tr>";

 while ($row = mysql_fetch_array($result))
 {
 echo "<tr>";
 echo "<td>", $row['name'], "</td><td>", $row['number'], "</td>";
 echo "</tr>";
 }

 echo "</table>";

 mysql_close($connection);
 ?>
 </body>
 </html>

13. WAP TO CREATE NEW TABLE AND SORT DATA BY NAME

 <html>
 <head>
 <title>
 Practical 13 : Create new table and Sort data by name
 </title>
 </head>

 <body>
 <h1>
 Practical 13 : Create new table and Sort data by name
 </h1>

 <?php
 $connection = mysql_connect("localhost","demo","")
 or die ("Couldn't connect to server");

 $db = mysql_select_db("canteen",$connection)
 or die ("Couldn't select database");

 $query = "CREATE TABLE shakes (name VARCHAR(20),
 number VARCHAR(20))";

 $result = mysql_query($query)
 or die("Query failed: " . mysql_error());

 $query = "INSERT INTO shakes (name, number) VALUES(
 'Pineapple', '80')";

 $result = mysql_query($query)
 or die("Query failed: " . mysql_error());

 $query = "INSERT INTO shakes (name, number)
 VALUES('Chikku', '60')";

 $result = mysql_query($query)
 or die("Query failed: " . mysql_error());

 $query = "INSERT INTO shakes (name, number)
 VALUES('Banana', '50')";

 $result = mysql_query($query)
 or die("Query failed: " . mysql_error());

 $query = "INSERT INTO shakes (name, number)
 VALUES('Chocolate', '100')";

 $result = mysql_query($query)
 or die("Query failed: " . mysql_error());

 $query = "INSERT INTO shakes (name, number)
 VALUES('Apple', '120')";

 $result = mysql_query($query)
 or die("Query failed: " . mysql_error());
 Echo "Display Values of table shakes";
 $query = "SELECT * FROM shakes";

 $result = mysql_query($query)
 or die("Query failed: " . mysql_error());

 echo "<table border='1'>";
 echo "<tr>";
 echo "<th>Name</th><th>Number</th>";
 echo "</tr>";

 while ($row = mysql_fetch_array($result))
 {
 echo "<tr>";
 echo "<td>", $row['name'], "</td><td>",
 $row['number'], "</td>";
 echo "</tr>";
 }

 echo "</table>";

 Echo "Display Values of table shakes after sort by name" ;
 $query = "SELECT * FROM shakes ORDER BY name";

 $result = mysql_query($query)
 or die("Query failed: " . mysql_error());

 echo "<table border='1'>";
 echo "<tr>";
 echo "<th>Name</th><th>Number</th>";
 echo "</TR>";

 while ($row = mysql_fetch_array($result))
 {
 echo "<tr>";
 echo "<td>", $row['name'], "</td><td>",
 $row['number'], "</td>";
 echo "</tr>";
 }

 echo "</table>";

 mysql_close($connection);
 ?>
 </body>
 </html>

14. WAP FOR SESSION HIT COUNTER IN PHP

 <html>
 <head>
 <title>
 Program 15: WAP for session hit counter
 </title>
 </head>
 <body>
 <h1>
 Program 15: WAP for session hit counter
 </h1>
 Hello there. You have been here
 <?php
 session_start();
 if (!isset($_SESSION['count'])) {
 $_SESSION['count'] = 0;
 } else {
 $_SESSION['count']++;
 }
 echo $_SESSION['count'];

 ?>
 times before.
 <body>
 </html>
15. WAP TO DEMONSTRATE USE OF SHELL COMMAND IN PHP

<html>
<head>
<title> Program to demonstate use of shell command in php </title>
</head>
<body>
<h1>Program to demonstate use of shell command in php</h1>
<?php
#Program
echo " ls using exec() Command <br>";
exec('ls -l', $output, $status);
echo "print output as array";
print_r($output);
echo "<br/>";
echo "print output using foreach";
foreach($output as $value)
{
echo $value."<br />";
}
//Print the return status of the executed command
echo $status;
echo"<br>";
echo "ls command using shell_exec()";
$output = shell_exec('ls');
echo "<pre>$output</pre>";
?>
</body></html>

16. WAP TO SET A COOKIE'S WITH EXPIRATION TIME IN PHP

<html>
<head>
<title>
Pract 16: Setting a cookie's with expiration time
</title>
<?php
setcookie("message", "No worries for 30 days.", time()+60*60*24*30);
?>
</head>

<body>
<h1>
Pract 16: Setting a cookie's with expiration time
</h1>
The cookie has been set to expire in 30 days. Go to
<a href="phpgetcookie.php">phpgetcookie.php</a> next.
<body>
</html>

Phpgetcookie.php
<html>
<head>
<title>
Reading a cookie
</title>
</head>

<body>
<h1>
Reading a cookie
</h1>
The cookie says:
<?php
if (isset($_COOKIE['message'])) {
echo $_COOKIE['message'];
}
?>
<body>
</html>

17. WAP TO READ A REMOTE DIRECTORY WITH FTP

18. WAP TO CREATE AND REMOVE DIRECTORIES WITH FTP

19. WAP TO SEND AN EMAIL USING PHP.

20. WAP TO DESIGN A STUDENT REGISTRATION FORM WITH DATABASE IN PHP

<html>
<head>
<title>Student Registration Form</title>
<style>
input,textarea{width:200px}
input[type=radio],input[type=checkbox]{width:10px}
input[type=submit],input[type=reset]{width:100px}
</style>
</head>
<?php
//connectivity
$con=mysql_connect("localhost","demo","") or die("not connected");
//select database
mysql_select_db("student",$con);
extract($_POST);
if(isset($save))
{
//for date of birt get one by one and concatente
$dob=$yy."-".$mm."--".$dd;
//hobbies retrieve in an array format change array into string
$h=implode(",",$hobb);
$img=$_FILES['pic']['name'];

//To check user already exists or not


$sql=mysql_query("select email from studentdetails where email='$e'");
$return=mysql_num_rows($sql);
//if $return returns true value it means user's email already exists
if($return)
{
$msg="<font color='red'>".ucfirst($e)."already exists choose another email</font>";
}
else
{
$query="insert into studentdetails values('','$n','$e','$p','$m','$g','$h','$dob','$add','$img',now())";
mysql_query($query);
$msg= "<font color='blue'>Your data saved</font>";
}
}
?>
<body>
<form method="post" enctype="multipart/form-data" >
<table width="400" border="1">
<tr>
<td colspan="2"><?php echo @$msg; ?></td>
</tr>
<tr>
<td width="160">Enter your Name</td>
<td width="220">
<input type="text" placeholder="your first name" name="n" pattern="[a-z A-Z]*" required /></td>
</tr>
<tr>
<td>Enter your Email</td>
<td><input type="email" name="e"/></td>
</tr>
<tr>
<td>Enter your Password</td>
<td><input type="password" name="p"/></td>
</tr>
<tr>
<td>Enter your Address</td>
<td><textarea name="add"></textarea></td>
</tr>
<tr>
<td>Enter your Mobile</td>
<td><input type="text" pattern="[0-9]*" name="m" /></td>
</tr>
<tr>
<td height="23">Select your Gender</td>
<td>
Male<input type="radio" name="g" value="m"/>
Female<input type="radio" name="g" value="f"/>
</td>
</tr>
<tr>
<td>Choose your Hobbies</td>
<td>
Cricket<input type="checkbox" value="cricket" name="hobb[]"/>
Singing<input type="checkbox" value="singing" name="hobb[]"/>
Dancing<input type="checkbox" value="dancing" name="hobb[]"/>
</td>
</tr>
<tr>
<td>Choose your Profile Pic </td>
<td><input type="file" name="pic"/></td>
</tr>
<tr>
<td>Select your DOB</td>
<td>
<select name="mm">
<option value="">Month</option>
<?php
for($i=1;$i<=12;$i++)
{
echo "<option value='$i'>".$i."</option>";
}
?>
</select>
<select name="dd">
<option value="">Date</option>
<?php
for($i=1;$i<=31;$i++)
{
echo "<option value='$i'>".$i."</option>";
}
?>
</select>
<select name="yy">
<option value="">Year</option>
<?php
for($i=1900;$i<=2015;$i++)
{
echo "<option value='$i'>".$i."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="save" value="Register Me"/>
<input type="reset" value="Reset"/>
</td>
</tr>
</table>
</form>

</body>
</html>

You might also like