Google Answers Logo
View Question
 
Q: Array cookie in PHP ( Answered 5 out of 5 stars,   3 Comments )
Question  
Subject: Array cookie in PHP
Category: Computers > Programming
Asked by: shantanuo-ga
List Price: $2.00
Posted: 21 Jul 2003 23:48 PDT
Expires: 20 Aug 2003 23:48 PDT
Question ID: 233626
Hi, 
I have written a feed reader for my personal use. 
The php code given below does work. 
But it works only with a single feed. 
How can I save multiple cookies (array?) and display a few more RSS feeds.
The example page can be found at...
http://shantanuoak.com/test1.php 

<?php
if ($delete==yes) {
setcookie ("FeedCookie5", $formfeedurl1,time()-3600);
}
if (isset($_COOKIE['FeedCookie5'])) { ?>
<script language="javascript" src="http://forever.p3k.org/rss/rss-box.r?url=<?php 

echo "$FeedCookie5"; 

?>&align=left&width=300&frameColor=black&titleBarColor=%23add8e6&titleBarTextColor=

black&boxFillColor=white&textColor=black&fontFace=&maxItems=7&compact=&xmlButton=&j

avascript=true"></script>

<form action="test1.php">
        <input type="text" name="formfeedurl1" size="26">
        <input type="submit" name="Add" value="Add Feed">
</form>
<a href="test1.php?delete=yes">Delete Cookie</a><br>Please referesh the page

<?php
exit;
}
{
setcookie ("FeedCookie5", $formfeedurl1,time()+3600);
}
?>

<form action="test1.php">
        <input type="text" name="formfeedurl1" size="26">
        <input type="submit" name="Add" value="Add Feed">
</form>
<a href="test1.php?delete=yes">Delete Cookie</a><br>Please refresh the page

Thanks
Shantanu Oak
Answer  
Subject: Re: Array cookie in PHP
Answered By: sycophant-ga on 22 Jul 2003 12:34 PDT
Rated:5 out of 5 stars
 
Hi shantanuo-ga ,  
 
There are two ways to do what you want.  
 
The first is to store an array in the cookie, this can be done easily
with serialize(). However I would steer away from storing potentially
large volumes of data in a single cookie.
 
The second and possibly better option is to store a number of cookies,
and reconstitute them into an array within your software.
 
You can see this file in action at
http://dylan.wibble.net/programming/ga-test/233626/test.php
The source is at: http://dylan.wibble.net/programming/ga-test/233626/test.txt
 
My code is a bit verbose, and could be done better, but it
demonstrates the principle well. By having predictable cookie names,
it is easy to handle them.
 
I hope that answers your question. Let me know if any of my code is
unclear to you.
 
Regards,  
Sycophant-ga

Request for Answer Clarification by shantanuo-ga on 22 Jul 2003 19:54 PDT
Hi
Thanks for answer. The purpose of posting a question was not to learn
array cookies, but to use it to solve my problem. If you can write
such a perfect code, you can also edit the code mentioned above! :)
1. I want to know how many cookies are set. So the columns required
are total number of cookies + 1. (after adding the cookie(0))
2. I want to fill those different columns depending upon the values
set in the cookie.
3. I want a single link to "Delete All Cookies".

Shantanu Oak

Clarification of Answer by sycophant-ga on 24 Jul 2003 03:03 PDT
Hi shantanuo-ga,

Done.
http://dylan.wibble.net/programming/ga-test/233626/test2.php 
The source is at: http://dylan.wibble.net/programming/ga-test/233626/test2.txt

You have the variable $cookiecount, which contains the number of
retrieved cookies (which is now a maximum of 100).

You can pass "deleteall=yes" which will deleteall cookies. Or
"delete=$i" where $i is the ID of a cookie (to remove a specific one).
Or, "formfeedurl=$i" where $1 is a URL to add.

Hope that covers it.

Regards,
Sycophant-ga
shantanuo-ga rated this answer:5 out of 5 stars
Your marvellous code is worth a million dollars! Is it possible to
rate 6 stars or 8 stars instead of just 5?

Comments  
Subject: Re: Array cookie in PHP
From: sycophant-ga on 22 Jul 2003 02:44 PDT
 
I have an answer for this, but for some reason I have been unable to
post this through my ISP's proxy server. I am still trying.
Subject: Re: Array cookie in PHP
From: knowledge_seeker-ga on 22 Jul 2003 06:11 PDT
 
Sycophant is experiencing technical difficulties and is unable to post
an answer or further comments at this time. Rather than keep you
waiting, he has asked that I post his answer for him.

-K~ 

  
====================================

Hi shantanuo-ga , 

There are two ways to do what you want. 

The first is to store an array in the cookie, this can be done easily
with serialize(). However I would steer away from storing potentially
large volumes of data in a single cookie.

The second and possibly better option is to store a number of cookies,
and reconstitute them into an array within your software.

You can see this file in action at
http://dylan.wibble.net/programming/ga-test/233626/test.php
The source is at: http://dylan.wibble.net/programming/ga-test/233626/test.txt

My code is a bit verbose, and could be done better, but it
demonstrates the principle well. By having predictable cookie names,
it is easy to handle them.

I hope that answers your question. Let me know if any of my code is
unclear to you.

Regards, 
Sycophant-ga
Subject: Re: Array cookie in PHP
From: sycophant-ga on 22 Jul 2003 12:35 PDT
 
Thank you knowledge-seeker, 

I have posted my answer above. Hopefully this way if shantanuo-ga has
any problems I can be notified through the normal channels of any
clarification requests.

Regards,
Sycophant-ga

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy