Sunteți pe pagina 1din 21

Program using controls and functions

<html>
<head>
<title>Controls And Functions</title>
</head>
<body>
<form action="index.php" method="post">
<table cellpadding="5px" align="center">
<tr>
<td>Name</td>
<td><input type="text" name="nam"></td>
</tr>
<tr>
<td>Age</td>
<td><input type="text" name="age" ></td>
</tr>
<tr>
<td><input type="submit" name="submit"
value="Registar"></td>
<td><input type="reset" name="reset" Value="Clear
All"></td>
</tr>
</table>
</form>
<?php
if(isset($_POST["submit"]))
{
validate($_POST["nam"],$_POST["age"]);
}

function validate($name,$age)
{
if($age>=18)
{
echo $name." Age $age You are Eligible For Voting...";
}
else
{
echo $name." Age $age You are Not Eligible For Voting...";
}
}
?>
</body>
</html>
Program using controls and functions

Output
Message passing mechanism between pages
Index.php
<html>
<head>
<title>Message Passing Mechanism</title>
</head>
<body>
<h1 align="center">Student Registration</h1>
<form action="secondpage.php" method="post">
<table cellpadding="5px" align="center">
<tr>
<td>Name</td>
<td><input type="text" name="nam"></td>
</tr>
<tr>
<td>Age</td>
<td><input type="text" name="age" ></td>
</tr>
<tr>
<td>Department</td>
<td><input type="text" name="dep" ></td>
</tr>
<tr>
<td><input type="submit" name="submit"
value="Registar"></td>
<td><input type="reset" name="reset" Value="Clear
All"></td>
</tr>
</table>
</form>
<?php
if(isset($_GET["err"]))
{
echo "<i style='color:red;'>".$_GET["err"]."</i>";
}
?>
</body>
</html>
secondpage.php

<?php

if(isset($_POST["submit"]))
{
$a="";
if($_POST["nam"]!=""&&$_POST["age"]!=""&&$_POST["dep"]!="")
{
$a.="<h1>Student Registration</h1>";
$a.="<table border='1px' cellpadding='5px'>";
$a.="<tr><td>Name</td><td>".$_POST["nam"]."</td></tr>";
$a.="<tr><td>Age</td><td>".$_POST["age"]."</td></tr>";
$a.="<tr><td>Department</td><td>".$_POST["dep"]."</td></tr>";
$a.="<table>";
echo $a;
}
else
{
header("location:first.php?err=Enter All Feilds");
}
}
else
{
header("location:first.php?err=Access Denied");
}

?>
Output
String Functions and Arrays
<html>
<head>
<title>Strings And Array</title>
</head>
<body>
<form action="index.php" method="post">
Enter The Names :
<input type="text" name="nam">
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST["submit"]))
{
$names=explode(",",$_POST["nam"]);
echo "<br>Array Format : ";print_r($names);
echo "<br><br>";
foreach($names as $a)
{
echo "<br>Upper case : ".strtoupper($a);
echo "<br>Lower case : ".strtolower($a);
echo "<br>Reverse : ".strrev($a);
echo "<br><br>";
}
asort($names);
echo "<br>Name In Ascending Order ";
echo "<br><br>";
foreach($names as $a)
{

echo "<br>".$a;
}
echo "<br><br>";
rsort($names);
echo "<br>Name Reverse Sort ";
echo "<br><br>";
foreach($names as $a)
{
echo "<br>".$a;
}
}
else
{
echo "Enter The Name List Separate By Comma(,)";
}
?></body> </html>
Output

Array Format : Array ( [0] => sun [1] => mon [2] => tue [3] => wed [4] => thu [5] => fri [6] =>
sat )

Upper case : SUN


Lower case : sun
Reverse : nus

Upper case : MON


Lower case : mon
Reverse : nom

Upper case : TUE


Lower case : tue
Reverse : eut

Upper case : WED


Lower case : wed
Reverse : dew

Upper case : THU


