PHP RecordNew
PHP RecordNew
(AUTONOMOUS)
(Affiliated to Bharathiar University )
Accredited at the ‘A+ level by NAAC with 3.38 out of 4 (3rd Cycle)
PRACTICAL RECORD
OPEN SOURCE COMPUTING LAB
OCTOBER 2023
II-MCA
2022-2024 BATCH
CMS COLLEGE OF SCIENCE AND COMMERCE
(AUTONOMOUS)
(Affiliated to Bharathiar University )
Accredited at the ‘A+ level by NAAC with 3.38 out of 4 (3rd Cycle)
College with Potential For Excellence Conferred by UGC, New Delhi.
Mr./Ms.______________________
…………………………….. ...………………………….
Staff In charge HOD
…………………………….. ……………………………
Internal Examiner External Examiner
MCA 2022-2024 CMS CBE
INDEX
1 Factorial
2 Color Sampler
5 Login Aunthentication
6 Age calculator
7 Hit counter
8 Students Information
10 Text Editor
3
Ex.No:1
Date: FACTORIAL
AIM:
ALGORITHM:
SOURCE CODE:
<html>
<head>
<title>Factorial</title>
</head>
<body>
<?php
if(!isset($_POST['submit']))
{
?>
<form action="3.php" method="POST">
<label>Enter an integer:</label><br>
<input type="text" name="txt1" size="5"><br>
<input type="submit" value="submit" name="submit"><br>
<?php
}
else
{
$n=$_POST['txt1'];
function factorial($a)
{
if($a==0)
{
return(1);
}
else
{
return($a*factorial($a-1));
}
}
echo "Factorial of $n is ".factorial($n);
}
?>
</body></html>
5
OUTPUT:
RESULT:
Ex.No:2
COLOR SAMPLER
Date:
AIM:
ALGORITHM:
Step 1: Start the process.
7
SOURCE CODE:
Color.html
<html>
<body>
<form method="GET" action="display .php">
R: <input type="text" name="r" size="3"/>
G: <input type="text" name="g" size="3"/>
B: <input type="text" name="b" size="3"/>
<input type="submit" name="submit" value="submit"/>
</form>
</body>
</html>
display.php
<html>
<body>
<? php
$r=$_GET['r'];
$g=$_GET ['g'];
$b=$_GET ['b'];
$rgb=$r . ',' . $g . ',' . $b ;
?>
R: <?php echo $r; ?>
G: <?php echo $g; ?>
B: <?php echo $b; ?>
<div style="width:150px; height:150px;
background-color:rgb(<?php echo $rgb; ?>)" />
</body></html>
MCA 2022-2024 CMS CBE
OUTPUT:
RESULT:
9
Ex.No:3
STRING HANDLER FUNCTIONS
Date:
AIM:
To write program to manipulate string using different types of string functions in PHP.
ALGORITHM:
SOURCE CODE:
<?php
$str=" ";
echo (boolean) empty($str);
$str=null;
echo "<br/>".(boolean) empty($str);
$str="0";
echo "<br/>".(boolean) empty($str);
unset($str);
echo "<br/>".(boolean) empty($str);
$str="welcome ";
echo "<br/>".strlen($str);
$str="y";
echo "<br/>".str_repeat($str,3);
$str="welcome to home";
echo "<br/>".substr($str,3,4);
echo "<br/>".substr($str,3,5).substr($str,-4,4);
$str="abracadabra";
echo "<br/>".substr($str,2,1);
$a="hello";
$b="hello";
$c="hEllo";
11
echo "<br/>".strcmp($a,$b);
echo "<br/>".strcmp($a,$c);
$str="[email protected]";
echo "<br/>".str_replace('@','at',$str);
$str="Yellow brigands";
echo "<br/>".UCwords($str);
echo "<br/>".UCfirst($str);
?>
MCA 2022-2024 CMS CBE
OUTPUT:
RESULT:
13
Ex.No:4
MULTI DIMENSIONAL ARRAY
Date:
Aim:
To write a program to access multi-dimensional array.
Algorithm:
Step 1: Start the process.
SOURCE CODE:
<html>
<body>
<?php
$mybook=array(
array('title'=>"The grapes of wrath",
'author'=>"John Stainbeck",
'pubyear'=>1939),
array('title'=>"The Trial",
'author'=>"Kafka",
'pubyear'=>1935),
array('title'=>"The Habit",
'author'=>"Tolkein",
'pubyear'=>1937),
array('title'=>"The Tale Of Two Cities",
'author'=>"Charles Dickens",
'pubyear'=>1939),
);
$booknum=0;
foreach($mybook as $book)
{
$booknum++;
echo "<h2> Book #$booknum</h2>";
echo "<dl>";
foreach($book as $key=>$value)
{
echo "<dt> $key </dt><dd> $value </dd>";
}
echo "</dl>";
}
?>
</body>
</html>
15
Output:
RESULT:
Ex.No:5
AIM:
To design a login authentication system using PHP.
ALGORITHM:
Step 1:Start the process.
Step 2: Create a database and a table using my My SQL commands with PHP.
Step 3: Insert an admin details into the table as a default user.
Step 4: Create an html form to accept username and password.
Step 5: For existing user, accept the username and password from the form and
Compare it with the table data to authenticate.
Step 6: If the username and password is correct, user can access a webpage,
Otherwise login failed.
Step 7: For new user, create an html form to get the user details for registration.
Step 8: Direct the user details by POST method to insert it into the table using
MySQL commands.
Step 9: Then display the user’s profile details from the table.
Step 10: Logout option is to exit from its current account and automatically Directs to
login page.
Step 11: Stop the process.
17
SOURCE CODE:
<html>
<body>
<?php
if(!isset ($_POST['submit']))
{ ?>
</form>
<?php
else
$u=$_POST['n1'];
$p=$_POST['n2'];
$conn= mysql_connect("localhost","root","admin");
if(!$conn)
die("connection failed".mysql_error());
mysql_select_db("A1",$conn);
MCA 2022-2024 CMS CBE
$count=mysql_num_rows($sql);
if($count==1)
else
?>
</body>
</html>
MYSQL
Database changed
->usernamevarchar(20),
->passwordvarchar(10));
19
Query OK, 1 row affected (0.02 sec)
+----------+----------+
| username | password |
+----------+----------+
| mahi | 12345 |
| anjali | 54321 |
| dharshu | 56789 |
+----------+----------+
mysql>
OUTPUT
MCA 2022-2024 CMS CBE
RESULT:
21
Ex.No:6
Date: AGE CALCULATOR
AIM:
To design and to write a program to calculate age inyears, months and dates from date of
birth in PHP.
ALGORITHM:
Step 1: Start the process.
Step 2: If form is not submitted display the form and get ‘dob’ in mm/dd/yy format
submit the data.
Step 3: If form is submitted process the form input.
Step 4: Split date value in to components.
Step 5: Calculate time stamp corresponding to datevalue.
Step 6: Calculate time stamp corresponding to today’sdate.
Step 7: Check that the value entered is correct.
Step 8: Calculate the age and convert it to years and months.
MCA 2022-2024 CMS CBE
SOURCE CODE:
PHP:
<html>
<head>
<title>building age calculation</title>
</head>
<body>
<?php
if(!isset($_POST['submit']))
{
?>
<form method="POST" action ="dob.php">
<h2>Enter your date of birth in mm/dd/yyyyformat:</br>
<input type="text" name="dob"/>
<p>
<input type="submit" name="submit" value="submit"/>
</form>
<?php
}
else
{
date_default_timezone_set('Asia/calcutta');
$dateArr=explode("/",$_POST['dob']);
$dateTs=strtotime($_POST['dob']);
$now=strtotime('today');
if(sizeof($dateArr)!=3)
{
die("error:please enter a valid dob");
}
if(!checkdate($dateArr[0],$dateArr[1],$dateArr[2]))
{
die("error:please enter a valid dob");
}
23
if($dateTs>=$now)
{
die("error:please enter a dob earlier than today");
}
$agedays=floor($now-$dateTs)/86400;
$ageyears=floor($agedays/365);
$agemonth=floor(($agedays-($ageyears*365))/30);
echo "You are approximately $ageyears year and $agemonth month old";
}
?>
</body>
</html>
OUTPUT:
RESULT:
AIM:
To design a web page and write a program to find number of hit in a web page.
ALGORITHM:
Step 1: Start the process.
Step 2: checking whether the counter present or not.
Step 3: If present open the file use f(open)
Step 4: Increment counter.
Step 5: update the counter number to the file.
Step 6: If not exit the program
Step 7: Stop the process.
25
SOURCE CODE:
<html>
<head>
<title>Hit counter</title>
</head>
<body>
<h1>A Hit counter</h1>
<?php
$counterFile="./count.dat";
if(!file_exists($counterFile))
{
if(!($handle=fopen($counterFile,"w")))
{
die("can not create the counterFile");
}
else
{
fwrite($handle,0);
fclose($handle);
}}
(!($handle=fopen($counterFile,"r")))
{
die("can not read the counterfile");
}
$counter=(int)fread($handle,20);
fclose($handle);
$counter++;
echo "<p>you are visitor no: $counter</p>";
if(!($handle=fopen($counterFile,"w")))
{
die("can not open the counterfile for writing");
}
fwrite($handle,$counter);
fclose($handle);
?>
MCA 2022-2024 CMS CBE
</body>
</html>
OUTPUT:
RESULT:
27
Ex.No:8 STUDENTSINFORMATION
Date:
AIM:
ALGORITHM:
Step-2: Click Start -> Program -> Accessories -> Notepad and save the file as html file.
Step 10:Use My_SQL_fetch_array() function to return the first row from the record set as an
array
Step 11:Each call toMy_SQL_fetch_array will return the next row in the record set.
SOURCE CODE:
<html>
<body>
<?php
$con=mysql_connect("localhost","root","admin");
if(!$con)
{
die('could not connect='.mysql_error());
}
if(mysql_query("CREATE DATABASE my_db",$con))
{
echo"Database created<br>";
}
else
{
echo"error creating database",mysql_error();
echo "<br>";
}
mysql_select_db("my_db",$con);
$sql="CREATE TABLE student(firstnamevarchar(15),lastnamevarchar(15),age
int(3))";
mysql_query("$sql",$con);
mysql_select_db("my_db",$con);
mysql_query("INSERT INTO student (firstname,lastname,age)
VALUES('Rekha','A','19')");
mysql_query("INSERT INTO student (firstname,lastname,age)
VALUES('Maya','c','18')");
$result=mysql_query('SELECT*FROM student');
while($row=mysql_fetch_array($result))
29
{
echo $row['firstname']." ".$row['lastname']." ".$row['age'];
echo"<br/>";
}
mysql_close($con);
?>
</body>
</html>
OUTPUT:
RESULT:
AIM:
ALGORITHM:
31
SOURCE CODE:
<html>
<body>
<?php
$con=mysql_connect("localhost","root","admin");
if(!$con)
{
die('could not connect='.mysql_error());
}
if(mysql_query("CREATE DATABASE my_db",$con))
{
echo"Database created<br>";
}
else
{
echo"error creating database",mysql_error();
echo "<br>";
}
mysql_select_db("my_db",$con);
$sql="CREATE TABLE employee(firstnamevarchar(15),lastnamevarchar(15),age
int(3))";
mysql_query("$sql",$con);
mysql_select_db("my_db",$con);
mysql_query("INSERT INTO employee (firstname,lastname,age)
VALUES('Ajay','Karan','25')");
mysql_query("INSERT INTO employee (firstname,lastname,age)
VALUES('Harshith','R','18')");
$result=mysql_query('SELECT*FROM employee');
while($row=mysql_fetch_array($result))
{
echo $row['firstname']." ".$row['lastname']." ".$row['age'];
echo"<br/>";
}
mysql_close($con);
MCA 2022-2024 CMS CBE
?>
</body>
</html>
OUTPUT:
RESULT:
33
Ex.No:10 TEXT EDITOR
Date:
AIM:
To build a text editor using PHP.
ALGORITHM:
SOURCE CODE:
PHP:
Texteditor.php
<html>
<head>
</head>
<body><?
php
define("PATH_TO_FILES","C:\wamp\www\php\sample");
if(isset($_POST["savefile"]))
{
savefile
();
elseif(isset($_GET["filename"]))
displayeditform();
elseif(isset($_POST["createfile"]))
{
createfi
le(); }
else {
displayfilelist();
functiondisplayfilelist($message="")
if(!file_exists(PATH_TO_FILES))
die("directory not found");
35
if(!($dir=dir(PATH_TO_FILES)))
die("cannot open directory");
?>
<?php
if($message)
<table cellpadding="5",cellspacing="0">
<tr>
<th>filename</th>
<th>size(byte)</th>
<th>last modified</th>
</tr>
<?php
while($filename=$dir->read())
$filepath=PATH_TO_FILES."/$filename";
".txt")
echo '<tr><td><a
href="text_edito.php?filename='.urlencode($filename).'">'.$filename.'</a></td>';
echo '<td>'.filesize($filepath).'</td>';
$dir->close();
?>
MCA 2022-2024 CMS CBE
</table>
<label for="filename">filename</label>
<br>
<br>
</form>
</body>
</html>
<?php
functiondisplayeditform($filename="")
if(!$filename)
$filename=basename($_GET["filename"]);
if(!$filename)
die("invalid
filename");
$filepath=PATH_TO_FILES."/$filename";
if(!file_exists($filepath))
die("file not found");
?>
37
<?php
echohtmlspecialchars(file_get_contents($filepath))
?>
</textarea>
</form>
<?php
functionsavefile()
$filename= basename($_POST["filename"]);
$filepath=PATH_TO_FILES."/$filename";
if(file_exists($filepath) )
if(file_put_contents($filepath,$_POST["filecontents"])=== false)
} else {
die("not found");
functioncreatefile()
$filename = basename($_POST["filename"]);
$filename=preg_replace("/[^A-Za-z0-9_\-]/","",$filename);
if(!$filename )
{
MCA 2022-2024 CMS CBE
$filename .=".txt";
$filepath=PATH_TO_FILES ."/$filename";
if(file_exists($filepath))
}
e
l
s
e
{
if(file_put_contents($filepath,"")===false)
die("coudnot create file");
chmod($filepath,0666);
displayeditform("$filename");
?>
</body>
</html>
OUTPUT:
39
RESULT: