• Skip to main content
  • Skip to primary sidebar

Technical Notes Of
Ehi Kioya

Technical Notes Of Ehi Kioya

  • Forums
  • About
  • Contact
MENUMENU
  • Blog Home
  • AWS, Azure, Cloud
  • Backend (Server-Side)
  • Frontend (Client-Side)
  • SharePoint
  • Tools & Resources
    • CM/IN Ruler
    • URL Decoder
    • Text Hasher
    • Word Count
    • IP Lookup
  • Linux & Servers
  • Zero Code Tech
  • WordPress
  • Musings
  • More
    Categories
    • Cloud
    • Server-Side
    • Front-End
    • SharePoint
    • Tools
    • Linux
    • Zero Code
    • WordPress
    • Musings

Understanding Cookies In PHP

Tagged: Cookies, PHP

  • This topic has 0 replies, 1 voice, and was last updated 1 year, 1 month ago by Chinomnso.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • December 2, 2019 at 11:41 am #81424
    Participant
    @chinomnso

    A cookie is a small piece of data sent by a website from a web server to a web browser to be stored there as a way to remember pieces of information about a user’s browsing activity. In other words, a cookie is a mechanism for storing data in a browser and using that data to track or identify returning users.

    When can you use cookies?

    They can be used when you want to track what pages a user has visited on your site, or to enhance user experience. As an example of tracking users, consider this: You’re adding a voting feature to a page on your site. Let us assume users are not allowed to create accounts on your site, so you have to find a way to track users who have previously voted and prevent them from voting again.

    Once a user votes, you have to create a cookie and save it in the user’s browser. When the user returns to the site, you can disable the voting section on the webpage, or redirect them to another page if the voting page was created exclusively for that purpose.

    More about cookies

    Generally, a cookie binds small pieces of data to a specific user. A cookie contains at least, two bits of data: the name of the cookie and a value. But a cookie may also be made up of much more information like its scope, validity, and even more.

    Apart from cookies that are stored in your browser by the website you are directly visiting, other cookies can be stored in your browser by other sites linked to the one you’re visiting. These are third-party cookies. Perhaps, the most annoying application of third-party cookies is their usage in advertising. You search for home audio systems on an e-commerce site, and for the next two weeks, every single site you visit shows you home audio ads. Third-party cookies are to blame for all of this.

    How to implement cookies in PHP

    PHP supports the implementation of cookies out-of-the-box. Therefore, you don’t need to install any extensions before you can start working with cookies. Cookies can be set using setcookie() or setrawcookie() function. Because cookies are part of the HTTP header, setcookie() must be called before any output is sent to the browser.

    Here’s the basic syntax for implementing cookies in PHP:

    1
    setcookie ( string $name [, string $value = "" [, array $options = [] ]] ) : bool

    The setcookie() function has the following parameters:

    name
    The name of the cookie you’re setting.

    value
    The value assigned to the cookie. Because the value of the cookie is saved on a user’s computer, be sure that you’re not storing sensitive information with the cookie.

    expires
    This sets the validity of the cookie by assigning it an expiration date. This is a Unix timestamp. Therefore you’ll probably set it using the time() function and append the number of seconds till the expiration. Here’s an example:

    1
    $expires = time()+60*60*24*60;

    The code above gets the current time and adds it to the total number of seconds in 60 days and assigns the result of the calculation to a variable. This means the cookie would end in 60 days. If you set the expiration time to 0, the cookie expires at the end of the browsing session, or when the browser closes.

    To access cookies in PHP, $_COOKIE['cookiename'] is used. You may also be able to access the value of the cookie via $_REQUEST['cookiename'].

    Remember though, that a user can circumvent cookie-based restrictions by disabling cookies in their browser, or by clearing their browser cookies. So, there are certain features of your site that you certainly would not use cookies for.

    To learn more about the setcookie() function, check out the official documentation on php.net.

  • Author
    Posts
Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.
Log In

Primary Sidebar

FORUM   MEMBERSHIP

Log In
Register Lost Password

POPULAR   FORUM   TOPICS

  • How to find the title of a song without knowing the lyrics
  • The Art of Exploratory Data Analysis (Part 1)
  • Welcome Message
  • How To Change Or Remove The WordPress Login Error Message
  • Getting Started with SQL: A Beginners Guide to Databases
  • Replacing The Default SQLite Database With PostgreSQL In Django
  • How to Implement Local SEO On Your Business Website And Drive Traffic
  • Forums
  • About
  • Contact

© 2021   ·   Ehi Kioya   ·   All Rights Reserved
Privacy Policy