Cookie small piece of data होती है जो कि client / user browser पर store की जाती है। जब भी कोई user request send की जाती है तो cookie request के साथ embed होती है। Cookies को server द्वारा create , access या send किया जा सकता है।


हालाँकि client browser पर Cookies JavaScript के through भी create / access किया जा सकता है। Browser में set की गयी cookies को देखने के लिए आप Inspect Element (Press F12) करके  Storage: section में Cookies menu में site listing में देख सकते हैं।

PHP Cookies Image


Client Browser में Cookie store करने का main reason end user को track करना होता है। जिससे उस information को future में use किया जा सके। Cookie का सबसे अच्छा example ' Remember Password ' Mechanism है जिसमे किसी user के username , password cookie के रूप में browser में store कर दिया जाता है , और जब हम उस login पेज दुबारा visit करते हैं तो हमें username , password fields fill मिलते हैं।

PHP Set Cookies

PHP में Cookie set करने के लिए predefined function setcookie() use करते हैं जिसमे required parameter , key name और value होते हैं। हालाँकि आप optional parameter के रूप में time भी set कर सकते है जीतनी देर तक cookie store करना चाहते हैं , दिया गया time expire होने पर cookie data भी expire हो जाती है।

PHP setcookie() function

setcookie() function 7 parameter accept करता है , जिनके बारे में नीचे define किया गया है।

setcookie(
    string $name,
    $value = "",
    $expires_or_options = unix timestamp,
    $path = "",
    $domain = "",
    $secure = false,
    $httponly = false
)
  1. string $name | required cookie name , जिस name से cookie data store करना है। इसी name से आप set की गयी value को access करेंगे।

  2. $value | optional वो value जो हमें cookie में set / update करनी है , अगर cookie को delete करना है तो आप empty string pass कर सकते हैं।

  3. $expires_or_options | optional यह expired time है जितने time तक cookie data accessible रहेगा। यहाँ हमें UNIX timestamp value provide करनी होती है। by default 0 set रहता है जिसका मतलब कि जब तक browser open है cookie को access कर सकते हैं। browser close हो जाने के बाद आप उस cookie को access नहीं कर पाएंगे।

  4. $path | optional वो path / URL जिस पर आपको cookie data access करना है। मतलब हम किसी particular URL के लिए भी cookie set कर सकते हैं। फिर cookie data सिर्फ उसी URL पर accessible होगा। अगर आप slash ( / ) set करते हैं तो cookie data प्रत्येक URL के लिए accessible होगा है।

  5. $domain | optional जिस domain के लिए cookie set करनी है , by default current domain ही set होता है। और अगर आप cookie data को सभी subdomain के लिए accessible रखना चाहते हैं तो .example.com करके set करना पड़ेगा।

  6. bool $secure | optional cookie data http या https से access होगा , by default false होता है मतलब data दोनों type से access होगा।

  7. bool $httponly | optional मतलब cookie को सिर्फ http request से access किया जा सकता या नहीं। by default false होता है जिसका मतलब है कि cookie data हर तरह की request के लिए accessible रहेगा। अगर true set है तो फिर data की script request like : JS AJAX request के लिए accessible नहीं होगा।

PHP cookie Example

CopyFullscreenClose Fullscreen
<?php
  // 60 : 1 minute
  // 60 * 60 : 1 hour
  // 60 * 60 * 24 : 24 hours (1 day)
  // 60 * 60 * 24 * 365 : 1 year
  setcookie("key1", "Some Value");
  setcookie("key2", "other Value", time()+3600);  /* expire in 1 hour */
?>

PHP Accessing Cookies

PHP में Cookies को access करने के लिए simply $_COOKIE Super Global Variables का use किया जाता है। हालाँकि किसी particular key check करने के लिए isset() function use कर सकते हैं।

File : test_cookie.php

CopyFullscreenClose Fullscreen
<?php
    setcookie('name', 'Rahul Kumar Rajput');
if(isset($_COOKIE['name']))
echo $_COOKIE['name']; ?>
Output
Rahul Kumar Rajput

PHP Deleting Cookies

PHP में Cookies delete करने के लिए setcookie() function को name argument के साथ expired time set कर दिया जाता है ।

File : test_cookie2.php

Copy Fullscreen Close Fullscreen
<?php
    /* expire 60 seconds ago */
    setcookie('name', '', time()-60); 
?>

ऊपर दिए गए example में आप देख सकते हैं कि किस तरह से PHP में Cookies को delete किया जाता है।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers