classical encryption techniques - welcome to computer...

92
Chapter 7:- PHP Compiled By:- Sanjay Patel Assistant Professor, SVBIT.

Upload: hoangcong

Post on 12-Mar-2018

222 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Chapter 7:-

PHP

Compiled By:- Sanjay Patel

Assistant Professor,

SVBIT.

Page 2: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Outline

Starting to script on server side,

Arrays,

Function and forms,

Advance PHP

Databases:-Basic command with PHP examples,

Connection to server, creating database, selecting a

database, listing database, listing table names

creating a table, inserting data, altering tables,

queries, deleting database, deleting data and tables,

PHP my admin and database bugs.Sanjay Patel

Page 3: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

PHP stands for PHP: Hypertext Preprocessor

PHP is a server-side scripting language, like ASP

PHP scripts are executed on the server

PHP supports many databases (MySQL, Informix,

Oracle, Sybase, Solid, PostgreSQL, Generic ODBC,

etc.)

PHP is an open source software (OSS)

PHP is free to download and use

What is PHP?

Sanjay Patel

Page 4: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Getting Start

Sanjay Patel

How to escape from HTML and enter PHP mode

PHP parses a file by looking for one of the special tags that tells it to start interpreting the text as PHP code. The parser then executes all of the code it finds until it runs into a PHP closing tag.

Starting tag Ending

tag

Notes

<?php ?> Preferred method as it allows the use of

PHP with XHTML

<? ?> Not recommended. Easier to type, but

has to be enabled and may conflict with

XML

<script language="php"> ?> Always available, best if used when

FrontPage is the HTML editor

<% %> Not recommended. ASP tags support

was added in 3.0.4

<?php echo “Hello World”; ?>

Page 5: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

PHP files may contain text, HTML tags and scripts

PHP files are returned to the browser as plain HTML

(View source would not work here!)

PHP files have a file extension of ".php", ".php3", or

".phtml"

What is a PHP File?

Sanjay Patel

Page 6: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

PHP runs on different platforms (Windows, Linux,

Unix, etc.)

PHP is compatible with almost all servers used

today

PHP is FREE to download from the official PHP

resource: www.php.net

PHP is easy to learn and runs efficiently on the

server side

Why PHP?

Sanjay Patel

Page 7: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Why is PHP used?

Sanjay Patel

Easy to Use:- Code is embedded into HTML. The

PHP code is enclosed in special start and end tags

that allow you to jump into and out of "PHP

mode".

Cross Platform:- Runs on almost any Web server

on several operating systems. One of the strongest

features is the wide range of supported

databases.

Cost Benefits:- PHP is FREE to download

Page 8: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

PHP can generate dynamic page content

PHP can create, open, read, write, and close files on

the server

PHP can collect form data

PHP can send and receive cookies

PHP can add, delete, modify data in your database

What Can PHP Do?

Sanjay Patel

Page 9: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Hello world example

Sanjay Patel

<html>

<body>

<?php

echo "Hello World";

?>

</body>

</html>

PHP code can be placed anywhere in the

document.

Starts and ends with <?php … ?>

Each code line in PHP must end with a semicolon

Page 10: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Simple HTML Page with PHP

Sanjay Patel

The following is a basic example to output text using PHP.

<html><head>

<title>My First PHP Page</title>

</head>

<body>

<?php

echo "Hello World!";

?>

</body></html>

Copy the code onto your web server and save it as “test.php”.

You should see “Hello World!” displayed.

Page 11: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

My First Program

Sanjay Patel

Page 12: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Comments in PHP

Sanjay Patel

Page 13: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

All variables in PHP start with a $ sign symbol.

Variables may contain strings, numbers, or arrays.

To concatenate two or more variables together, use

the dot (.) operator.

Variables are used for storing values, like text

strings, numbers or arrays.

All variables in PHP are denoted with a leading

dollar sign($)

The value of a variable is the value of its most

recent assignment

Variables in PHP

