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

PHP RecordNew

Uploaded by

aadhis9633
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)
12 views

PHP RecordNew

Uploaded by

aadhis9633
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/ 40

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.

Chinnavedampatti, Coimbatore - 641 049

DEPARTMENT OF COMPUTER APPLICATIONS

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.

Chinnavedampatti, Coimbatore - 641 049

REGISTER NO: ________________

Certified that the Bonafide record of work done by

Mr./Ms.______________________

of ___________________ during the year 2022-2024

…………………………….. ...………………………….
Staff In charge HOD

Submitted for the Practical Examination held on __________________at


CMS College of Science & Commerce, Coimbatore-641 049.

…………………………….. ……………………………
Internal Examiner External Examiner
MCA 2022-2024 CMS CBE

INDEX

S. No DATE NAME OF THE PROGRAM PAGE. SIGNATURE


NO

1 Factorial

2 Color Sampler

3 String Handler Functions

4 Multi dimentional array

5 Login Aunthentication

6 Age calculator

7 Hit counter

8 Students Information

9 Connect Employee Details with


Mysql

10 Text Editor

3
Ex.No:1

Date: FACTORIAL

AIM:

To write a program to print the factorial calculator of a number.

ALGORITHM:

Step 1: Start the process.

Step 2: Read the number from the user.

Step 3: Post the values to php file.

Step 4: Check the number, if it is less than zero.

Step 5: Or repeat until the entered number meets.

Step 6: Print the value.

Step 7: Stop the process.


MCA 2022-2024 CMS CBE

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:

The program is executedsuccessfully and the output is verified.


MCA 2022-2024 CMS CBE

Ex.No:2
COLOR SAMPLER
Date:

AIM:

To write a program for interactive html color sampler.

ALGORITHM:
Step 1: Start the process.

Step 2: Get the values for R, G, and B

Step 3: Post the values to php file.

Step 4: Combined 3 values to get the particular color

Step 5: Stop 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:

The program is executed successfully and the output is verified

9
Ex.No:3
STRING HANDLER FUNCTIONS
Date:

AIM:

To write program to manipulate string using different types of string functions in PHP.

ALGORITHM:

Step 1: Start the process.

Step 2: Declare an empty string .

Step 3: Declare a string for reversing the string.

Step 4: Declare a string for compare the two strings

Step 5: Declare a string for trim a string

Step 6: Declare a string for replace the words

Step 7: Stop the process


MCA 2022-2024 CMS CBE

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="one small step";


