Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • PHP Session Vs Cookies

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 271
    Comment on it

    PHP Session is defined as a storage type which hold the values that are set to the $SESSION[], until we attempt to destroy the session.
    Whereas Cookies are the temporary and also have the small storage.
    For each data, the name, value, expiration period and etc will be send And,
    Cookies preserves data at client side until it reaches the expiration time or the browser get restarted.

    For working with session we have to follow the cycle that contain ->

    1-Starting session-> First we have to write a cecho $_SERVER[‘HTTP_COOKIE’]; // Output: platform=php
    echo getenv(‘HTTP_COOKIE’); // Output: platform=phpode to start a session. This function comes in the first line of the program by which no result should be sent to the browser before getting current session by this function.The code we use to start the session is

    session_start();  // starts the session 

    2-Creating session variable-> It is created by associating value to the global $_SESSION .

    $_SESSION["tagName"] = "PHP";

    3-Accessing session variable-> The session which are newly made are saved into temporary directory So we can access these session from any PHP page. We can create a new PHP page and write the following code into it

    $tag = $_SESSION["tagName"];
    echo "Welcome $tag ";

    4-Clearing session variable-> If we think that a session variable is no longer needed, then it can be destroyed by the use of session_destroy() If we want it destroy right immediately, then we should call unset().

    echo $_SESSION["tagName"];//session remains there until refresh or page redirect
    echo $_SESSION["tagName"];//session no more;

    Working with Cookies

    For working with cookies, we have to follow two steps

       1- Setting cookies-We can set the cookies by calling header() function and by setting essentials values for cookie header.

       header("Set-Cookie: platform=php; expires=SAT, 12-April-16 17:30:48 GMT; path=/;");

       This method should called infirst line of program it is because the cookie details should be send before creating the header. If any result will be send to the browser, then the header will automatically visible and created without cookie information.

    2-Accessing cookies
    We can access the cookies by using the code below

    echo $_SERVER[HTTP_COOKIE]; // Output: platform=php
    echo getenv(HTTP_COOKIE); // Output: platform=php


 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: