php - II
php - II
<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>
<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>
<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;
<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;
}
}
<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>";
}
}
<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';
}
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;
}
}
?>
<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;
}
}
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>
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>
<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>
<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>
<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>
<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>
<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'];
</body>
</html>