0% found this document useful (0 votes)
36 views9 pages

Ucs 210612

Uploaded by

Grace C
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views9 pages

Ucs 210612

Uploaded by

Grace C
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

PHP Assignment-2.

1.Cookies.

2.Sessions.

3.Write a Php program to send a message to different E-Mail Id’s.

4.Write a Php program to upload an image file.

Cookies.

1.How to create a cookie:

Program:

<?php

$name="user";

$value="Kartheeswari ";

setcookie($name,$value,time()+(3600*24));

?>

<!DOCTYPE html>

<html lang="en" dir="ltr">

<head>

<title>Cookie</title>

</head>

<body>

<?php

if(!isset($_COOKIE[$name])){

echo "Cookie Named". $name."is not set!";

else{
echo "cookie ". $name." is set!<br>";

echo "Value is: ".$value;

?>

</body>

</html>

Output:

Cookie user is set!

Value is: Kartheeswari

2.How to modify a cookie value:

<?php

$name="user";

$value="UCS210612";

setcookie($name,$value,time()+(3600*24));

?>

<!DOCTYPE html>

<html lang="en" dir="ltr">

<head>

<meta charset="utf-8">

<title>Cookie</title>

</head>

<body>

<?php
if(!isset($_COOKIE[$name])){

echo "Cookie Named". $name."is not set!";

else{

echo "cookie ". $name." is set!<br>";

echo "Value is: ".$value;

?>

</body>

</html>

Output:

Cookie user is set!

Value is:UCS210612

3.How to delete a cookie:

<?php

setcookie("user", "", time() - 3600);

?>

<html>

<body>

<?php

echo "Cookie 'user' is deleted.";

?>

</body>

</html>
Output:

Cookie user is deleted.

4.How to check the cookie is enabled:

<?php

setcookie("test_cookie","test",time()+3600);

?>

<!DOCTYPE html>

<html lang="en" dir="ltr">

<head>

<meta charset="utf-8">

<title>Enable</title>

</head>

<body>

<?php

if (count($_COOKIE)>0) {

echo "Cookies are enabled.";

else {

echo "Cookies are disabled";

?>

</body>

</html>
Output:

Cookies are enabled.

2.Sessions.

1.How to start a php session:

<?php

session_start();

?>

<!DOCTYPE html>

<html lang="en" dir="ltr">

<head>

<meta charset="utf-8">

<title></title>

</head>

<body>

<?php

$_SESSION["favcolor"]="Blue";

$_SESSION["favanimal"]="Lion";

echo "Sessions variables are set..";

?>

Output:

Sessions variables are set..

2.Get Session Variables:


<?php

echo "Favourite color is: ".$_SESSION["favcolor"]."<br>";

echo "Favourite Animal is: ".$_SESSION["favanimal"]."<br>";

?>

</body>

</html>

Output:

Favourite color is: Blue.

Favourite Animal is:Lion

3.How to display all session variables:

<?php

print_r($_SESSION);

?>

4.Modify Session Values:

<?php

$_SESSION["favcolor"]="Red";

echo "Modified session variable color:".$_SESSION["favcolor"]."<br>";

?>

<?php

print_r($_SESSION);

?>
Output:

Modified session variable color: Red.

5.How to delete a session variable:

<?php

session_unset();

session_destroy();

?>

3.Php program to upload an image file.

Img.html

<!DOCTYPE html>

<html lang="en" dir="ltr">

<head>

<meta charset="utf-8">

<title>Image</title>

</head>

<body>

<form action="upload.php" method="post" enctype="multipart/form-data">Select image to


upload:<input type="file" name="filetoupload" id="filetoupload">

<input type="submit" name="submit" value="Upload Image">

</form>

</body>
</html>

Upload.php

<?php

$target_dir="uploads/";

$target_file="$target_dir";

basename($_FILES["filetoupload"]["name"]);

$uploadok=1;

$imagefiletype=strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

if(isset($_POST["submit"])){

$check=getimagesize($_FILES["filetoupload"]["tmp_name"]);

if ($check!==false) {

echo "File is an image -".$check["mime"].".";

else {

echo "File is not an image.";

$uploadok=0;

if ($uploadok==0) {

echo "Sorry, your file was not Uploaded.";

else

if(move_uploaded_file($_FILES["filetoupload"]["tmp_name"],$target_file))
{

echo "The file ".htmlspecialchars(basename($_FILES["filetoupload"]["name"]))."has been uploaded.";

else {

echo "Sorry, there was an error uploading your file.";

?>

Output:

Select image to upload: Choose File No file chosen Upload Image

File is an image –image/jpeg. The file IMG-20210405-WA0004.jpg has been uploaded.

4. How to send a message to different E-Mail Id’s.

<?php

$to=$contact;

$subject=’the subject’;

$message=’hello’;

mail($to,$subject,$message,$headers);

?>

You might also like