Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 PHP and the Web: Session : 4

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

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

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

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

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

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

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

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

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

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

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

13 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”;

14 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

15 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”; }

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

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

18 Passing Array from Form

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

20 File Upload

21 File Upload Form

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

23 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.

24 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

25 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']

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

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

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

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

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

31 Upload Example

32 A upload form Choose a file to upload:

33 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); } ?>

34 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

35 Session management

36 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.

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

38 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.

39 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.

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

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

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

43 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.

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

45 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

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

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

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


Download ppt "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."

Similar presentations


Ads by Google