Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8203871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:42:30+00:00 2026-06-07T07:42:30+00:00

I just started with OOP programming in PHP and I have made a cookie

  • 0

I just started with OOP programming in PHP and I have made a cookie class.

With doing that i have got a few questions unanswered

  • is my class correct?

  • how do I use it properly in my page? ( lets think i want to see how many times the visitor visited my website before and output the result for the user )

I already tested it after loging in and using this code:

$cookie = new Cookie();
$cookie->store();
print_r($_COOKIE);

(I had a result thrown back but I don’t know if its the good result)
Bellow you can find my Cookie class.

<?php
class Cookie {
    /* cookie $id */ 
    private $id = false;

    /* cookie life $time */
    private $time = false;

    /* cookie $domain */
    private $domain = false;    

    /* cookie $path */
    private $path = false;

    /* cookie $secure (true is https only) */
    private $secure = false;


    public function __construct ($id, $time = 3600, $path = false, $domain = false, $secure = false) {
        $this->id = $id;
        $this->time = $time;                  
        $this->path = $path;
        $this->domain = $domain;      
        $this->secure = $secure;
    }

    public function store() {
        foreach ($this->parameters as $parameter => $validator) {
            setcookie($this->id . "[" . $parameter . "]", $validator->getValue(), time() + $this->time, $this->path, $this->domain, $this->secure, true);        
        }            
    }     

    public function restore() {
        if (isset($_COOKIE[$this->id])) {

            foreach ($_COOKIE[$this->id] as $parameter => $value) {
                $this->{$parameter} = $value;
            }
        }   
    }           

    public function destroy() {
        $this->time =  -1;
    }   
}
?>

I hope someone can give me a good example! thanks for the help in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T07:42:33+00:00Added an answer on June 7, 2026 at 7:42 am

    This code should do the most frequent tasks you’ll need to manipulate cookies.
    Don’t get confused by reading the getter and setter methods – they’re used to access the private variables defined in the class.
    Have in mind this class is used per cookie and you need to have a new instance for every new cookie you’ll operate over.
    Below the class I’ve added an example how to use the class.

    <?php
    /**
     * Cookie manager.
     */
    class Cookie
    {
        /**
         * Cookie name - the name of the cookie.
         * @var bool
         */
        private $name = false;
    
        /**
         * Cookie value
         * @var string
         */
        private $value = "";
    
        /**
         * Cookie life time
         * @var DateTime
         */
        private $time;
    
        /**
         * Cookie domain
         * @var bool
         */
        private $domain = false;
    
        /**
         * Cookie path
         * @var bool
         */
        private $path = false;
    
        /**
         * Cookie secure
         * @var bool
         */
        private $secure = false;
    
        /**
         * Constructor
         */
        public function __construct() { }
    
        /**
         * Create or Update cookie.
         */
        public function create() {
            return setcookie($this->name, $this->getValue(), $this->getTime(), $this->getPath(), $this->getDomain(), $this->getSecure(), true);
        }
    
        /**
         * Return a cookie
         * @return mixed
         */
        public function get(){
            return $_COOKIE[$this->getName()];
        }
    
        /**
         * Delete cookie.
         * @return bool
         */
        public function delete(){
            return setcookie($this->name, '', time() - 3600, $this->getPath(), $this->getDomain(), $this->getSecure(), true);
        }
    
    
        /**
         * @param $domain
         */
        public function setDomain($domain) {
            $this->domain = $domain;
        }
    
        /**
         * @return bool
         */
        public function getDomain() {
            return $this->domain;
        }
    
        /**
         * @param $id
         */
        public function setName($id) {
            $this->name = $id;
        }
    
        /**
         * @return bool
         */
        public function getName() {
            return $this->name;
        }
    
        /**
         * @param $path
         */
        public function setPath($path) {
            $this->path = $path;
        }
    
        /**
         * @return bool
         */
        public function getPath() {
            return $this->path;
        }
    
        /**
         * @param $secure
         */
        public function setSecure($secure) {
            $this->secure = $secure;
        }
    
        /**
         * @return bool
         */
        public function getSecure() {
            return $this->secure;
        }
    
        /**
         * @param $time
         */
        public function setTime($time) {
            // Create a date
            $date = new DateTime();
            // Modify it (+1hours; +1days; +20years; -2days etc)
            $date->modify($time);
            // Store the date in UNIX timestamp.
            $this->time = $date->getTimestamp();
        }
    
        /**
         * @return bool|int
         */
        public function getTime() {
            return $this->time;
        }
    
        /**
         * @param string $value
         */
        public function setValue($value) {
            $this->value = $value;
        }
    
        /**
         * @return string
         */
        public function getValue() {
            return $this->value;
        }
    }
    
    /**
     * Create a cookie with the name "myCookieName" and value "testing cookie value"
     */
    $cookie = new Cookie();
    // Set cookie name
    $cookie->setName('myCookieName');
    // Set cookie value
    $cookie->setValue("testing cookie value");
    // Set cookie expiration time
    $cookie->setTime("+1 hour");
    // Create the cookie
    $cookie->create();
    // Get the cookie value.
    print_r($cookie->get());
    // Delete the cookie.
    //$cookie->delete();
    
    ?>
    

    P.S. I’ve commented the $cookie->delete(); on purpose so that you can see the content of print_r($cookie->get()).

    Edit:
    Question: Where does the code go to see if the cookie is set?
    Answer:
    You should check what does $_COOKIE do in the php documentation.
    Basically the server sends headers to the client’s browser which stores the cookies on the client’s computer. When the client initializes a connection to the server it passes the cookies with the request.

    Question: Where goes the $cookie->delete();
    Answer:
    There isn’t a direct way to delete cookies. So in order to do that you need to create a cookie with the same name and expiration time which is in the past. When you do that the cookie is removed from the client’s browser.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just started practicing OOP in php through the book Concepts, Techniques and
I just started doing OOP, so I apologize in advance if there is a
Just started learning Groovy, got the PragProg book Programming Groovy and had a problem
I just started learning Objective-C and OOP ... so I have very stupid question
I have just started learning the concept of Object oriented programming and have put
I just started programming OOP and I'm running into a scope problem. In the
I'm just getting started with OOP PHP with PHP Object-Oriented Solutions by David Powers,
I have just started delving into the world of functional programming. A lot of
I just started with OOP on Javascript. I am new to programming world. Could
I have just started studying OOP and I am a little confused of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.