| | |
PHP cookie question
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jun 2005
Posts: 92
Reputation:
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.
PHP Syntax (Toggle Plain Text)
$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!
![]() |
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
| Thread Tools | Search this Thread |
apache api array auto beginner binary broken cache cakephp checkbox class cms code codingproblem cron curl customizableitems database date display dynamic echo email error errorlog file files filter folder form format forms forum function functions gc_maxlifetime global google headmethod host href htaccess html image include insert ip javascript joomla limit link login mail malfunctioning memmory memory menu mlm multiple mysql nodes oop parameter parsing paypal pdf php phpmysql popup query radio random recursion recursiveloop remote script search select server sessions snippet source space sql static survey syntax system table trouble tutorial up-to-date update upload url validator variable video web youtube






