Tuesday, August 10, 2010

Sessions in PHP

Sessions in PHP


• Hmm....so what are sessions actually???? 

• A session is a semi-permanent interactive information interchange, also known as a dialogue, a conversation or a meeting, between two or more communicating devices, or between a computer and user.

• A session is set up or established at a certain point in time, and torn down at a later point in time.

• A session is typically, but not always, stateful, meaning that at least one of the communicating parts needs to save information about the session history in order to be able to communicate, as opposed to stateless communication, where the communication consists of independent requests with responses and HTTP communication is stateless.

• why use them???

• As a website becomes more sophisticated, so must the code that backs it. When you get to a stage where your website need to pass along user data from one page to another, it might be time to start thinking about using sessions.

• A normal HTML website will not pass data from one page to another. In other words, all information is forgotten when a new page is loaded. This makes it quite a problem for tasks like a shopping cart, which requires data(the user's selected product) to be remembered from one page to the next.

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

• It is important to ponder if the sessions' temporary storage is applicable to your website. If you require a more permanent storage you will need to find another solution, like a MySQL database.

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

• Note: If you are not experienced with session programming it is not recommended that you use sessions on a website that requires high-security, as there are security holes that take some advanced techniques to plug.

Monday, August 9, 2010

Cookies in PHP

 Cookies:


• Before discussing cookies in php, the question is what actually cookies are???? 

• Hmm... so a computer cookie is a small text file which contains a unique ID tag, placed on your computer by the website. The website saves a complimentary file with a matching ID tag. In this file various information can be stored, from pages visited on the site, to information voluntarily given to the site. When you revisit the site days or weeks later, the site can recognize you by matching the cookie on your computer with the counterpart in its database.

• U can also say that, a cookie consists of one or more name-value pairs containing bits of information, which may be encrypted for information privacy and data security purposes. The cookie is sent as an HTTP header by a web server to a web browser and then sent back unchanged by the browser each time it accesses that server.

• There are two types of computer cookies: temporary and permanent. Temporary cookies, also called session cookies, are stored temporarily in your browser's memory and are deleted as soon as you end the session by closing the browser. Permanent cookies, also called persistent cookies, are stored permanently on your computer's hard drive and, if deleted, will be recreated the next time you visit the sites that placed them there.

Thursday, August 5, 2010

Conditional Statements and PHP Functions

Conditional Statements:


• Like in C, in php too v have 4 types of conditional statements:

if statement - use this statement to execute some code only if a specified condition is true.

if...else statement - use this statement to execute some code if a condition is true and another code if the condition is false.

if...elseif....else statement - use this statement to select one of several blocks of code to be executed.

switch statement - use this statement to select one of many blocks of code to be executed.

• These statements r used with same syntax as used in C.

Wednesday, August 4, 2010

Concatenation Operator and Form Handling

The Concatenation Operator:


• The concatenation operator (.) is used to put two strings together like (+) is used in java 4 concatenation.

• Consider the code:

<?php

$txt1=”Hello World!”;

$txt2=”What a nice day!”;

echo $txt1.” ”.$txt2;

?>

Display:

Hello World! What a nice day!

• If we look at the code above you see that we used the concatenation operator two times. This is because we had to insert a third string (a space character), to separate the two strings.



PHP Form Handling:

• U all r familiar with form tag used in html. Let us see an html form having two input fields and one submit button.

<html>

<body>

<form action="welcome.php" method="post">

Name: <input type="text" name="fname" >

Age: <input type="text" name="age" >

<input type="submit" value=”SUBMIT”>

</form>

</body>

</html>

Monday, August 2, 2010

PHP Comment and PHP echo

PHP Comment:


• Syntax for single line comment-

   o <?php

   o #This is an example comment

   o //This is another example comment

   o echo "Sentence 1."; //This is our first statement

   o echo "<br />"; #This is a line break

   o echo "Sentence 2."; // This is our second statement

   o ?>

• U will notice that both types of commenting styles i.e. ‘//’ and ‘#’ r used on a single line .

• But it is very tedious task when needs to comment multiple lines. So there is another way of commenting. This style is used for multiple lines:

  o <?php

  o /* This is an easier

  o way to write comments

  o on multiple lines */

  o ?>

• So ‘/* and */’ is used for multiple line comments.


PHP-echo


• As u know php command echo is used 4 outputting text 2 a web browser but how to use echo command 4 displaying values stored in variables????

• Observe the following code:

<?php

$myString=”Hello!”;

echo $myString;

echo “<h5>I luv using PHP!!</h5>”;

?>

Display:

Hello!

I luv using PHP!!