Download - Php file upload, cookies & session
PHP File Upload, Cookies & Session
Jamshid HashimiTrainer, Cresco Solution
http://www.jamshidhashimi.com [email protected] @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<?phpsession_start();// store session data$_SESSION['visit'] = 1;?>
<html><body>
<?php//retrieve session dataecho "Pageviews=". $_SESSION['visit'];?>
</body></html>
DEMO 3
QUESTIONS?