Sessions

PHP Session Variables 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. When you are working with an application, you open it, do some changes and then you close it. This is much like a Session. The computer knows who you are. It knows when you start the application and when you end. But on the internet there is one problem: the web server does not know who you are and what you do because the HTTP address doesn't maintain state. A PHP session solves this problem by allowing you to store user information on the server for later use (i.e. username, shopping items, etc). However, session information is temporary and will be deleted after the user has left the website. If you need a permanent storage you may want to store the data in a database.

Sessions work by creating a unique id (UID) for each visitor and store variables based on this UID. The UID is either stored in a cookie or is propagated in the URL.

Starting a PHP Session

Before you can store user information in your PHP session, you must first start up the session. Note: The session_start() function must appear BEFORE the <html> tag:

<?php session_start();?>

<html>
<body>

</body>
</html>

The code above will register the user's session with the server, allow you to start saving user information, and assign a UID for that user's session.

Classes and class methods

A class is a template for a real world object to be used in a program. For example, a programmer can create a car class which emulates a car. This class can contain the properties of a car (color, model, year, etc.) and methods (functions) that specify what the car does (drive, reverse, stop, etc.).

Creating a class

To create a class, use the class keyword, followed by the class name, followed by an opening brace and a closing brace. All the specifics and logisitcs of the class go between the braces.

Syntax:

class className { }

Class functions are just like regular functions, except they are defined inside a class definition. Class functions are known as methods, and they can be used to either retrieve data or manipulate data. We will be referring to class functions as 'functions' or 'methods' interchangeably throughout this lesson.:

class MyClass {

... // List of methods

...

... // List of properties

...} E.g

class Book

{ var $title; var $numPages;

function setNumPages($numOfPages)

{ $numPages = $numOfPages; }


Понравилась статья? Добавь ее в закладку (CTRL+D) и не забудь поделиться с друзьями:  



double arrow
Сейчас читают про: