Php practical programs, Study notes for Computer Science
Ramya_28
Ramya_28

Php practical programs, Study notes for Computer Science

28 pages
10Number of visits
Description
lab Practical programs for bachelor of computer applications
20 points
Download points needed to download
this document
Download the document
Preview3 pages / 28
This is only a preview
3 shown on 28 pages
This is only a preview
3 shown on 28 pages
This is only a preview
3 shown on 28 pages
This is only a preview
3 shown on 28 pages

PHP PRACTICAL PROGRAMS

1.Develop and demonstrate an HTML file that includes PHP script to generate first ‘n’ Fibonacci numbers

<html>

<body>

<h1>Fibonacci seris</h1>

<form action="" method="post">

Enter the limit:

<input type="text" name="n"><br>

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

<?php

$a=0;

$b=1;

$c=0;

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

{

$x=$_POST['n'];

for($i=0;$i<$x;$i++)

{

$a=$b;

$b=$c;

echo "$c<br>";

$c=$a+$b;

}

}

?>

</form>

</body>

</html>

2. Create a PHP script that accepts a number through inputbox and checks whether it is palindrome or not

<html>

<body>

<h1>Palindrom</h1>

<form action="" method="post">

Enter the number:

<input type="text" name="n"><br>

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

<?php

if(isset($_POST['sub']))

{

$x=$_POST['n'];

$r=0;

$z=$x;

while($x!=0)

{

$t=$x%10;

$r=$r*10+$t;

$x=intval($x/10);

}

if($z==$r)

{

echo "$z is palindrom";

}

else

{

echo "$z is not palindrom";

}

}

?>

</form>

</body>

</html>

3. Create an HTML page that has one input box, which can take multiline text and a submit button. Once the user clicks the submit button, it should display the number of vowels, characters, lines and words using an alert message. Words should be separated with white space and lines should be separated with the new line character

<html>

<body>

<form action="" method="post">

Enter the text:

<textarea name="str" rows="10" cols="50"></textarea><br>

<input type="submit" name="sub">

</form>

<?php

if(isset($_POST['sub']))

{

$string=strtolower($_POST['str']);

$vowels=array('a','e','i','o','u');

$len=strlen($string);

$num=0;

for($i=0;$i<$len;$i++)

{

if(in_array($string[$i],$vowels))

{

$num++;

}

}

echo "Number of vowels: $num<br>";

$len=$len-1;

echo "Number of characters: $len<br>";

$word=str_word_count($string);

echo "Number of words: $word<br>";

$line=substr_count($_POST['str'],"\n");

echo "Number of lines: $line";

}

?>

</body>

</html>

4. Develop a simple calculator web application that takes 2 numbers and an operator (+,-,*, /,%) from an HTML page and returns the result.

<html>

<body>

<h1>simple calculator</h1>

<form action="" method="post">

Enter the number:

<input type="text" name="num1"><br><br>

Enter another number:

<input type="text" name="num2"><br><br>

<select name="fun"><br>

<option value="+">Addition</option>

<option value="-">Subtration</option>

<option value="*">Multiplication</option>

<option value="/">Division</option>

<option value="%">Modulus</option>

</select>

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

</form>

<?php

if(isset($_POST['sub']))

{

$num1=$_POST['num1'];

$num2=$_POST['num2'];

$fun=$_POST['fun'];

if(is_numeric($num1) && is_numeric($num2))

{

if($fun!=null)

{

switch($fun)

{

case "+":

$result=$num1+$num2;

break;

case "-":

$result=$num1-$num2;

break;

case "*":

$result=$num1*$num2;

break;

case "/":

$result=$num1/$num2;

break;

case "%":

$result=$num1%$num2;

break;

}

echo "result: $result";

}

}

}

?>

</body>

</html>

5. Create a web application that takes a name as the input and on submit, it shows a hello <name> on the page. It should show a start time and a logout button at the right top corner of the page. On clicking this button it should show a logout page with a message and duration of usage