Sanjay Patel

Page 14: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Syntax

$name_of_variable=value;

Variables are assigned with the = operator, with

the variable on the left-hand side and the

expression to be evaluated on the right.

Variable Syntax

Sanjay Patel

Page 15: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

A variable name must start with a letter or an

underscore "_“.

A variable name can only contain alpha-numeric

characters and underscores (a-z, A-Z, 0-9, and _ ).

A variable name should not contain spaces. If a

variable name is more than one word, it should be

separated with an underscore ($my_string), or with

capitalization ($myString).

Rules for declaration Variable

Sanjay Patel

Page 16: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Variable Example

Sanjay Patel

Page 17: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Another Example

Sanjay Patel

<html>

<body>

<?php

$x=5;

$y=6;

$z=$x+$y;

echo $z;

?>

</body>

</html>

• The result would be:

“11”

Page 18: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Local Scope Variable Example

Sanjay Patel

<?php

$x=5; // global scope

function myTest()

{

echo $x; // local scope

}

myTest();

?>

• The result would be:

“Blank”

Page 19: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Global ScopeVariable Example

Sanjay Patel

<html>

<body>

<?php

$x=5; // global scope

$y=10; // global scope

function myTest()

{

global $x, $y;

$y=$x+$y;}

myTest(); // run function

echo $y; // output the new value

for variable $y

?>

</body>

</html>

• The result would be:

“15”

Page 20: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

PHP’s type is simple, streamlined and flexible, and it

insulates the programmer from low-level details.

PHP makes it easy not to worry too much about

typing of variables and values, both because does

not require variables to be typed and because it

handles a lot of type conversion for you.

Data types in PHP

Sanjay Patel

Page 21: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Integers

Ex, 1,3,5,5,44,3,3334

Doubles

Ex, 3.14,5.33.4.55

Booleans

Ex. TRUE and FALSE

String

Ex, “hello”

Array

Objects

Data Types Example

Sanjay Patel

Page 22: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Example

Sanjay Patel

Page 23: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

A string variable is used to store and manipulate

text.

String variables are used for values that contain

characters.

After we have created a string variable we can

manipulate it.

A string can be used directly in a function or it can

be stored in a variable.

String Variables in PHP

Sanjay Patel

Page 24: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Example

Sanjay Patel

<?php

$txt="Hello world!";

echo $txt;

?>

• The result would be:

“Hello world!";”

<?php

$txt1="Hello world!";

$txt2="What a nice day!";

echo $txt1 . " " . $txt2;

?>

• The result would be:

“Hello world! What a nice

day! “

<?php

echo strpos("Hello world!","world");

?>

• The result would be:

“6“

Page 25: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

PHP operator

Sanjay Patel

Page 26: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Cont’d..

Sanjay Patel

Page 27: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Cont’d..

Sanjay Patel

Page 28: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

-An array can store one or more values in a single

variable name.

-Each element in the array is assigned its own ID so

that it can be easily accessed.

$array[key] = value;

Array

Sanjay Patel

Page 29: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

When working with PHP, sooner or later, you might

want to create many similar variables.

Instead of having many similar variables, you can store

the data as elements in an array.

There are three different kind of arrays:

Numeric array - An array with a numeric ID key

Associative array - An array where each ID key is

associated with a value

Multidimensional array - An array containing one or

more arrays

Three Kind of array

Sanjay Patel

Page 30: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

A numeric array stores each element with a numeric ID

key.

There are different ways to create a numeric array.

1. In this example the ID key is automatically assigned.

2. In this example we assign the ID key manually:

Numeric Arrays

Sanjay Patel

Page 31: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

3. The ID keys can be used in a script:

Cont’d..

Sanjay Patel

Page 32: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

An associative array, each ID key is associated with

a value.

When storing data about specific named values, a

numerical array is not always the best way to

do it.

With associative arrays we can use the values as

keys and assign values to them.

Associative array

Sanjay Patel

