•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 392,071 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,268 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 1208 | Replies: 1
![]() |
| |
•
•
Join Date: Jun 2005
Posts: 85
Reputation:
Rep Power: 4
Solved Threads: 0
I try to set cookie values from mysql DB, I have the code:
Now the strange thing is, if the number of ID is less than 16, script runs without any issue, I can set cookie and retrieve those cookie values, however if the number of ID from DB is more than 16, script cannot set cookies, I don't know what causes this problem, anyone can give me a clue? thanks in advance.
$result = mysql_query("select ID from table");
while ($thisrow=mysql_fetch_row($result))
{
$gname="name_".$i;
setcookie ("$gname", $thisrow[0]);
$i++;
}Now the strange thing is, if the number of ID is less than 16, script runs without any issue, I can set cookie and retrieve those cookie values, however if the number of ID from DB is more than 16, script cannot set cookies, I don't know what causes this problem, anyone can give me a clue? thanks in advance.
It probably has nothing to do with the number 16, it may just so happen that the browser has run out of space to store cookies from your domain.
There is a limited amount of space allocated for each domain.
usually, you dont want to be setting all the rows in your mysql query result to cookies, since space is limited. However, you would rather just save meta data on the clients browser.
Then use this meta data to access settings for the client that is saved on your server database, or txt files etc.
Using your example:
[php]
// check if there is a cookie named saved_db_queries for this user
// The value of this cookie is the unique id for the file for which the users mysql queries are cached
$user_query_id = $_COOKIE['saved_db_queries'];
if (!$user_query_id) {
// user has not been here, create a new file id to save mysql queries to
$user_query_id = setcookie('saved_db_queries', $_SERVER['REMOTE_ADDR'].rand(0, 1000);
}
// this file is used to save the users query results
// its created out of the id we have saved as a cookie on the users computer
$file = 'cache/query_'.$user_query_id.'.txt';
$fp = fopen($file, 'w'); // create a pointer to a file resource/stream
$result = mysql_query("select ID from table");
while ($thisrow=mysql_fetch_row($result))
{
$gname="name_".$i;
saveQueryToFile($fp, "$gname", $thisrow[0]):
$i++;
}
fclose($fp); // close our file resource pointer
/**
* Writes a single line of string data to a txt file
*
*/
function saveQueryToFile($fp, $line_id, $line_data)
{
if (is_resource($fp))
{
$line = $line_id.' '.$line_data."\r\n";
fwrite($fp, $line, strlen($line);
}
}
[/php]
Here, we only set a cookie named "saved_db_queries" on the users computer, with the value being the a unique identifyer for the file on the server where we will save data for this user.
This way we dont need to save data on the client, jus the meta data needed to retrieve user based data for this user.
There are other ways you can save data client side, on the users browser, other than cookies. There however involve javascript.
Its usually called JavaScript Object Persistance, which you can look up on google.
An example is using Frames, such as Gmail.
All these however, only last while the user is viewing your website, once they close the browser, pfft.. gone.
usually, theres no need for saving lots of permanent data on the browser however, other methods will do.
Note: Saving data on the browser (cookies) can be misleading.
Every time a http request is made, cookie headers have to sent back to the server in order for your php to read the cookies on the browser.
So saving the data on your server in the first place, and using only an id to link that data to the user, actually saves bandwidth.
There is a limited amount of space allocated for each domain.
usually, you dont want to be setting all the rows in your mysql query result to cookies, since space is limited. However, you would rather just save meta data on the clients browser.
Then use this meta data to access settings for the client that is saved on your server database, or txt files etc.
Using your example:
[php]
// check if there is a cookie named saved_db_queries for this user
// The value of this cookie is the unique id for the file for which the users mysql queries are cached
$user_query_id = $_COOKIE['saved_db_queries'];
if (!$user_query_id) {
// user has not been here, create a new file id to save mysql queries to
$user_query_id = setcookie('saved_db_queries', $_SERVER['REMOTE_ADDR'].rand(0, 1000);
}
// this file is used to save the users query results
// its created out of the id we have saved as a cookie on the users computer
$file = 'cache/query_'.$user_query_id.'.txt';
$fp = fopen($file, 'w'); // create a pointer to a file resource/stream
$result = mysql_query("select ID from table");
while ($thisrow=mysql_fetch_row($result))
{
$gname="name_".$i;
saveQueryToFile($fp, "$gname", $thisrow[0]):
$i++;
}
fclose($fp); // close our file resource pointer
/**
* Writes a single line of string data to a txt file
*
*/
function saveQueryToFile($fp, $line_id, $line_data)
{
if (is_resource($fp))
{
$line = $line_id.' '.$line_data."\r\n";
fwrite($fp, $line, strlen($line);
}
}
[/php]
Here, we only set a cookie named "saved_db_queries" on the users computer, with the value being the a unique identifyer for the file on the server where we will save data for this user.
This way we dont need to save data on the client, jus the meta data needed to retrieve user based data for this user.
There are other ways you can save data client side, on the users browser, other than cookies. There however involve javascript.
Its usually called JavaScript Object Persistance, which you can look up on google.
An example is using Frames, such as Gmail.
All these however, only last while the user is viewing your website, once they close the browser, pfft.. gone.
usually, theres no need for saving lots of permanent data on the browser however, other methods will do.
Note: Saving data on the browser (cookies) can be misleading.
Every time a http request is made, cookie headers have to sent back to the server in order for your php to read the cookies on the browser.
So saving the data on your server in the first place, and using only an id to link that data to the user, actually saves bandwidth.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
Similar Threads
- Am I looking for PHP? (PHP)
- php rewrite question (PHP)
- any one fermilliar with php-nuke? I have a question. (Existing Scripts)
Other Threads in the PHP Forum
- Previous Thread: PHP and MySql
- Next Thread: Parse Email Headers


Hybrid Mode