Lower case : thu
Reverse : uht
Upper case : FRI
Lower case : fri
Reverse : irf

Upper case : SAT


Lower case : sat
Reverse : tas

Name In Ascending Order

fri
mon
sat
thu
tue
wed
sun

Name Reverse Sort

sun
wed
tue
thu
sat
mon
fri
Parsing Functions using Tokenizing
Index.php

<?php
$parser=xml_parser_create();

function char($parser,$data)
{
echo $data;
}

xml_set_character_data_handler($parser,"char");
$fp=fopen("test.xml","r");
while ($data=fread($fp,filesize("test.xml")))
{
xml_parse($parser,$data,feof($fp)) or
die (sprintf("XML Error: %s at line %d",
xml_error_string(xml_get_error_code($parser)),xml_get_current_line_number($parser)));
}

xml_parser_free($parser);
?>

Test.xml

<?xml version="1.0" encoding="UTF-8"?>


<note>
<to>Hai</to>
<from>Welcome</from>
<heading>to</heading>
<body>PHP world!</body>
</note>
Output

Hai Welcome to PHP world!


Program using Session
Create.php

<?php

// Start the session

session_start();

?>

<!DOCTYPE html>

<html>

<body>

<?php

// Set session variables

$_SESSION["name"] = "Rani";

$_SESSION["depart"] = "BCA";

echo "Session variables are set.";

?>

</body>

</html>
Output

Session In PHP

1. Create Session Variable


2. Get Variable and Its Values
3. Destroy Variable

Create Session Variable

Session variables are set.

Get Variable and Its Values

Student Name : Rani.


Department : BCA.
Array ( [name] => Rani [depart] => BCA )

Destroy Variable

All The Session Values Destroyed


Program using cookie and session

Index.php

<!DOCTYPE html>
<html>
<title>Session and Cookies</title>
<body>
<h1>Session And Cookies</h1>
<ol>
<li><a href="setcookie.php" target="_blank">Set Cookie Values</a></li>
<li><a href="view.php" target="_blank">Set Session Using Cookie</a></li>
</ol>

</body>
</html>

Setcookie.php

<!DOCTYPE html>
<?php
session_start();
$cookie_name = "Tutor";
$cookie_value = "Joes";
setcookie($cookie_name, $cookie_value, time()+1000, "/");
?>
<html>
<body>

<?php
if(!isset($_COOKIE[$cookie_name]))
{
echo "Cookie value is not set!";
// remove all session variables
session_unset();

// destroy the session


session_destroy();
}
else
{
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
$_SESSION["name"] = $_COOKIE[$cookie_name];
}
?>
</body> </html>
View.php
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// Echo session variables that were set on previous page
if(isset($_SESSION["name"]))
{
echo "Welcome ".$_SESSION["name"].".<br>";

}
else
{
echo "Session Variables is already destroy.";
}
?>

</body>
</html>
Output

Session and Cookies


1. Set Cookie Values
2. Set Session Using Cookie

Set cookie values

Cookie 'Tutor' is set!


Value is: Joes

Set session using cookie

Welcome Joes.
Checking file system functions, network functions, date and time functions

Index.php

<!DOCTYPE html>
<html>
<title>PHP Functions</title>
<body>
<ol>
<li><a href="regular.php" target="_blank">Regular Expression
Functions</a></li>
<li><a href="hash.php" target="_blank">Hash Function</a></li>
<li><a href="html.php" target="_blank">HTML
Function</a></li>
</ol>
</body>
</html>

Regular.php

<?php
// create a string
$string = 'abcdefghijklmnopqrstuvwxyz0123456789';

if(preg_match("/abc/", $string))
{

echo 'The string has abc.<br>';


}

if(preg_match("/^abc/", $string))
{
echo 'The string begins with abc.<br>';
}
else
{
echo 'No match found .<br>';
}

if(preg_match("/89\z/i", $string))
{
echo 'The string ends with 89';
}
else
{
echo 'No match found';
}

?>

Hash.php