Page 33: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Example

Sanjay Patel

Page 34: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Example

Sanjay Patel

Page 35: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

In a multidimensional array, each element in the

main array can also be an array. And each

element in the sub-array can be an array, and so

on.

Multidimensional array

Sanjay Patel

Page 36: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Example

Sanjay Patel

Page 37: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Looping statements in PHP are used to execute the

same block of code a specified number of times.

Very often when you write code, you want the same

block of code to run a number of times. You can use

looping statements in your code to perform this.

PHP Looping

Sanjay Patel

Page 38: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

While ,Do..while, For loop..

Sanjay Patel

Page 39: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

The real power of PHP comes from its functions.

In PHP, there are more than 700 built-in functions.

we will create our own functions.

A function will be executed by a call to the function.

You may call a function from anywhere within a

page.

Syntax:- function functionName()

{

code to be executed;

}

PHP Function

Sanjay Patel

Page 40: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Example

Sanjay Patel

<?php

function writeName()

{

echo "Kai Jim Refsnes";

}

echo "My name is ";

writeName();

?>

• The result would be:

My name is Kai Jim Refsnes

Page 41: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

PHP function

Sanjay Patel

Page 42: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

PHP Functions - Adding parameters

Sanjay Patel

<?php

function writeName($fname)

{

echo $fname . " Refsnes.<br>";

}

echo "My name is ";

writeName("Kai Jim");

echo "My sister's name is ";

writeName("Hege");

echo "My brother's name is ";

writeName("Stale");

?>

• The result would be:

My name is Kai Jim Refsnes.

My sister's name is Hege Refsnes.

My brother's name is Stale Refsnes.

Page 43: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

PHP Functions - Two parameters

Sanjay Patel

<?php

function

writeName($fname,$punctuation)

{

echo $fname . " Refsnes" . $punctuation .

"<br>";

}

echo "My name is ";

writeName("Kai Jim",".");

echo "My sister's name is ";

writeName("Hege","!");

echo "My brother's name is ";

writeName("Ståle","?");

?>

• The result would be:

My name is Kai Jim Refsnes.

My sister's name is Hege Refsnes!

My brother's name is Ståle

Refsnes?

Page 44: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

PHP function- Return Value

Sanjay Patel

Page 45: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

The PHP date() function is used to format a

time or a date.

PHP date()

Sanjay Patel

Page 46: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

The first parameter in the date() function specifies

how to format the date/time.

It uses letters torepresent date and time formats.

Here are some of the letters that can be used:

d - The day of the month (01-31)

m - The current month, as a number (01-12)

Y - The current year in four digits

PHP date()

Sanjay Patel

Page 47: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Example

Sanjay Patel

Page 48: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Server Side Includes (SSI) are used to create

functions,

headers,

footers,

elements

that will be reused on multiple pages.

SSI(server side includes)

Sanjay Patel

Page 49: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

You can insert the content of a file into a PHP file

before the server executes it, with the include() or

require() function.

The two functions are identical in every way, except

how they handle errors:

include() generates a warning, but the script will

continue execution

require() generates a fatal error, and the script

will stop

SSI ( include(), require() function)

Sanjay Patel

Page 50: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

This can save the developer a considerable amount

of time.

This means that you can create a standard header

or menu file that you want all your web pages to

include.

When the header needs to be updated, you can

only update this one include file, or when you add a

new page to your site, you can simply change the

menu file (instead of updating the links on all web

pages).

Advantages

Sanjay Patel

Page 51: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

The include() function takes all the text in a

specified file and copies it into the file that uses the

include function.

If an error occurs, the include() function generates a

warning, but the script will continue execution

PHP include() function

Sanjay Patel

Page 52: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Example

Sanjay Patel

Page 53: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Output

Sanjay Patel

Page 54: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

The require() function is identical to include(), they

only handle errors differently.

The include() function generates a warning (but the

script will continue execution)

while the require() function generates a fatal error