echo "<br/>".strrev($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=" the names bond,james bond";


echo "<br/>".str_word_count($str);

$str="[email protected]";
echo "<br/>".str_replace('@','at',$str);

$str=" abc ";


echo "<br/>".trim($str);

$str="Yabba Dubba Doo";


echo "<br/>".strtolower($str);
echo "<br/>".strtoupper($str);

$str="Yellow brigands";
echo "<br/>".UCwords($str);
echo "<br/>".UCfirst($str);

?>
MCA 2022-2024 CMS CBE

OUTPUT:

RESULT:

The program is executed successfully and the output is verified.

13
Ex.No:4
MULTI DIMENSIONAL ARRAY
Date:

Aim:
To write a program to access multi-dimensional array.

Algorithm:
Step 1: Start the process.

Step 2: Create nested arrays with appropriate field

Step 3: Display the contents in the array.

Step 4: Stop the process.


MCA 2022-2024 CMS CBE

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:

The program is executed successfully and the output is verified.


MCA 2022-2024 CMS CBE

Ex.No:5

Date: LOGIN AUTHENTICATION

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>

<h2>login authentication </h2>

<?php

if(!isset ($_POST['submit']))

{ ?>

<form method="post" action="Multi.php">

username<input type="text" name="n1"/><br>

password<input type="password" name="n2"/><br>

<input type="submit" name="submit" /><br>

<input type="reset" name="reset" /><br>

</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

$sql=mysql_query("select * from Login where username ='$u' and password='$p' ",$conn);

$count=mysql_num_rows($sql);

if($count==1)

echo"<h1>welcome to the webpage $u</h1>";

echo"<br><h2>your login is successful<h2>";

else

echo"<br><h2>your login is unsuccessful<h2>";

?>

</body>

</html>

MYSQL

mysql> create database A1;

Query OK, 1 row affected (0.00 sec)

mysql> use A1;

Database changed

mysql> create table Login(

->usernamevarchar(20),

->passwordvarchar(10));

Query OK, 0 rows affected (0.05 sec)

mysql> insert into Login values("mahi","12345");

Query OK, 1 row affected (0.03 sec)

mysql> insert into Login values("anjali","54321");

19
Query OK, 1 row affected (0.02 sec)

mysql> insert into Login values("dharshu","56789");

Query OK, 1 row affected (0.02 sec)

mysql> select * from Login;

+----------+----------+

| username | password |

+----------+----------+

| mahi | 12345 |

| anjali | 54321 |

| dharshu | 56789 |

+----------+----------+

3 rows in set (0.00 sec)

mysql>

OUTPUT
MCA 2022-2024 CMS CBE

RESULT:

The program is executed successfully and the output is verified.

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:

The program is executed successfully and the output is verified.


MCA 2022-2024 CMS CBE

Ex.No:7 HIT COUNTER


Date:

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:

The program is executed successfully and the output is verified.

27
Ex.No:8 STUDENTSINFORMATION
Date:

AIM:

To connectstudents information with MYSQL using PHP program.

ALGORITHM:

Step 1: Start a program.

Step-2: Click Start -> Program -> Accessories -> Notepad and save the file as html file.

Step 3:Create a connection string by giving user name and password.

Step 4 :Display error message if not able to connect.

Step 5 :Create a database, if could not create display error message.

Step 6 :Create a table student with fields firstname, lastname, age.

Step 7 :Execute a query by giving tablename by connection string.

Step 8:Insert datas into the table student.

Step 9:Select datas from table and store in result variable

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.

Step 12:Display the value of each row.

Step 13:Close the table.

Step 14:Stop the program.


MCA 2022-2024 CMS CBE

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:

The program is executed successfully and the output is verified.


MCA 2022-2024 CMS CBE

Ex.No:9 CONNECT EMPLOYEE DETAILS


Date: WITH MYSQL

AIM:

To Connect employee details with MYSQL using PHP program.

ALGORITHM:

Step 1: Start a program.


Step-2: Click Start -> Program -> Accessories -> Notepad and save the file as html file.
Step 3:Create a connection string by giving user name and password.
Step 4 :Display error message if not able to connect.
Step 5 :Create a database, if could not create display error message.
Step 6 :Create a table employee with fields firstname, lastname, age.
Step 7 :Execute a query by giving tablename by connection string.
Step 8:Insert datas into the tableemployee.
Step 9:Select datas from table and store in result variable
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.
Step 12:Display the value of each row.
Step 13:Close the table.
Step 14:Stop the program.

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:

The program is executed successfully and the output is verified

33
Ex.No:10 TEXT EDITOR
Date:

AIM:
To build a text editor using PHP.

ALGORITHM:

Step 1: Start the process.


Step 2: create PHP page and define four functions to create, edit, save and Display files with
its detail.
Step 3: Invoke the functions based on the user’s choice.
Step 4: By default, display function is invoked to display the files exist in a Specified path.
Step 5: In display function, define the path of the file location and check the files are
already exist in a directory.
Step 6: If the files exist, display file name, size and last modified.
Step 7: In create function, get the filename from the user and assign it to the Variable, then
check it is already exist or not in specified path.
Step 8: If not exist, create and open it edit mode. Otherwise “file exists” message is
displayed to the user.
Step 9: In edit function, if the file is already created and exist in a specified
path. Open it to write or modify the contents.
Step 10: Then call the save function to save the file.
Step 11: Again display function is called to update and display the file list to the user.
Step 12: Stop the process.
MCA 2022-2024 CMS CBE

SOURCE CODE:
PHP:

Texteditor.php

<html>

<head>

<title>Building Text Editor</title>

</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)

echo'<p class="error">'.$message.'<p>' ?>

<h2>CHOOSE A FILE TO EDIT</h2>

<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";

if($filename !="." && $filename != ".." && !is_dir($filepath) &&strrchr($filename,".") ==

".txt")

echo '<tr><td><a

href="text_edito.php?filename='.urlencode($filename).'">'.$filename.'</a></td>';
echo '<td>'.filesize($filepath).'</td>';

echo '<td>'.date("m j, Y H:i:s",filemtime($filepath)).'</td></tr>';

$dir->close();

?>
MCA 2022-2024 CMS CBE

</table>

<h2>..............or create a new file: </h2>

<form action="text_edito.php" method="post">

<label for="filename">filename</label>

<input type="text" name="filename" value=""/>.txt

<br>

<br>

<input type="submit" name="createfile" value="create file"/>

</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");

?>

<h2>editing <?php echo $filename ?></h2>

<form action="text_edito.php" method="post" >

<input type="hidden" name="filename" value="<?php echo $filename ?>"/>

<textarea name="filecontents" rows="20" cols="80" >

37
<?php

echohtmlspecialchars(file_get_contents($filepath))

?>

</textarea>

<input type="submit" name="savefile" value="save file"/>

<input type="submit" name="cancel" value="cancel"/>

</form>

<?php

functionsavefile()

$filename= basename($_POST["filename"]);
$filepath=PATH_TO_FILES."/$filename";

if(file_exists($filepath) )

if(file_put_contents($filepath,$_POST["filecontents"])=== false)

die("could not save file");


displayfilelist();

} else {
die("not found");

functioncreatefile()

$filename = basename($_POST["filename"]);
$filename=preg_replace("/[^A-Za-z0-9_\-]/","",$filename);
if(!$filename )

{
MCA 2022-2024 CMS CBE

displayfilelist("invalid filename-please try again");


return;

$filename .=".txt";

$filepath=PATH_TO_FILES ."/$filename";
if(file_exists($filepath))

displayfilelist("the file $filename already exists!");

}
e
l
s
e
{

if(file_put_contents($filepath,"")===false)
die("coudnot create file");
chmod($filepath,0666);

displayeditform("$filename");

?>

</body>

</html>

OUTPUT:

39
RESULT:

The program is executed successfully and the output is verified.

You might also like