Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PHP Cookie/Session MySQL 資料庫操作

PHP Cookie/Session MySQL 資料庫操作

David Kuo (Davy)

November 17, 2014
Tweet

More Decks by David Kuo (Davy)

Other Decks in Education

Transcript

  1. Cookie • a.k.a. HTTP cookie, web cookie, or browser cookie

    • Tracking cookies — commonly used as ways to compile long-term records of individuals' browsing histories. • Authentication cookies — the most common method used by web servers to know whether the user is logged in or not, and which account they are logged in with.
  2. Tracking cookies • Used to track internet users' web browsing.

    1. When user requests a page of the site with no cookie, the server presumes that this is the first page visited by the user, and creates a token and sends it as a cookie back. 2. The cookie will automatically be sent to the server every time; the server will stores the requested URL the time of the request, and the cookie into log. • By analyzing the log, it is possible to find out which pages the user has visited, in what sequence, and for how long. Then, figure out the trending of users’ habit.
  3. Personalization cookies • Used to remember the information about the

    user who has visited a website in order to show relevant content in the future. • Used to remember users' preferences. Users select their preferences by submitting to the server. The server stores the preferences in a cookie. This way, every time the user accesses a page, the server can personalize the page according to the user preferences.
  4. Structure of Cookie • Cookie has a size of 4KB

    • (name, value) pair of the cookie (i.e. name=value) • Expiry of the cookie • Path the cookie is good for • Domain the cookie is good for • Need for a secure connection to use the cookie • Whether or not the cookie can be accessed through other means than HTTP (i.e., JavaScript)
  5. bool setcookie ( 
 string $name
 [, string $value
 [,

    int $expire = 0
 [, string $path
 [, string $domain
 [, bool $secure = false
 [, bool $httponly = false
 )
  6. bool setcookie ( 
 string $name
 [, string $value
 [,

    int $expire = 0
 [, string $path
 [, string $domain
 [, bool $secure = false
 [, bool $httponly = false
 ) expire at 
 (0 for this session)
  7. bool setcookie ( 
 string $name
 [, string $value
 [,

    int $expire = 0
 [, string $path
 [, string $domain
 [, bool $secure = false
 [, bool $httponly = false
 ) the path cookie good for
  8. bool setcookie ( 
 string $name
 [, string $value
 [,

    int $expire = 0
 [, string $path
 [, string $domain
 [, bool $secure = false
 [, bool $httponly = false
 ) the domain cookie good for
  9. bool setcookie ( 
 string $name
 [, string $value
 [,

    int $expire = 0
 [, string $path
 [, string $domain
 [, bool $secure = false
 [, bool $httponly = false
 ) only in https
  10. bool setcookie ( 
 string $name
 [, string $value
 [,

    int $expire = 0
 [, string $path
 [, string $domain
 [, bool $secure = false
 [, bool $httponly = false
 ) only through http header
  11. Session • Storage session on server • Session has no

    size limit • There is a cookie to save session ID • (name, value) pair of the session (i.e. name=value)
  12. UPDATE `table`
 SET `bio` = "Hi",
 `email` = "[email protected]"
 WHERE

    `username` = "Ann"; 選取資料表 修改內容 選取條件
  13. <?php $database = new mysqli(
 "localhost", 
 "r00t", 
 "p455w0rd",

    
 "db_test"
 ); ?> host username password database
  14. mysqli::__construct (
 [ string $host = ini_get("mysqli.default_host")
 [, string $username

    = ini_get("mysqli.default_user")
 [, string $passwd = ini_get("mysqli.default_pw")
 [, string $dbname = ""
 [, int $port = ini_get("mysqli.default_port")
 [, string $socket = ini_get("mysqli.default_socket")
 )
  15. mysqli::__construct (
 [ string $host = ini_get("mysqli.default_host")
 [, string $username

    = ini_get("mysqli.default_user")
 [, string $passwd = ini_get("mysqli.default_pw")
 [, string $dbname = ""
 [, int $port = ini_get("mysqli.default_port")
 [, string $socket = ini_get("mysqli.default_socket")
 ) k
  16. mysqli::__construct (
 [ string $host = ini_get("mysqli.default_host")
 [, string $username

    = ini_get("mysqli.default_user")
 [, string $passwd = ini_get("mysqli.default_pw")
 [, string $dbname = ""
 [, int $port = ini_get("mysqli.default_port")
 [, string $socket = ini_get("mysqli.default_socket")
 ) k
  17. <?php $result = $database->query(
 'SELECT `*` FROM `table` WHERE `username`

    = "Davy"'
 ); ?> http://php.net/manual/en/mysqli.query.php • bool • mysqli_result • http://php.net/manual/en/class.mysqli-result.php • fetch_all(……) • fetch_array(……) • fetch_assoc(……) • int $field_count
  18. Guestbook • Write a simple guestbook in PHP with MySQL

    • Use mysqli::prepare to build your statements • Build a user system that everyone can register, login and logout • Every user can create an new post, and edit or delete their own post • Every user can reply on any post, and also can edit or delete their own reply • A post or a reply must show the informations of `Author`, `Title`, `Content`, `Time`, etc
  19. Bonus • Welcome to add some features for bonus points

    • e.g. admin center, paginate, security, points system, activating account, etc.
  20. Deadline • Demo on Dec. 8 • Mail the source

    code and the link to your guestbook 
 to [email protected] or [email protected] 
 before 12/8 24:00
 remember to tell me who you are
  21. Need Help? • PHP.net, http://php.net/docs.php • Google, http://lmgtfy.com/ • Facebook

    group, 
 https://www.facebook.com/groups/1476116432662453/ • Or contact us directly