(and the script execution will stop after the error).

PHP require() function

Sanjay Patel

Page 55: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Notice that the echo statement is still executed! This is

because a Warning does not stop the script execution.

Example ( use include() function )

Sanjay Patel

Page 56: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

The echo statement was not executed because the

script execution stopped after the fatal error.

Example ( use require() function )

Sanjay Patel

Page 57: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Another Example

Sanjay Patel

Page 58: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

It is recommended to use the require() function

instead of include(), because scripts should not

continue executing if files are missing or misnamed.

Recommendation

Sanjay Patel

Page 59: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

The fopen() function is used to open files

in PHP.

File Handling

Data Storage

Though slower than a database

Manipulating uploaded files

From forms

Creating Files for download

PHP File Handling

Sanjay Patel

Page 60: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

A file is opened with fopen() as a “stream”, andPHP returns a ‘handle’ to the file that can be usedto reference the open file in other functions.

Each file is opened in a particularmode.

A file is closed with fclose() or when your scriptends.

Open/Close a File

Sanjay Patel

Page 61: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

The first parameter of this function contains the

name of the file to be opened

The second parameter specifies in which mode the

file should be opened.

Opening a File ( fopen() )

Sanjay Patel

Page 62: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

File Open Modes

Sanjay Patel

Page 63: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

If the fopen() function is unable to open

the specified file, it returns 0 (false).

Note

Sanjay Patel

Page 64: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Example

Sanjay Patel

Page 65: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Fclose()

Sanjay Patel

Page 66: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

We need to be aware of the End Of File (EOF) point..

feof($file) The feof() function checks if the "end-of-file" (EOF)

has been reached.

The feof() function is useful for looping through

data of unknown length.

Check end of file ( feof() )

Sanjay Patel

Page 67: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

The fgets() function is used to read a single line from

a file.

Note: After a call to this function the file pointer has

moved to the next line.

fgets() (Reading a File Line by Line)

Sanjay Patel

Page 68: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Example

Sanjay Patel

Page 69: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

The fgetc() function is used to read a single

character from a file.

Note: After a call to this function the file pointer

moves to the next character.

fgetc() (Reading a File Character by Character)

Sanjay Patel

Page 70: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

The PHP $_GET and $_POST variables are used to

retrieve information from forms, like user input.

PHP Form Handling:-The most important thing to

notice when dealing with HTML forms and PHP is

that any form element in an HTML page will

automatically be available to your PHP scripts.

PHP Forms and User Input

Sanjay Patel

Page 71: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Sample Output:-

The example HTML page above contains two input fields

and a submit button.

When the user fills in this form and click on the submit

button, the form data is sent to the "welcome.php" file.

Example

Sanjay Patel

Page 72: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

The $_GET variable is used to collect values from

a form with method="get“.

The $_GET variable is an array of variable names

and values sent by the HTTP GET method.

Information sent from a form with the GET method is

