Professional Documents
Culture Documents
<?php
if( $_GET["name"] || $_GET["age"] )
{ echo "Welcome ". $_GET['name']. "<br />";
echo "You are ". $_GET['age']. " years old.";
exit(); } ?>
<html> <body>
<form action="<?php $_PHP_SELF ?>"
method="GET"> Name: <input type="text"
name="name" /> Age: <input type="text" name="age"
/> <input type="submit" /> </form> </body> </html>
9/6/2014
9/6/2014
<html> <body>
<?php include("xxmenu.php"); ?>
<p>This is an example to show how to include
wrong PHP file!</p> </body> </html>
<html> <body>
<?php require("xxmenu.php"); ?>
<p>This is an example to show how to include
wrong PHP file!</p> </body> </html>
9/6/2014
Reading a file
Reading a file
Sample
Sample
Writing a file
9/6/2014
Sample
<?php $filename =
"/home/user/guest/newfile.txt";
$file = fopen( $filename, "w" );
if( $file == false )
{ echo ( "Error in opening new file" );
exit(); }
fwrite( $file, "This is a simple test\n" );
fclose( $file ); ?>
Sample
<html> <head> <title>Writing a file using
PHP</title> </head>
<body>
<?php if( file_exist( $filename ) )
{ $filesize = filesize( $filename );
$msg = "File created with name $filename ";
$msg .= "containing $filesize bytes";
echo ($msg ); }
else { echo ("File $filename does not exit" ); } ?>
</body> </html>
PHP Cookies
9/6/2014
IsSet() Function
You can use isset() function to check if a cookie
is set or not.
<html> <head> <title>Accessing Cookies with
PHP</title> </head> <body>
<?php if( isset($_COOKIE["name"]))
echo "Welcome " . $_COOKIE["name"] . "<br
/>";
else echo "Sorry... Not recognized" . "<br />";
?> </body> </html>
Sample
<?php
setcookie("name", "John Watkin", time()+3600,
"/","", 0);
setcookie("age", "36", time()+3600, "/", "", 0);
?>
<html> <head> <title>Setting Cookies with
PHP</title> </head> <body>
<?php echo "Set Cookies"?> </body> </html>
Sample
<html> <head> <title>Accessing Cookies with
PHP</title> </head> <body>
<?php echo $_COOKIE["name"]. "<br />";
/* is equivalent to */
echo $HTTP_COOKIE_VARS["name"]. "<br />";
echo $_COOKIE["age"] . "<br />";
/* is equivalent to */
echo $HTTP_COOKIE_VARS["name"] . "<br />";
?> </body> </html>
9/6/2014
PHP Sessions
PHP Sessions
Sample
<html>
<head>
<title>Setting up a PHP session</title>
</head>
<body>
<?php echo ( $msg ); ?>
</body>
</html>
Sample
<?php session_start();
if( isset( $_SESSION['counter'] ) )
{ $_SESSION['counter'] += 1; }
else { $_SESSION['counter'] = 1; }
$msg = "You have visited this page ".
$_SESSION['counter'];
$msg .= "in this session."; ?>
9/6/2014
Sample
<?php unset($_SESSION['counter']); ?>
<?php session_destroy(); ?>
<?php session_start();
if (isset($_SESSION['counter']))
{ $_SESSION['counter'] = 1; }
else { $_SESSION['counter']++; }
?>
$msg = "You have visited this page ".
$_SESSION['counter']; $
msg .= "in this session.";
echo ( $msg );
<p> To continue click following link <br /> <a
href="nextpage.php?<?php echo htmlspecialchars(SID);
>"> </p>