<html>

<body>

<form method="post">

Username:

<input type="text" name="text1"><br>

<input type="submit" value="SignIn" name="sub">

</form>

<?php

session_start();

date_default_timezone_set("Asia/Calcutta");

if(isset($_POST['sub']))

{

$_SESSION['luser']=$_POST['text1'];

$_SESSION['start']=time();

$tm=$_SESSION['start'];

echo "<p align='right'>session started at".date("h:i:sa",$tm)."<br>";

echo "<form action='logout.php' method='post'>";

echo "<input type='submit' value='Logout'></p>";

echo "</form>";

echo "Hello".$_SESSION['luser'];

}

?>

</body>

</html>

<html>

<body>

<?php

session_start();

date_default_timezone_set("Asia/Calcutta");

echo "<p align='right'>session started at".date("h:i:sa",time())."</p><br>";

echo "Thank you ".$_SESSION['luser']."<br>";

$session_time=time()-$_SESSION['start'];

echo "Session duration ".$session_time." seconds";

?>

</body>

</html>

6. Develop a web application that takes the name and age from an HTML page. If the age is less than 18, it should display a page with “hello <name>, you are not authorized to visit this site” otherwise it should display the message, “<name>, welcome to this site”.

<html>

<body>

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

Name:

<input type="text" name="name"><br><br>

Age:

<input type="text" name="age"><br><br>

<input type="submit" value="signin" name="sub">

</form>

</body>

<html>

<html>

<body>

<?php

if(isset($_POST['sub']))

{

$name=$_POST['name'];

$age=$_POST['age'];

if($age<18)

{

echo "Hello ".$name.". You are not authorized to visit this site";

}

else

{

echo "welcome ".$name." to this site";

}

}

?>

</body>

</html>

7. Create a web page that keeps track of how many times a visitor has loaded a page.

<html>

<body>

<?php

session_start();

if(isset($_SESSION['counter']))

{

$_SESSION['counter']+=1;

}

else

{

$_SESSION['counter']=1;

}

echo "You have visited the page ".$_SESSION['counter']." time in this

session";

?>

</body>

</html>

8. Develop a database based web application that performs login validation and prints the corresponding message based on checking the username and password from MySQL database.

<html>

<body>

<h1>User Login</h1>

<form method="post">

Username :

<input type="text" name="uname"><br><br>

Password :

<input type="password" name="pass"><br><br>

<input type="submit" value="login" name="sub">

</form>

<?php

if(isset($_POST['sub']))

{

$conn=new mysqli('localhost','root','','info');

if($conn->connect_errno)

{

echo "Failed to connect with the server";

exit();

}

else

{

$uname=$_POST['uname'];

$pass=$_POST['pass'];

$q="select Message from users where Username='$uname' and Password='$pass'";

$result=$conn->query($q);

if($result->num_rows>0)

{

$row=$result->fetch_assoc();

echo "Message ".$row['Message'];

}

else

{

echo "Either username or password is wrong";

}

}

}

?>

</body>

</html>

9.Develop a web application that lists all cookies stored in the browser and on clicking “list cookies” button, add cookies if necessary

<html>

<body>

<?php

$firstname="aaa";

$middlename="bbb";

$lastname="ccc";

setcookie("first_name",$firstname,time()+86400);

setcookie("middle_name",$middlename,time()+86400);

setcookie("last_name",$lastname,time()+86400);

echo "cookie set";

?>

</body>

</html>

<html>

<body>

<?php

if(!empty($_COOKIE))

{

foreach($_COOKIE as $key=>$val)

{

echo $key." is ".$val."<br>";

}

}

else

{

echo "Cookie is not set. You have to set cookie";

}

?>

</body>

</html>

10.Create a PHP script that reads an array and displays the array in sorted order.

<html>

<body>

<h1>Sorting an array</h1>