visible to everyone (it will be displayed in the

browser's address bar)

it has limits on the amount of information to send

(max. 100 characters).

PHP $_GET

Sanjay Patel

Page 73: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Example

Sanjay Patel

Page 74: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

When using the $_GET variable all variable names

and values are displayed in the URL.

So this method should not be used when sending

passwords or other sensitive information!

However, because the variables are displayed in

the URL, it is possible to bookmark the page. This

can be useful in some cases.

Why use $_GET?

Sanjay Patel

Page 75: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

The $_POST variable is used to collect values

from a form with method="post".

The $_POST variable is an array of variable names

and values sent by the HTTP POST method.

Information sent from a form with the POST method

is invisible to others

It has no limits on the amount of information to send.

PHP $_POST

Sanjay Patel

Page 76: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Example

Sanjay Patel

Page 77: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Variables sent with HTTP POST are not shown in the

URL

Variables have no length limit

However, because the variables are not displayed in

the URL, it is not possible to bookmark the page.

Why use $_POST?

Sanjay Patel

Page 78: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

A cookie is often used to identify a user.

What is a Cookie?

A cookie is often used to identify a user. A cookie is

a small file that the server embeds on the user's

computer.

Each time the same computer requests a page with

a browser, it will send the cookie too.

With PHP, you can both create and retrieve cookie

values.

PHP Cookies

Sanjay Patel

Page 79: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

A cookie is a small text file that is stored on a user’s computer.

Each cookie on the user’s computer is connected to a particular domain.

Each cookie be used to store up to 4kB of data.

A maximum of 20 cookies can be stored on a user’s PC per domain.

What is a Cookie?

Sanjay Patel

Page 80: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

The setcookie() function is used to set a cookie.

Note: The setcookie() function must appear

BEFORE the <html> tag.

Syntax:-

How to Create a Cookie?

Sanjay Patel

Page 81: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

setcookie(name [,value [,expire [,path[,domain [,secure]]]]])

Syntax (set cookie)

Sanjay Patel

name cookie name

value data to store (string)

expire UNIX timestamp when the cookie expires.

Default is that cookie expires when browser

is closed.

path Path on the server within and below which

the cookie is available on.

domain Domain at which the cookie is available for.

secure If cookie should be sent over HTTPS

connection only. Default false.

Page 82: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

In the example below, we will create a cookie named

"user" and assign the value "Alex Porter" to it.

We also specify that the cookie should expire after

one hour:

Example

Sanjay Patel

Page 83: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

The PHP $_COOKIE variable is used to retrieve a

cookie value.

In the example below, we retrieve the value of the

cookie named "user" and display it on a page:

How to Retrieve a Cookie Value?

Sanjay Patel

Page 84: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

In the following example we use the isset() function

to find out if a cookie has been set.

Cont’d..

Sanjay Patel

Page 85: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

When deleting a cookie you should assure that the

expiration date is in the past.

How to Delete a Cookie?

Sanjay Patel

Page 86: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

When you are working with an application, you

open it, do some changes and then you close it. This

is much like a Session.

A PHP session variable is used to store information

about, or change settings for a user session.

Session variables hold information about one single

user, and are available to all pages in one

application.

PHP Session

Sanjay Patel

Page 87: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

When you are working with an application, you

open it, do some changes and then you close it. This

is much like a Session.

The computer knows who you are. It knows when you

start the application and when you end. But on the

internet there is one problem: the web server does

not know who you are and what you do because

the HTTP address doesn't maintain state.

PHP Session Variables

Sanjay Patel

Page 88: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

A PHP session solves this problem by allowing you to

store user information on the server for later use (i.e.

username, shopping items, etc).

However, session information is temporary and will be

deleted after the user has left the website.

If you need a permanent storage you may want to

store the data in a database.

Sessions work by creating a unique id (UID) for each

visitor and store variables based on this UID.

The UID is either stored in a cookie or is propagated

in the URL.

Solution

Sanjay Patel

Page 89: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Before you can store user information in your PHP

session, you must first start up the session.

Note: The session_start() function must appear

BEFORE the <html> tag.

The code above will register the user's session with

the server, allow you to start saving user

information, and assign a UID for that user's session.

Syntax (Starting a PHP Session)

Sanjay Patel

Page 90: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

The correct way to store and retrieve session

variables is to use the PHP $_SESSION variable:

Storing a Session Variable

Sanjay Patel

Page 91: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

Example

Sanjay Patel

Page 92: Classical Encryption Techniques - Welcome To Computer …svbitce2010.weebly.com/uploads/8/4/4/5/8445046/che6… ·  · 2013-04-18PHP with XHTML  Not recommended. ... -Code

If you wish to delete some session data, you can use

the unset() or the session_destroy() function.

The unset() function is used to free the specified

session variable.

You can also completely destroy the session by calling the Session_destroy()

function:

Note: session_destroy() will reset your session and

you will lose all your stored session data.

Destroying a Session

Sanjay Patel