PHP 5 - PowerPoint PPT Presentation

About This Presentation
Title:

PHP 5

Description:

Cookies must be deleted with the same parameters as they were set with. ... PS:Session variable will be created on using even if you will not register it! Use it ... – PowerPoint PPT presentation

Number of Views:15
Avg rating:3.0/5.0
Slides: 13
Provided by: nii71
Category:
Tags: php | register

less

Transcript and Presenter's Notes

Title: PHP 5


1
PHP 5
  • Cookies, Sessions

2
Server Side Includes
  • You can insert the content of one file into
    another file before the server executes it, with
    the require() function. The require() function is
    used to create functions, headers, footers, or
    elements that will be reused on multiple pages.
  • lt?php require("header.htm") ?gt

3
How to create variables storing values across php
scripts calls?
. . .
  • Client-server connection is not permanent
  • gt Cannot be saved in program memory
  • There are many clients connecting simultaneously
  • gt Cannot be saved in file (you cannot
    identify clients as well sometimes)

4
Different mechanisms of the same solution
  • Cookies
  • Cookies are a mechanism for storing data in the
    remote browser and thus tracking or identifying
    return users.
  • Sessions
  • Session support in PHP consists of a way to
    preserve certain data across subsequent accesses.
    This enables you to build more customized
    applications and increase the appeal of your web
    site.

5
What is a Cookie?
  • A cookie is a small file that the server embeds
    on the user's computer. Each time the same
    computer requests for a page with a browser, it
    will send the cookie too. With PHP, you can both
    create and retrieve cookie values.

6
How to Create a Cookie
  • The setcookie() function is used to create
    cookies.
  • Note The setcookie() function must appear BEFORE
    the lthtmlgt tag.
  • setcookie(name, value, expire, path,
    domain, secure)
  • This sets a cookie named "uname" - that expires
    after ten hours.
  • lt?php setcookie("uname", name, time()36000) ?gt
  • lthtmlgt ltbodygt

7
How to Retrieve a Cookie Value
  • To access a cookie you just refer to the cookie
    name as a variable or use _COOKIE array
  • Tip Use the isset() function to find out if a
    cookie has been set.
  • lthtmlgt ltbodygt
  • lt?php
  • if (isset(uname))
  • echo "Welcome " . uname . "!ltbr /gt"
  • else
  • echo "You are not logged in!ltbr /gt" ?gt
  • lt/bodygt lt/htmlgt

8
How to Delete a Cookie
  • It will expire
  • or
  • Cookies must be deleted with the same parameters
    as they were set with. If the value argument is
    an empty string (""), and all other arguments
    match a previous call to setcookie, then the
    cookie with the specified name will be deleted
    from the remote client.

9
What is a Session?
  • The session support allows you to register
    arbitrary numbers of variables to be preserved
    across requests.
  • A visitor accessing your web site is assigned an
    unique id, the so-called session id. This is
    either stored in a cookie on the user side or is
    propagated in the URL.

10
How to Create a Session
  • The session_start() function is used to create
    cookies.
  • lt?php
  • session_start()
  • ?gt

11
How to Retrieve a Session Value
  • Register Session variable
  • session_register('var1','var2',...) // will also
    create a session
  • PSSession variable will be created on using even
    if you will not register it!
  • Use it
  • lt?php
  • session_start()
  • if (!isset(_SESSION'count'))
  • _SESSION'count' 0
  • else
  • _SESSION'count'
  • ?gt

12
How to Delete a Session Value
  • session_unregister(varname)
  • How to destroy a session
  • session_destroy()
Write a Comment
User Comments (0)
About PowerShow.com