<form action="" method="post">

Enter the string:

<input type="text" name="str">

<input type="submit" name="sub">

</form>

<?php

if(isset($_POST['sub']))

{

$string=$_POST['str'];

$x=strlen($string);

$count=substr_count($string," ");

$array=explode(" ",$string);

for($i=0;$i<$count;$i++)

{

for($j=$i+1;$j<=$count;$j++)

{

if($array[$i]>$array[$j])

{

$temp=$array[$i];

$array[$i]=$array[$j];

$array[$j]=$temp;

}

}

}

print_r($array);

}

?>

</body>

</html>

11. Develop a web application that develops a student registration form and displays the entered details to another page

<html>

<body>

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

<table>

<caption>Student Registration Form</caption>

<tr>

<th>Name</th>

<td><input type="text" name="a"></td>

</tr>

<tr>

<th>Age</th>

<td><input type="text" name="b"></td>

</tr>

<tr>

<th>Gender</th>

<td><input type="radio" name="gender" value="male" checked>male

<input type="radio" name="gender" value="female">female

<input type="radio" name="gender" value="other">Other</td>

</tr>

<tr>

<th>date of birth</th>

<td><input type="text" name="c"></td>

</tr>

<tr>

<th>Course</th>

<td><input type="text" name="d"></td>

</tr>

<tr>

<th>Phone no</th>

<td><input type="text" name="e"></td>

</tr>

<tr>

<td><input type="submit" name="sub"></td>

</tr>

</table>

</form>

</body>

</html>

<html>

<body>

<form>

<table style="width:100%" border="1">

<caption> Student Details</caption>

<tr>

<th>Name</th>

<td><?php echo $_POST['a']?></td>

</tr>

<tr>

<th>Age</th>

<td><?php echo $_POST['b']?></td>

</tr>

<tr>

<th>Gender</th>

<td><?php echo $_POST['gender']?></td>

</tr>

<tr>

<th>Date of Birth</th>

<td><?php echo $_POST['c']?></td>

</tr>

<tr>

<th>course</th>

<td><?php echo $_POST['d']?></td>

</tr>

<tr>

<th>Phone no</th>

<td><?php echo $_POST['e']?></td>

</tr>

</table>

</form>

</body>

</html>

12.Create a MySQL table containing a few details of employees. Create a PHP web application that can save the employee data given through HTML forms and can fetch and display the table contents into an HTML table.

<html>

<body>

<form method="post">

<table>

<caption>Employee details</caption>

<tr>

<th>Employee department</th>

<td><input type="text" name="dept">

</tr>

<tr>

<th>Employee Name</th>

<td><input type="text" name="name">

</tr>

<tr>

<th>Employee id</th>

<td><input type="text" name="id">

</tr>

<tr>

<th>Employee Salary</th>

<td><input type="text" name="sal">

</tr>

<tr>

<td align="center" colspan="2"><input type="submit" name="sub"></td>

</tr>

</table>

</form>

<?php

$conn=new mysqli('localhost','root','','empdb');

if($conn->connect_errno)

{

echo "Failed to connect with the server";

exit();

}

if(isset($_POST['sub']))

{

$edept=$_POST['dept'];

$ename=$_POST['name'];

$eid=$_POST['id'];

$esal=$_POST['sal'];

$q="insert into empdet(dept,name,id,sal)values

('$edept','$ename','$eid','$esal')";

if($conn->query($q)===TRUE)

{

echo "<script>window.alert('Details saved');

location.href='display.php';</script>";

}

else

die('error');

}

?>

</body>

</html>

<html>

<body>

<table style="border=1" align="center">

<caption align="center">Employee Details</caption>

<tr>

<th>Employee Department</th>

<th>Employee Name</th>

<th>Employee id</th>

<th>Employee salary</th>

</tr>

<?php

$conn=new mysqli('localhost','root','','empdb');

if($conn->connect_errno)

