• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

PHP - Cookies

Gabriel Tibiano

New Member
Joined
Nov 21, 2009
Messages
420
Reaction score
4
Hey,

I'm looking for a way to store the pages that the User already visited on my website, then I can do something, such as "read" and "unread" of the forums.

I tried to create a system using cookies, but I could not imagine anything beyond of millions cookies (one for each page accessed) on the user's computer.

Could anyone help me to figure it out?
Is there any other way to do this?
 
Last edited:
The database storage or local javascript storage (but better database, 'cause of local javascript storage is only available for HTML5, and not all main browsers supports it). You can store it as JSON data.
 
Serialize data and store it as one cookie.

can you show me an example of it?

something like this?
PHP:
$cookie_data = 'value1|value2|value3';

setcookie('cookie_name', $cookie_data);
but how to get single value then?

---------------UPDATE-------------------

Ohh!! Found a better sample here, i'll try then
 
Last edited:
Hey folks,

I'll post below the code that I did, but i'm having troubles: the variable isn't being added to the cookie, why?
every time you insert a new value it resets the cookie for only one value

PHP:
<?php

$pages = array($pages, $number_to_serialize_in_pages);
setcookie('readpages', serialize($pages));

$serialized = $_COOKIE['readpages'];
$read_pages = unserialize($serialized);

assert(is_array($read_pages));
if (in_array($number_to_serialize_in_pages, $read_pages)) {
echo $on
}else{
echo $off
}

?>
 
When you set a cookie, they are available since the client loads the page again, not immidiately.
 
Hey folks,

I'll post below the code that I did, but i'm having troubles: the variable isn't being added to the cookie, why?
every time you insert a new value it resets the cookie for only one value

PHP:
<?php

$pages = array($pages, $number_to_serialize_in_pages);
setcookie('readpages', serialize($pages));

$serialized = $_COOKIE['readpages'];
$read_pages = unserialize($serialized);

assert(is_array($read_pages));
if (in_array($number_to_serialize_in_pages, $read_pages)) {
echo $on
}else{
echo $off
}

?>
setcookie('readpages', serialize($pages), time() + 315360000, '/');
 
When you set a cookie, they are available since the client loads the page again, not immidiately.
ye I know that, i'll put an header later..

setcookie('readpages', serialize($pages), time() + 315360000, '/');
isn't saving with the others.. it is resetting and saving over :X..

I want something that increase a page-value to the already existing cookie everytime when I acess new page:

$pages = 1, 15, 23, 107, 37
$new_page = 14

$pages+$new_page = 1 | 15 | 23 | 107 | 37 | 14
 
Last edited:
should I use explode() ? never used that, but heard something about it

- - - Updated - - -

bumpp :p
 
I took the liberty of creating a class for you, to ease your struggle.
http://pastebin.com/LENdbueK

PHP:
$post = 10;

// Check to see whether the specified post is unread.
if (OtLand\PageView::isUnread($post)) {
     OtLand\PageView::markAsRead($post);
}

// Or if you'd like to mark a post as unread
if (OtLand\PageView::isRead($post)) {
     OtLand\PageView::markAsUnread($post);
}

// You can also pass arrays of id's...
OtLand\PageView::markAsRead([1,2,3,4,5,6]); /* [1,2,3,4,5,6] is the same as doing array(1,2,3,4,5,6); if you have php 5.4 */
OtLand\PageView::markAsUnread([1,2,3,4,5,6]);

// If you would need to reset the user cookie, you may call the reset method.
OtLand\PageView::reset();

// And finally, if you would like to get all of the ids as an array...
var_dump(
     OtLand\PageView::getAll()
);
I hope that helps, if you would encounter any bugs with it, please let me know. I have not done any extensive testing as of right now.

PS. It does actually check whether an id already exists within the cookie before setting it, so you don't have to do that manually like in my example. I just wanted to let you know that the isRead and isUnread methods do exist, in case you ever need them.
 
Last edited:
Back
Top