You are on page 1of 40

Passing variables

between pages

Summary of the
previous lecture

Operators in PHP
Conditional Statements in PHP
Looping Statements
Arrays in PHP

Outline
Super Global variables
Passing variables between pages
Using
Using
Using
Using

query strings
sessions
cookies
forms

Super Global Variables


PHP automatically makes few
variables available in your
program
These are array variables and can
be accessed by name
These variables are called superglobal variables because they can
be accessed without regard to
scope
4

Super Global
Variables
$_GET: contains all the query string
variables that were attached to the
URL

$_POST: contains all the submitted


form variables and their data
$_SESSION:contains data available
to a PHP script that has previously
been stored in a session

Passing variables
through URL
Passing variables through pages URL
http://www.mydomain.com/news/articles/
showart.php?id=12345
http://www.mydomain.com/news/articles/
showart.php?id=12345&lang=en

use urlencode() if there are special


characters in a variable
$name = urlencode(Life of Gul Makaee);

Disadvantages
Everyone can see the values of the variables
Users can arbitrarily change the variable
6

Passing variables with


Sessions
A session is basically a temporary
set of variables that exists only
until the browser has shut down.
E.g. Whether an authorized person has
logged in to the site. The information is
stored temporarily for your program

Every session is assigned a unique


sessionID.
$_SESSION: represents data
available to a PHP script that has
previously been stored in a session

3. Passing variables using


sessions
Session
starts

Session
variable is
created
Link to the
next page

3. Passing variables using


sessions
Session
starts

Session variable is
accessed

link

3. Passing variables using


sessions

Session
variables
value

10

3. Passing variables using


sessions
All PHP session information is at the top of the
page, before any HTML code is used. This is
very important! If there is even a leading space
before the PHP code at the top of the page, you
will receive an error such as:
Warning: session_start(): Cannot send session
cache limiter - headers already sent(output
started at C:\Program Files\Apache Software
Foundation\Apache2.2\htdocs\moviesite.php:1
) in C:\Program Files\Apache Software
Foundation\Apache2.2\htdocs\moviesite.php
on line 2
11

3. Passing variables
using sessions
session_start()- is used to start a
session
$_SESSION[variable_name]- is
used to store data in session
variable
session_destroy()- is used to
destroy a session
unset($_SESSION[variable_name])
- is used to unset a specific
variable
12

3. Passing variables using


sessions
Session is
destroyed

Session is
accessed

13

Passing variables with


Cookies
Cookies are tiny bits of information stored on
your web site visitors computer.
Cookies are used to gain information such as a
persons IP address, operating system, however
primary objective is to store information only
People are using cookies to track your browsing
habits. People disable cookies by calling these
activities as an invasion to their privacy
Also people can potentially open cookie files and
change them, as they are stored in a commonly
named directly
Relying on cookie information isnt a best idea
14
from a web development point

Passing variables with


Cookies
Why to use cookies?
Storing information in cookies are long
lived for as long as the developer has
decided and automatically expire.
Sessions can be used in conjunction with
cookies
You can set the life of these cookies
longer than the life of the browse suing
the session.cookie.lifetime
configuration in php.ini file
15

Passing variables with


Cookies
To set a cookie use setcookie() function. You
can set the following information set in it:
Cookie name (mandatory)
Value of the cookie (e.g. persons username)
Time in seconds when the cookie will expire
Path (the directory where the cookie will be
saved, the default is usually sufficient)
Domain(domains that may access this cookie,
optional)
Whether a cookie must have a secure HTTPS
connection to be set(default is 0, to enable set
16
it to 1)

Passing variables with


Cookies
<?php
setcookie(username , Humaira ,
time + 60);

?>

17

Cookies and Sessions


Cookies

Sessions

Limited storage space

Practically unlimited
space

Insecure storage i.e. On Reasonably securely


the client-side
stored on the serverside
User controlled

No user control

18

Passing form data


Forms provide a mean of
submitting information from
the client to the server
We can create HTML forms
using <form> tag
Method and action are the
most common attributes of
<form>
19

1. Passing form data


action - gives the URL of the
application that is to receive
and process the forms data
method - sets the HTTP method
that the browser uses to send
the form's data to the server for
processing
most common methods are POST or
GET
20

1. Passing form data


Get method : all form data is
encoded into the URL,
appended theactionURL as
query string
parameters
Asad
asd@gmail.
comsub
mit
Action page
name

Input
field
name

Value
entered by
user

21

1. Passing form data


Post method: form data
appears within the message
body of the HTTP request

22

1.1 Super Global


Variables
<body>
<form method=get
action=action.php>
<input type=text
name=name>
<input type=text
name=email>
<input type=submit>
</form>
</body>
$_GET

nam
e

Asad

asd@gmail.
com
sub
mit

email

asd@gmail.c
Asad
om
23

1.1 Super Global


Variables
<body>
Asad
<form method=post>
asd@gmail.
<input type=text
com
sub
name=name>
mit
<input type=text
name=email>
<input type=submit>
</form>
$_POS
</body>
asd@gmail.c
T
Asad
om
nam
e

email

24

1.2 Accessing form data on


action page
$_GET

Asad

asd@gmail.
comsub
mit

nam
e

Asad

email

asd@gmail.c
om

Ation.php
<?php
$name =
$_GET[name];
$email =
$_GET[email];
?>
25

1.2 Accessing form data on


action page
$_POS
T

Asad

asd@gmail.
comsub
mit

nam
e

Asad

email

asd@gmail.c
om

Ation.php
<?php
$name =
$_POST[name];
$email =
$_POST[email];
?>
26

2. Passing text field


data
Post
Method

Text
field

Text
field
name

27

2. Passing text field


data
Data is
received

Display a
message

28

2. Passing text field


data

We are at form
page

We are on
action page

29

2. Passing hidden field


data

Hidde
n field

Field
name

Hidden
value

Accessing
hidden value

30

2. Passing hidden field


data

31

2.3 Getting value from


checkbox
label

name

value

Getting
value of
C
Getting
value of
VB

32

2.3 Getting value from


checkbox

33

2.3 Getting value from


checkbox

Checking for value


of C

34

2.4 Getting value from


radio button
Same
name

Value is
set

Value is not
set
Getting value
of radio

35

2.4 Getting value from


radio button

36

2.5 Getting value from


select list
Name of the
list
Option and
value

37

2.5 Getting value from


select list

38

Summary
Super global variables
Passing data between pages

39

References
Chapter 2, Beginning
PHP6,Apache,Mysql web
development by Matt Doyle, Wrox
publishers, 2009, ISBN: 0470413964
Chapter 13, Beginning PHP and
MySQL by W. Jason Gilmore, Apress
publisher, 4th edition; 2010, ISBN-13
(electronic): 978-1-4302-3115-8.
40