<?php
echo "<h1>Sha1 HASHING</h1>";
$str = "Hello";
echo sha1($str);

if (sha1($str) == "f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0")
{
echo "<br>Hello world!";
}

echo "<h1>MD5 HASHING</h1>";


$str = "Hello";
echo md5($str);

if (md5($str) == "8b1a9953c4611296a827abf8c47804d7")
{
echo "<br>Hello world!";
}
?>

Html.php

<!DOCTYPE html>
<html>
<body>

<?php
$str = "Jane & 'Tarzan'";
echo htmlentities($str, ENT_COMPAT); // Will only convert double quotes
echo "<br>";
echo htmlentities($str, ENT_QUOTES); // Converts double and single quotes
echo "<br>";
echo htmlentities($str, ENT_NOQUOTES); // Does not convert any quotes
?>
</body>
</html>
Output

1. Date And Time Functions


2. Network Function (works only through Internet)
3. File Function

Date And Time Functions

Data in Short Format : 20/01/2015


Data in Long Format : 20 January 2015

Tuesday 20th January 2015 02:17:43 PM

Network Function (works only through Internet)

Array ( [0] => alt3.aspmx.l.google.com [1] => alt1.aspmx.l.google.com [2] =>


alt4.aspmx.l.google.com [3] => aspmx.l.google.com [4] => alt2.aspmx.l.google.com )
localhost127.0.0.1
joe
-655395948
216.239.115.148

File Function

File is Present.
Write File Content.
Read File Content.
Hello World. Testing!Hello World. Testing!Hello World. Testing!Hello World. Testing!Hello
World. Testing!Hello World. Testing!
File Size :126
Last modified: January 20 2015 08:50:32.
Checking regular expression, html functions and hashing functions

Index.php

<!DOCTYPE html>
<html>
<title>PHP Functions</title>
<body>
<ol>
<li><a href="date.php" target="_blank">Date And Time
Functions</a></li>
<li><a href="network.php" target="_blank">Network Function
(works only through Internet)</a></li>
<li><a href="file.php" target="_blank">File Function</a></li>
</ol>
</body>
</html>

Date.php

<html>
<head>
<title>Date and Functions In PHP</title>
</head>
<body>
<?php
echo "Data in Short Format : ".date("d/m/Y");
echo"<br>";
echo "Data in Long Format : ".date("d F Y");
echo"<br>";
date_default_timezone_set("Asia/Kolkata");

echo"<br>";
echo date("l dS F Y h:i:s A");

?>
<body>
</html>

Network.php

<?php
$hosts = array();
$ret = getmxrr('google.com', $hosts);
if ($ret) {
print_r($hosts);
} else {
echo 'MX retrieval failed';
}
echo "<br>";
echo gethostbyaddr('127.0.0.1');
echo "<br>";
echo gethostbyname('joe');
echo "<br>";
echo ip2long('216.239.115.148');
echo "<br>";
echo long2ip(-655395948);

$data = dns_get_record('google.com');
print_r($data);

?>

File.php

<?php
if(file_exists("test.txt"))
{
echo "File is Present.<br>";
}
else
{
echo "File Not Found.<br>";
}

echo "Write File Content.<br>";


file_put_contents("test.txt","Hello World. Testing!",FILE_APPEND);

echo "Read File Content.<br>";


echo file_get_contents("test.txt");
echo "<br>File Size :".filesize("test.txt");

echo "<br />";


echo "Last modified: ".date("F d Y H:i:s.",filemtime("test.txt"));
?>
Output

1. Regular Expression Functions


2. Hash Function
3. HTML Function

Regular Expression Functions

The string has abc.


The string begins with abc.
The string ends with 89

Hash function

Sha1 HASHING

f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0
Hello world!

MD5 HASHING

8b1a9953c4611296a827abf8c47804d7
Hello world!

HTML function

Jane & 'Tarzan'


Jane & 'Tarzan'
Jane & 'Tarzan'

S-ar putea să vă placă și