Tag Archives: php session tutorial
PHP Operators
Arithmetic Operators
| Operator | Description | Example | Result |
|---|---|---|---|
| + | Addition | x=2 x+2 |
4 |
| - | Subtraction | x=2 5-x |
3 |
| * | Multiplication | x=4 x*5 |
20 |
| / | Division | 15/5 5/2 |
3 2.5 |
| % | Modulus (division remainder) | 5%2 10%8 10%2 |
1 2 0 |
| ++ | Increment | x=5 x++ |
x=6 |
| – | Decrement | x=5 x– |
x=4 |
Assignment Operators
| Operator | Example | Is The Same As |
|---|---|---|
| = | x=y | x=y |
| += | x+=y | x=x+y |
| -= | x-=y | x=x-y |
| *= | x*=y | x=x*y |
| /= | x/=y | x=x/y |
| .= | x.=y | x=x.y |
| %= | x%=y | x=x%y |
Comparison Operators
| Operator | Description | Example |
|---|---|---|
| == | is equal to | 5==8 returns false |
| != | is not equal | 5!=8 returns true |
| <> | is not equal | 5<>8 returns true |
| > | is greater than | 5>8 returns false |
| < | is less than | 5<8 returns true |
| >= | is greater than or equal to | 5>=8 returns false |
| <= | is less than or equal to | 5<=8 returns true |
Logical Operators
| Operator | Description | Example |
|---|---|---|
| && | and | x=6 y=3 (x < 10 && y > 1) returns true |
| || | or | x=6 y=3 (x==5 || y==5) returns false |
| ! | not | x=6 y=3 !(x==y) returns true |
Operators are used to operate on values. This section lists the different operators used in PHP.
AJAX RESPONSE XML
The most important advantage of XML is that it’s the most easily readable format for other humans. A secondary advantage is that XML has been around for quite a while and that many developers are already accustomed to it.
responseText returns the HTTP response as a string.
responseXML returns the response as XML.
The ResponseXML property returns an XML document object, which can be examined and parsed using the DOM.
A web page can fetch information from a database with AJAX technology. The selected data from the database can be converted to an XML document, and then the DOM can be used to extract the values to be displayed.
The following example might look equal to the “PHP AJAX and MySQL” example in the previous chapter. However, there is a big difference: this time we get the data from the PHP page as XML, with the responseXML function. Receiving the response as an XML document allows us to update this page several places, instead of just receiving an HTML output, and displaying it.
PHP Include
The include() or require() function allows to insert the content of one PHP file into another PHP file before the server executes it. These two functions are used to create functions, headers, footers, or elements that will be reused on multiple pages. include() and require() functions are quite identical, except they handle errors in different ways. include() generates a warning, but the script will continue execution, while require() generates a fatal error, and the script will stop. Server side includes save a lot of time. You can create a standard header, footer, or menu file for all your web pages. When the header needs to be updated, you can just update the include file. And when you add a new page to your site, you don’t need to update the links on all your web pages. Just change the menu.
PHP Sessions
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.
PHP Session Variables
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.
Storing a Session Variable
The correct way to store and retrieve session variables is to use the PHP $_SESSION variable:
<?php
session_start();
// store session data
$_SESSION['views']=1;
?>
<html>
<body>
<?php
//retrieve session data
echo “Pageviews=”. $_SESSION['views'];
?>
</body>
</html>
Output:
Pageviews=1
In the example below, we create a simple page-views counter. The isset() function checks if the “views” variable has already been set. If “views” has been set, we can increment our counter. If “views” doesn’t exist, we create a “views” variable, and set it to 1:
<?php
session_start();
if(isset($_SESSION['views']))
$_SESSION['views']=$_SESSION['views']+1;
else
$_SESSION['views']=1;
echo “Views=”. $_SESSION['views'];
?>







