Presentation is loading. Please wait.

Presentation is loading. Please wait.

Open Source Programming

Similar presentations


Presentation on theme: "Open Source Programming"— Presentation transcript:

1 Open Source Programming
SESSION A normal HTML website will not pass data from one page to another. (OR) In other words, all information is forgotten when a new page is loaded. This makes a quite problem for tasks like a shopping cart, which requires data (the user's selected product) to be remembered from the time of selection to billing. (i.e. one page to the next page). Cookie used for such requirements, however, limitations on cookie size and the number of cookies allowed, and various inconveniences surrounding their implementation, prompted to introduce another solution called as session handling. A PHP session solves this problem by allowing you to store user information on the server for later use (i.e. username, shopping cart items, etc). However, this session information is temporary and is usually deleted very quickly after the user has left the website that uses sessions. Open Source Programming

2 Open Source Programming
SESSION Mechanism There are two things that the session mechanism must hang onto: the session ID itself and any associated variable bindings. The session ID is either stored as a cookie on the browser's machine, or it is incorporated into the GET/POST arguments submitted with page requests. The contents of session variables are stored in special files on the server, one file per session ID: Doing this kind of storage requires the session code to serialize the data by turning it into a linear sequence of bytes that can be written to a file and read back to recreate the data It's possible to configure PHP to store the contents of session variables in a server-side database, rather than in files Open Source Programming

3 Open Source Programming
Storing the Sessions The session variables are stored on the web server The Path is /var/lib/php/session The file name is the session ID Eg. sess_076opqrstu56kldr670ndft0op Open Source Programming

4 Open Source Programming
Working of a Session Sessions work by creating a unique identification(UID) number for each visitor and storing variables based on this ID. This helps to prevent two users' data from getting confused with one another when visiting the same webpage. Session_start() – it is required, for every session’s program. $_SESSION[‘session_name’] – can access the created sessions through this super global. isset($_SESSION[‘session_name’]) – used to check the availability of a session. unset($_SESSION[‘session_name’]) – deletes the session Session_destroy() – deletes all sessions. Open Source Programming

5 Open Source Programming
Session Functions session_start — Initialize session data. session_destroy — Destroys all data registered to a session session_unset — Free all session variables session_name — Get and/or set the current session name session_register — Register one or more global variables with the current session. session_destroy — Destroys all data registered to a session. session_id — Get and/or set the current session id. Session_regenerate_id — Update the current session id with a newly generated one session_is_registered — Find out whether a global variable is registered in a session Open Source Programming

6 Open Source Programming
Start a Session Before you can begin storing user information in your PHP session, you must first start the session. When you start a session, it must be at the very beginning of your code, before any HTML or text is sent. bool session_start (void) Sample program to Start Session: Session_start.php <?php session_start(); // starts PHP session! echo “The session was started”; ?> This tiny piece of code will register the user's session with the server, allow you to start saving user information and assign a UID (unique identification number) for that user's session. Open Source Programming

7 Open Source Programming
Create a Session When you want to store user data in a session use the $_SESSION (super global). This is used for both store and retrieve session data. $_SESSION[‘session_name’] Sample program to Create Session: Session_Create.php <?php session_start(); // starts PHP session! $_SESSION[‘view’] = 1; // creates a new session with name “view” ?> Open Source Programming

8 Open Source Programming
Access Session Can access the existing sessions through the following super global: $_SESSION[‘session_name’] – global array $_HTTP_SESSION_VARS[‘session_name’] – environment variables Sample program to Access a Session: Session_Access.php <?php session_start(); // starts PHP session! if (isset($_SESSION[‘view’]) // checks the availability $_SESSION[‘view’] += 1; // access the existing ?> Open Source Programming

9 Open Source Programming
Delete Session Imagine that you were running an online business and a user used your website to buy your goods. The user has just completed a transaction on your website and you now want to remove everything from their shopping cart. Can delete the existing session by the function “unset”. The sample program shows it: Sample program to Delete Session: Session_Delete.php <?php session_start(); // starts PHP session! if (isset($_SESSION[‘view’]) // checks the existing { unset($_SESSION[‘view’]); // deletes the existing echo “The session ‘view’ was deleted”; } else echo “no session with name ‘view’”; ?> Open Source Programming

10 Open Source Programming
Delete All Sessions Session_destroy() destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session. Variables associated with the session, or unset the session cookie. bool session_destroy (void) Sample program to Delete all Session: Sessions_Delete.php <?php session_start(); // starts PHP session ……… //codings session_destroy(); // deletes all sessions ?> Note: destroy will reset your session, so don't call that function unless you are entirely comfortable losing all your stored session data! Open Source Programming

11 Open Source Programming
Session Name session_name() returns the name of the current session. bool session_name (string $name) If name is given, session_name() will update the session name and return the old session name. The session name is reset to the default value stored in session.name at request startup time. Thus, you need to call session_name() for every request Sample program for Session Name: Session_Name.php <?php // set the session name to WebsiteID  $previous_name = session_name("WebsiteID"); echo "The previous session name was $previous_name<br />"; ?> Open Source Programming

12 Open Source Programming
Session id session_id() is used to get or set the session id for the current session. The constant SID can also be used to retrieve the current name and session id as a string suitable for adding to URLs. bool session_id (string $id) The session id can be updated by the function bool session_regenerate_id ([bool $delete_old_session=false]) Sample program for Session Name: Session_Name.php <?php session_start(); echo "session id is ".session_id()."<br>"; echo "barney is ",$_SESSION['barney']++."<br>"; ?> Open Source Programming

13 Session Functions session_ cache_ expire session_ cache_ limiter
session_ commit session_ decode session_ destroy session_ encode session_ get_ cookie_ params session_ id session_ is_ registered session_ module_ name session_ name session_ regenerate_ id session_ register_ shutdown session_ register session_ save_ path session_ set_ cookie_ params session_ set_ save_ handler session_ start session_ status session_ unregister session_ unset session_ write_ close


Download ppt "Open Source Programming"

Similar presentations


Ads by Google