Pages

Thursday 21 June 2012

PHP SESSIONS


In This Post We Will Discuss PHP SESSIONS Briefly.This is most commonly used in websites where user input or registeration is required most common example we see is in our mailing websites like ymail,gmail or banking sites like ebanking.we can say seesions are everywhere.
Moreover sessions are time slots of access.
Mechanism is like user1=time1 similarly user2=time2.When we signout from any site and after that no other person could access your id without password.
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 we 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.
An alternative way to make data accessible across the various pages of an entire website is to use a PHP Session.
A session creates a file in a temporary directory on the server where registered session variables and their values are stored. This data will be available to all pages on the site during that visit.
The location of the temporary file is determined by a setting in the php.ini file calledsession.save_path. Bore using any session variable make sure you have setup this path.




Tuesday 19 June 2012

GET POST meathods in PHP

                      $_GET AND $_POST Methods   

In PHP, the predefined $_GET variable is used to collect values in a form with method="get".
In PHP, the predefined $_POST variable is used to collect values in a form with method="post".
This means both the methods solve the same purpose in PHP.
                                   
                                            $_GET VS $_POST

It's not a matter of security. The HTTP protocol defines GET type requests as being idempotent, while POST may have side effects. In plain English that means that GET is used for viewing something, without changing it, while POST is used for changing something. For example, a search page should use GET, while a form that changes your password should use POST.

Also, note that PHP confuses the concepts a bit. A POST request gets input from the querystring and through the request body. A GET request just gets input from the querystring. So a POST request is a superset of a GET request; You can use $_GET in a POST request, and it may even make sense to have a parameter with the same name in $_POST and $_GET, meaning different things. For example, let's say you have a form for editing an article. The article-id may be on the querystring (So, available through $_GET['id']), but let's say that we want to change the article-id. The new id may then be present in the request body (Eg. $_POST['id']). OK, perhaps not the best example, so, the difference between the two.

$_GET METHOD

The predefined $_GET variable is used to collect values in a form with method="get"

Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's address bar) and has limits on the amount of information to send.
Let us Know discuss a example using the get method.
Below is the simple html form which act as input for our PHP code to Work.
NOTE:-A simple HTML page is only way of Inputting data or for PHP script to work

<html>
<body>
<form action="<?php $_PHP_SELF ?>" method="GET">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
Know we write the PHP script working behind the scene:-
<?php
if( $_GET["name"] || $_GET["age"] )
{
echo "Welcome ". $_GET['name']. "<br />";
echo "You are ". $_GET['age']. " years old.";
exit();
}
?>
$_POST METHOD
The POST method transfers information via HTTP headers. The information is encoded as described in case of GET method and put into a header called QUERY_STRING.

The POST method does not have any restriction on data size to be sent.


The POST method can be used to send ASCII as well as binary data.


The data sent by POST method goes through HTTP header so security depends on HTTP protocol. By using Secure HTTP you can make sure that your information is secure.


The PHP provides $_POST associative array to access all the sent information using GET method.

NOW we will see a post example.
Below is HTML form for input purpose:-
<html>
<body>
<form action="<?php $_PHP_SELF ?>" method="POST">

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

<input type="submit" />
</form>
</body>
</html>

Now is the PHP Script ;-
<?php
if( $_POST["name"] || $_POST["age"] )
{
echo "Welcome ". $_POST['name']. "<br />";
echo "You are ". $_POST['age']. " years old.";
exit();
}
?>