Object for cookie in PHP

I am starting to study in PHP and I have problems with the application: I need to put information about the object in PHP for the cookie, and then get the cookie for the repeated object on another page. Does anyone have a solution to this problem?

The information I want to store in a cookie is just some information, preferably Customer, like background color, window size.

<?php  
class Client {    

private $id;
private $pSize;    
private $color;

function __construct($id) {
    $this->id = $id;
}

public function getPSize() {
    return $this->pSize;
}

public function setPSize($pSize) {
    $this->pSize = $pSize;
}

public function getColor() {
    return $this->color;
}

public function setColor($color) {
    $this->color = $color;
}
}
?> 

On the index.php page, I have:

<?php      
  include_once 'Client.class.php';
    //Test Preference Client
    $client = new Client(1);
    $client->setColor("#000000");        
    $client->setPSize(200);       

    //using Serialize to put to Cookie
    $StringClient = serialize($client);

    //Store to Cookie
    $_COOKIE['PreferenceClient'] = $StringClient;

? >

On another page, I get inrofmation:

 if(isset($_COOKIE['PreferenceClient'])){
       // Unsing Unserialize to Object
        $objClient = unserialize($_COOKIE['PreferenceClient']);

        //Test data:
        echo $objClient->getColor();            
        //Continue with Performing changes to the client if the information exists...
    }

I solved the problem. Thanks to everyone who helped. before I tried to get cookie information without serialization Guys, this is my first post, I apologize if I did something wrong. Should I do something for you?

+4
4

(, cookie) , unserialize.

setcookie ($name, serialize($object));   // set object

$object = unserialize($_COOKIE[$name]);   // get object

, . PHP

json serialization stdClass, .

setcookie ($name, json_encode($object));   // set object stdClass

$object = json_decode($_COOKIE[$name]);   // get object stdClass

session . serialize, unserialize. __sleep, __wakeup .

setcookie, $_ COOKIE, serialize, .

+4

: .

, , , , - . cookie , , .

:

Alma Do, cookie, /

public function __sleep() {
    return array('server', 'username', 'password', 'db');
}

cookie , / .

, XY, , .

+2

, . Cookie. Cookies , ​​ . PHP .

http://www.php.net/manual/de/book.session.php

In your session, you can serialize some data if you want, or save only an array or value.

session_start(); // on every page

$_SESSION['test'] = "123123";

echo $_SESSION['test'];
+1
source

to send a serialized object you must use the specified thing, for example time(), to bypass SPAMand managetimeOut!

session_start();  

setcookie('myobject',serialize("Myvalue"),(time()+(3600*24*30)));

be sure to save it in the session:

     unset($_SESSION['myobject']);

Save your object

     $_SESSION['myobject'] = unserialize($_COOKIE['myobject']);

Restore Obecjt:

$mySeriaLizedObject = $_SESSION['myobject'];
0
source

All Articles