QUIZGUM

Coding Class

Quizgum : COOKIE

Cookie

What is a Cookie

A cookie means that any information related to the connection is stored on the client computer when the user logs in and accesses the web server.
When a user logs in to a site, the cookie is stored on the user's computer to check whether the user has the cookie. Determine whether or not.
The total cookie size is only 4kb and you need to use a database to get more information.

Generating Cookies

The cookie creation and deletion function is setcookie().
Since you define the cookie to be sent in the header, you must declare it first.
setcookie("cookie name", "cookie value", timeout, "path", "host", "enable security")
Set the path to / to use cookies from the entire domain, or to / apm / to set the directory Applies to and subdirectories.
In Host, enter the host name to which the cookie applies.
If you set the https protocol to true, the cookie is created only for secure connection.
The ob_start() function allows you to use a function called output buffering.
When used at the beginning of a page, the output of the output statement is processed after processing all the contents of the page.
The following is an example of using ob_start() without first declaring a cookie.

make folder cookie in htdocs

php image

/htdocs/cookie/makeCookie.php

<?php
    ob_start();
    echo "Create cookie-> cookie name [cook_test], cookie value [apm] <br />";
    echo "===============================================<br />";
    echo "Output buffering ob_start() is used, so the cookie is created even if an echo statement appears first. <br />";
    setcookie("cook_test","apm");
    echo "you make cookie cook_test";
?>

atom

php image

result

php image

If you use ob_start() function, setcookie doesn't have to be first, but if you don't want to use it, it should be first. Just look below.

<?php
    setcookie("second","001");
?>

Let's study with the following example.
So let's look at cookies in two examples.
The following example creates two cookies.
One is for 30 seconds and one is for 15 seconds.
Save the source below as exam1.php in htdocs/cookie/

/htdocs/cookie/exam1.php

<?php
    $cook1 = setcookie("cookie1","apm",time()+30);
    $cook2 = setcookie("cookie2","che",time()+15);

    echo "Use the setcookie() function to create a cookie <br />";
    echo "<hr />";

    if($cook1 && $cook2){
        echo "Two cookies have been created. <br />";
        echo "1. name of cookie : <b>cookie1</b> | value of cookie : apm <br />";
        echo "2. name of cookie : <b>cookie2</b> | value of cookie : che <br />";
        echo "Cookies last for 30/15 seconds.<br />";
    }else{
        echo "Cookie not created<br />";
    }
?>

atom

php image

result

php image

The following example is from the above source to see how long a cookie exists.
Save the source below as exam2.php.

/htdocs/cookie/exam2.php

<?php
    echo "output cookie <br />";
    echo "1. cookie1 is {$_COOKIE['cookie1']} <br />";
    echo "2. cookie1 is {$_COOKIE['cookie2']} <br />";
    echo " cookie1 lasts 30 seconds. <br />";
    echo " cookie2 lasts 15 seconds. <br />";
?>

Launch the first example,
then launch the second example, refresh after 15 seconds, and refresh after 30 seconds

atom

php image

result(first)

php image

result(15 seconds later)

php image

result(30 seconds later)

php image

How To Delete Cookie

The second value is not used.

/htdocs/cookie/makeTestCookie.php

<?php
    setcookie("testCookie","itistest.",time()+86400);
    echo "make testCookie";
?>

atom

php image

86400 is one day.(60seconds * 60 minutes * 24 hours)
1day is 86400 seconds

This example deletes testCookie.

/htdocs/cookie/delTestCookie.php

<?php
    ob_start();
    echo "the value of testCookie is ".$_COOKIE['testCookie']."<br>";
    setcookie("testCookie");
    echo "delete testCookie";
?>

atom

php image

This example checks the value of testCookie.

/htdocs/cookie/confirmTestCookie.php

<?php
    echo "the value of testCookie is ".$_COOKIE['testCookie'];
?>

atom

php image

Let's test it.
testCookie lasts for one day.
delTestCookie.php deletes testCookie.
confirmTestCookie.php confirms the value of testCookie.

test

One Step : run makeTestCookie.php

result - makeTestCookie.php

php image

Two Step : run delTestCookie.php

result - delTestCookie.php

php image

Three Step : run confirmTestCookie.ph

result - confirmTestCookie.php

php image

deleted..