{

echo "Failed to connect with the server";

exit();

}

$query="select * from empdet";

if($result=$conn->query($query))

{

while($row=$result->fetch_assoc())

{

echo "<tr>";

echo "<td>".$row['dept']."</td>";

echo "<td>".$row['name']."</td>";

echo "<td>".$row['id']."</td>";

echo "<td>".$row['sal']."</td>";

echo "</tr>";

}

}

$conn->close();

?>

</table>

</body>

</html>

13. Create a database application in PHP that can delete a student record on accepting the enrolment number of the student. A message should be displayed on deletion of the record and the contents of the table should be displayed

<html>

<body>

<form method="post">

<table>

<caption>Strudent registration form</caption>

<tr>

<th>Name</th>

<td><input type="text" name="name"></td>

</tr>

<tr>

<th>Enrolment number</th>

<td><input type="text" name="enrlno"></td>

</tr>

<tr>

<th>course</th>

<td><input type="text" name="crs"></td>

</tr>

<tr>

<th>Phone no</th>

<td><input type="text" name="phno"></td>

</tr>

<tr>

<td align="center" colspan="2"><input type="submit" name="sub"></td>

</tr>

</table>

</form>

<?php

$conn=new mysqli('localhost','root','','stdb');

if($conn->connect_errno)

{

echo "Failed to connect with the server";

exit();

}

if(isset($_POST['sub']))

{

$sname=$_POST['name'];

$senrlno=$_POST['enrlno'];

$scrs=$_POST['crs'];

$sphno=$_POST['phno'];

$q="insert into studet(name,enrlno,crs,phno) values

('$sname','$senrlno','$scrs','$sphno')";

if($conn->query($q)===TRUE)

{

echo "<script> window.alert('Details saved');

location.href='project.php';</script>";

}

else

die('Error');

}

?>

</body>

</html>

<html>

<body>

<table align="center">

<caption>Student Details</caption>

<tr>

<td>Name</td>

<td>Enrolment number</td>

<td>Course</td>

<td>Phone number</td>

</tr>

<?php

$conn=new mysqli('localhost','root','','stdb');

if($conn->connect_errno)

{

echo "Failed to connect with the server";

exit();

}

$query="select * from studet";

if($result=$conn->query($query))

{

while($row=$result->fetch_assoc())

{

echo "<tr>";

echo "<td>".$row['name']."</td>";

echo "<td>".$row['enrlno']."</td>";

echo "<td>".$row['crs']."</td>";

echo "<td>".$row['phno']."</td>";

echo "</tr>";

}

}

?>

</table>

</body>

</html>

<html>

<body>

<form method="post">

<table>

<caption>To delete student data</caption>

<tr>

<th>Enter the enrolment number</th>

<td><input type="text" name="enrlno"></td>

</tr>

<tr>

<td align="center" colspan="2"><input type="submit" name="sub"></td>

</tr>

</table>

</form>

<?php

$conn=new mysqli('localhost','root','','stdb');

if($conn->connect_errno)

{

echo "Failed to connect with the server";

exit();

}

if(isset($_POST['sub']))

{

$senrlno=$_POST['enrlno'];

$q="delete from studet where enrlno='$senrlno'";

if($conn->query($q)===TRUE)

{

echo "<script> window.alert('Changes saved');

location.href='project.php';</script>";

}

else

die('Error');

}

?>

</body>

</html>

14.Create an HTML file to create a simple form with 5 input fields viz. Name, Password, Email, Pin code, Phone No. and a Submit button. Write a PHP script to demonstrate required field validations to validate all the input fields and display error messages if validations do not hold.

<html>

<body>

<?php

$nameerr=$passworderr=$emailerr=$pincodeerr=$phonenoerr="";

$name=$psw=$email=$pin=$phno="";

if($_SERVER["REQUEST_METHOD"]=="POST")

no comments were posted
This is only a preview
3 shown on 28 pages