PHP - $_POST and $_SESSION

10,675

Solution 1

In a nutshell, $_POST is a special array that stores data received from an HTTP POST request to a particular webpage. When the script loads, the raw HTTP POST data string is parsed and added to the $_POST array, so that it's easier for developers to use for common tasks, like handling HTML form submissions.

Example:

Raw HTTP data string format:
    key1=2&key2=3

$_POST array data format:
    $_POST = array('key1' => '2', 'key2' => '3');

$_SESSION data is not dependent on a particular page or HTTP request; its data is persisted across pages and is typically used for things like keeping track of account data while a user is logged-in. $_SESSION data is often stored in files on the server (or in a distributed storage mechanism like Redis) until it is either manually cleared (e.g., session_destroy()), or until PHP's garbage collection runs and destroys it.

Solution 2

Sample usages

$_POST

<?php
    // Access the username field with $_POST
    $username = $_POST['username'];

    // Output the username value
    echo $username;

    // If GET uncomment this
    // $username = $_GET['username'];
    // echo $username;

    // Or you can use $_REQUEST if you're in doubt about $_POST or $_GET
    // $username = $_REQUEST['username'];
    // echo $username;
?>

<form action="/" method="post"> <!-- You can change this as POST or GET -->
    <input type="text" name="username" />
    <input type="submit" value="Submit" />
</form>

By using $_POST, the address will be

http://domain.com/login

By using $_GET,

http://domain.com/login?username=somevalue

NOTE: $_GET displays the submitted value while $_POST don't

$_SESSION

<?php
    // You should call this first
    session_start();

    // Initialize the session value
    $_SESSION['mysession'] = 'hello_world';

    // Output the session value
    echo $_SESSION['mysession'];
?>
Share:
10,675
Grace Michelle
Author by

Grace Michelle

Updated on June 04, 2022

Comments

  • Grace Michelle
    Grace Michelle almost 2 years

    What's the differences between $_POST and $_SESSION? When should I use each of them? I've been searching on internet, but I still don't understand. Please give simple explanation and give an example. Thanks

    Maybe, this link can help you to explain the difference

    • Mike
      Mike almost 8 years
      You've been searching on the internet? What have you found so far? The fact that you are confusing these makes me think you haven't actually searched for them at all. $_POST (or $_GET) are sent by the user to your script. $_SESSION is used to have your script remember different values between requests and is populated using the script itself, not directly by the user. I don't know what else you want to know.
    • Grace Michelle
      Grace Michelle almost 8 years
      @Mike I've been searching for that! How about the $_POST? And give an example
    • Mike
      Mike almost 8 years
    • Mike
      Mike almost 8 years
      Give an example of what? What are you trying to do?
    • Julie Pelletier
      Julie Pelletier almost 8 years
      @GraceMichelle: He actually answered you very well. You should keep looking at tutorials.
  • Grace Michelle
    Grace Michelle almost 8 years
    So, if I want user to login to something, I better use $_SESSION?
  • Mike
    Mike almost 8 years
    @GraceMichelle You would need to use both. $_POST to get the username and password from the user and do your validation. $_SESSION to remember that the user is already logged in so that when they go to another page they don't have to keep re-entering their username and password.
  • badandyomega
    badandyomega almost 8 years
    If you want to retain information about their log-in and have access to it after the script terminates, yes. If you are handling the form submission data with their username and password, you'd use $_POST.
  • gannagainz
    gannagainz almost 3 years
    Why is it that if a form's method is "post", I can access the form's data using $_GET[] and using $_POST[]?