PHP and the Web: Session : 4. Predefined variables PHP provides a large number of predefined global variables to any script which it runs also called.

Slides:



Advertisements
Similar presentations
PHP Form and File Handling
Advertisements

LIS651 lecture 3 taming PHP Thomas Krichel
Powerpoint Templates Page 1 Powerpoint Templates Server Side Scripting PHP.
UFCE8V-20-3 Information Systems Development 3 (SHAPE HK) Lecture 3 PHP (2) : Functions, User Defined Functions & Environment Variables.
UFCE8V-20-3 Information Systems Development 3 (SHAPE HK)
Cookies, Sessions. Server Side Includes You can insert the content of one file into another file before the server executes it, with the require() function.
Lecture 6/2/12. Forms and PHP The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input When dealing with HTML forms.
1 Chapter 5 – Handling HTML Controls in Web Pages spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science.
Chapter 10 Managing State Information Using Sessions.
Servlets and a little bit of Web Services Russell Beale.
Chapter 10 Managing State Information PHP Programming with MySQL.
Using Session Control in PHP tMyn1 Using Session Control in PHP HTTP is a stateless protocol, which means that the protocol has no built-in way of maintaining.
CSE 154 LECTURE 13: SESSIONS. Expiration / persistent cookies setcookie("name", "value", expiration); PHP $expireTime = time() + 60*60*24*7; # 1 week.
Objectives Learn about state information
Christopher M. Pascucci Basic Structural Concepts of.NET Browser – Server Interaction.
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)Slide Topic Files.
PHP Security.
Advance Database Management Systems Lab no. 5 PHP Web Pages.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Basic php. Php Official site: PhP manual: Useful package (Apache, MySql,PhP):
INTERNET APPLICATION DEVELOPMENT For More visit:
Lecture 6 – Form processing (Part 1) SFDV3011 – Advanced Web Development 1.
Lecture 7 – Form processing (Part 2) SFDV3011 – Advanced Web Development 1.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Cookies & Sessions.
CHAPTER 12 COOKIES AND SESSIONS. INTRO HTTP is a stateless technology Each page rendered by a browser is unrelated to other pages – even if they are from.
Comp2513 Forms and CGI Server Applications Daniel L. Silver, Ph.D.
CSC 2720 Building Web Applications Cookies, URL-Rewriting, Hidden Fields and Session Management.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
JavaScript, Fourth Edition
Chapter 8 Cookies And Security JavaScript, Third Edition.
School of Computing and Information Systems CS 371 Web Application Programming PHP – Forms, Cookies, Sessions and Database.
Week seven CIT 354 Internet II. 2 Objectives Database_Driven User Authentication Using Cookies Session Basics Summary Homework and Project 2.
PHP1-1 PHP Lecture 2 Xingquan (Hill) Zhu
Lecture 8 – Cookies & Sessions SFDV3011 – Advanced Web Development 1.
1 Chapter 9 – Cookies, Sessions, FTP, and More spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science.
Cookies & Session Web Technology
CSC 2720 Building Web Applications Server-side Scripting with PHP.
14. Uploading Files to MySQL Database. M. Udin Harun Al Rasyid, S.Kom, Ph.D Desain dan.
STATE MANAGEMENT.  Web Applications are based on stateless HTTP protocol which does not retain any information about user requests  The concept of state.
PHP Programming with MySQL Slide 10-1 CHAPTER 10 Managing State Information.
PHP. $_GET / $_POST / $_SESSION PHP uses predefined variables to provide access to important information about the server and requests from a browser.
Sessions in PHP – Page 1 of 13CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: Sessions in PHP Reading: Williams.
ITM © Port, Kazman1 ITM 352 More on Forms Processing.
Sessions and Cookies State Management, Cookies, Sessions, Hidden Fields SoftUni Team Technical Trainers Software University
הרצאה 4. עיבוד של דף אינטרנט דינמי מתוך Murach’s PHP and MySQL by Joel Murach and Ray Harris.  דף אינטרנט דינמי משתנה עפ " י הרצת קוד על השרת, יכול להשתנות.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
PHP Session ISYS 475. Session The web server starts a session when a visitor visiting your web site and assigns a unique id, the session id for the session.
How to maintain state in a stateless web Shirley Cohen
SESSIONS 27/2/12 Lecture 8. ? Operator Similar to the if statement but returns a value derived from one of two expressions by a colon. Syntax: (expression)
PHP-language, sessions Teppo Räisänen Principal Lecturer Oulu University of Applied Sciences School of Business and Information Management
Since you’ll need a place for the user to enter a search query. Every form must have these basic components: – The submission type defined with the method.
1 DIG 3134 Lecture 6: Maintaining State Michael Moshell University of Central Florida Media Software Design.
ITM © Port,Kazman 1 ITM 352 Cookies. ITM © Port,Kazman 2 Problem… r How do you identify a particular user when they visit your site (or any.
1 PHP HTTP After this lecture, you should be able to know: How to create and process web forms with HTML and PHP. How to create and process web forms with.
Web Page Designing With Dreamweaver MX\Session 1\1 of 9 Session 3 PHP Advanced.
Unit-6 Handling Sessions and Cookies. Concept of Session Session values are store in server side not in user’s machine. A session is available as long.
Session 11: Cookies, Sessions ans Security iNET Academy Open Source Web Development.
CGS 3066: Web Programming and Design Spring 2016 PHP.
HTTP Transactions 1. 2 Client-Server Model 3 HTTP HyperText Transport Protocol Native protocol for WWW Sits on top of internet’s TCP/IP protocol HTTP.
PHP: Further Skills 02 By Trevor Adams. Topics covered Persistence What is it? Why do we need it? Basic Persistence Hidden form fields Query strings Cookies.
Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Fundamentals of Web DevelopmentRandy.
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● / www,histpk.org Hidaya Institute of Science & Technology
Managing State Chapter 13.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
DBW - PHP DBW2017.
ITM 352 Cookies.
Open Source Programming
<?php require("header.htm"); ?>
SESSION TRACKING BY DINESH KUMAR.R.
Presentation transcript:

PHP and the Web: Session : 4

Predefined variables PHP provides a large number of predefined global variables to any script which it runs also called superglobal or autoglobal

PHP Superglobals $_SERVER $_GET $_POST $_COOKIE $_FILES $_ENV $_SESSION

$_SERVER $_SERVER stores several web server related variables which is very useful for various purposes.

$_SERVER echo $_SERVER[‘PHP_SELF’]; Returns the file name currently executing in webserver.

$_SERVER echo $_SERVER[‘PHP_SELF’]; Displays the server name echo $_SERVER[' REMOTE_ADDR' ] Displays the remote address of a client

$_SERVER … There are several information available in the $_SERVER array check manual for more…

$_POST and $_GET A web form’s POST and GET variables are stored in associated array $_POST and $_GET. like, $_GET[“varname”]; $_POST[“varname”];

GET Example <?php echo $_GET[“fname”]; eco $_GET[“addr”]; ?> Name: Addr:

Handling Form <?php if ( isset($_GET[“submit”] ) ) { echo $_GET[“fname”]; echo $_GET[“addr”]; } ?> Name: Addr:

Handling Form <?php if ( isset($_POST[“submit”] ) ) { echo $_POST[“fname”]; echo $_POST[“addr”]; } ?> Name: Addr:

Variable Function PHP has several variable function especially useful to validate, verify and manipulate the variable.

isset($var) isset returns TRUE if var exists; FALSE otherwise Example, $k=0; if(isset($t) ) echo “t is declared”; if( isset($t) ) echo “k is declared”;

empty($var) Determine whether a variable is empty Example, $var =0; if (empty($var)) { echo '$var is either 0, empty, or not set at all'; }empty

Checking variable type To check whether a variable is an array use, is_array($var) Example $var = array(23,45,23); If( is_array($var)) { echo “Array type”; }

Radio Button <? if(isset($_POST[“submit”]) ) { $opt = $_POST[“opt”]; echo “Your choice is $opt; } Apple Orange ?>

Checkbox <? if(isset($_GET[“submit”]) ) { $opt = $_GET[“opt”]; echo “Your choice is $opt; } Apple Orange ?>

Passing Array from Form

Getting values <? $rows = $_POST[“row”]; echo $row[0]; echo $rows[5]; foreach($rows as $r) { echo $r.” ”; } ?>

File Upload

File Upload Form

To be remembered A file upload form must content an encoding type. i.e

Restricting upload size PHP also requires that a hidden field be included before the file upload field. This should be called MAX_FILE_SIZE and should have a value representing the maximum size in bytes of the file that you are willing to accept.

Inside file upload When a file is successfully uploaded, it is given a unique name and stored in a temporary directory (/tmp on UNIX systems). The full path to this file becomes available to you in a global variable

Handling Uploaded Files PHP stores all the uploaded file information in the $_FILES autoglobal array. $_FILES['userfile']['name'] $_FILES['userfile']['type'] $_FILES['userfile']['size'] $_FILES['userfile']['tmp_name'] $_FILES['userfile']['error']

$_FILES['userfile']['name'] The original name of the file on the client machine.

$_FILES['userfile']['type'] The mime type of the file, if the browser provided this information. An example would be "image/gif"

$_FILES['userfile']['size'] The size, in bytes, of the uploaded file i.e $totsize = $_FILES[‘userfile’][‘size’]; echo ‘Total uplodaed file size’.$totsize;

$_FILES['userfile']['tmp_name'] The temporary filename of the file in which the uploaded file was stored on the server.

$_FILES['userfile']['error'] The error code associated with this file upload. ['error'] was added from PHP

Upload Example

A upload form Choose a file to upload:

upload.php <?php If( isset ($_POST[‘submit’) ) { $uploadFile = $_FILES['userfile']['name']; if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadFile)) { print "File is valid, and was successfully uploaded. "; print “File type is : “. $_FILES[‘userfile’][‘type’]; } else { print "Possible file upload probs! Here's some debugging info:\n"; print_r($_FILES); } ?>

Some caution Always check file type after uploading the files. Always check the extension of the file. Always use MAX_FILE_SIZE restricting the file upload size

Session management

Session Management Session management is a mechanism to maintain state about a series of requests from the same user across some period of time. for example, to store each user items while they are shopping a site.

separate session? Since TCP/IP has its own session why we need a seprate session handling?

Because.. HTTP is a stateless protocol. It means in every transition the server immediately disconnect the connection. It present a problem when it comes to maintaining information about users visiting a Web site.

user session, how it works? There must be unique identifier number for each user store in storage device. When the user return back they must have this number (session id) to identify to the server. So server can retrieved user information store in the storage device.

Session in client The session variables can be stored in client side using Cookie

Session info can be stored in Cookies Hidden fields URL Web server process memory Files Database

Starting a Session A PHP session is started explicitly by session_start() session_start(); print($counter); $counter++; session_register("counter");

Inside session_start(..) PHP checks whether a valid session ID exist. If there is no session ID, PHP creates a new ID. If a valid ID exists, the frozen variables of that session are reactivated and introduced back to the global namespace. In next visit, Checks whether session is generated or not. If session id found then update the session timeout time.

session_start(..) Put session_start(..) at top of every php script so that the page will remain the part of the each session.

Registering a session variable Registering a session variable is done through the session_register() command All variables you want to preserve across page requests must be registered to the session library with the session_register() function

example session_start(); print($counter); $counter++; session_register("counter"); $bar = "This is a string"; $foo = "bar"; session_register($foo);

Ending a Session You can force a session end with the command session_destroy().

the $_SESSION superglobal the $_SESSION superglobal User $_SESSION to access the registered variables.