PHP File Upload, Cookies &
Session
Jamshid Hashimi
Trainer, Cresco Solution
http://www.jamshidhashimi.com
jamshid@netlinks.af
@jamshidhashimi
ajamshidhashimi
Afghanistan Workforce
Development Program
Agenda
• Date
• Include & Require
• File Upload
• Cookies
• Sessions
Date
• The PHP date() function is used to format a
time and/or date.
echo date("Y/m/d") . "<br>";
echo date("Y.m.d") . "<br>";
echo date("Y-m-d");
Date
• The strftime function can format that
timestamp in any way you’d like. You’ll pass it
a format string and the timestamp and get the
date back out.
echo strftime("%B %d, %Y", time());
Date
Include & Require
• In PHP, you can insert the content of one PHP
file into another PHP file before the server
executes it.
– require will produce a fatal error
(E_COMPILE_ERROR) and stop the script
– include will only produce a warning (E_WARNING)
and the script will continue
Include & Require
include 'filename';
or
require 'filename';
<div class="leftmenu">
<?php include 'menu.php'; ?>
</div>
DEMO 1
PHP File Upload
• With PHP, it is possible to upload files to the
server.
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file"
id="file"><br>
<input type="submit" name="submit"
value="Submit">
</form>
</body>
</html>
DEMO 2
Cookies
• 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.
• I am not a big fan of cookies!
Cookies
• We cannot trust data in $_COOKIES to contain
legitimate values
– Can easily modified!
setcookie(name, value, expire, path, domain);
setcookie("class", "AWD 01", time()+3600);
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.
Sessions
• php.ini – session.save_path
• Maximum size of a PHP session
– 128 M
• session_start()
• isset()
• unset()
• session_destroy()
Sessions
<?php
session_start();
// store session data
$_SESSION['visit'] = 1;
?>
<html>
<body>
<?php
//retrieve session data
echo "Pageviews=". $_SESSION['visit'];
?>
</body>
</html>
DEMO 3
QUESTIONS?

Php file upload, cookies & session

  • 1.
    PHP File Upload,Cookies & Session Jamshid Hashimi Trainer, Cresco Solution http://www.jamshidhashimi.com jamshid@netlinks.af @jamshidhashimi ajamshidhashimi Afghanistan Workforce Development Program
  • 2.
    Agenda • Date • Include& Require • File Upload • Cookies • Sessions
  • 3.
    Date • The PHPdate() function is used to format a time and/or date. echo date("Y/m/d") . "<br>"; echo date("Y.m.d") . "<br>"; echo date("Y-m-d");
  • 4.
    Date • The strftimefunction can format that timestamp in any way you’d like. You’ll pass it a format string and the timestamp and get the date back out. echo strftime("%B %d, %Y", time());
  • 5.
  • 6.
    Include & Require •In PHP, you can insert the content of one PHP file into another PHP file before the server executes it. – require will produce a fatal error (E_COMPILE_ERROR) and stop the script – include will only produce a warning (E_WARNING) and the script will continue
  • 7.
    Include & Require include'filename'; or require 'filename'; <div class="leftmenu"> <?php include 'menu.php'; ?> </div>
  • 8.
  • 9.
    PHP File Upload •With PHP, it is possible to upload files to the server. <!DOCTYPE html> <html> <body> <form action="upload.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file"><br> <input type="submit" name="submit" value="Submit"> </form> </body> </html>
  • 10.
  • 11.
    Cookies • A cookieis 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. • I am not a big fan of cookies!
  • 12.
    Cookies • We cannottrust data in $_COOKIES to contain legitimate values – Can easily modified! setcookie(name, value, expire, path, domain); setcookie("class", "AWD 01", time()+3600);
  • 13.
    Session • A PHPsession 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.
  • 14.
    Sessions • php.ini –session.save_path • Maximum size of a PHP session – 128 M • session_start() • isset() • unset() • session_destroy()
  • 15.
    Sessions <?php session_start(); // store sessiondata $_SESSION['visit'] = 1; ?> <html> <body> <?php //retrieve session data echo "Pageviews=". $_SESSION['visit']; ?> </body> </html>
  • 17.
  • 18.

Editor's Notes