Create a cookie with a random value in PHP --> use that random value inside a MySQL query
I'm trying to build an online store as a project, and every visitor to the website will get a cookie with a randomly generated 'seed' as a value, which looks like this:
setcookie('pagination_seed', rand(), time() + (18000)); //5 hours
Then I'll use that value to create a mysql query to echo out the products, sorted by the 'seed' saved by the cookie.
SELECT
SQL_CALC_FOUND_ROWS
name,
id
FROM
products
ORDER BY
rand('$cookie_session_seed')
LIMIT
.........(php calculation to make the pagination work).........
The problem is that rand() cannot store a 'consistently random' value in the cookie session. Each page refresh will give the same user a 'new' random value, because the visitors will, by default, land on the products page, and according to how cookie sessions work, it only starts to work after a page reload.
What is a proper method to show fresh results after every x amount of time, per visitor?
For this project it doesn't really matter if all the visitors get the same seed (it would be better if they do get the same seed), as long as new and fresh results can be seen every x hours.
Please be lenient, I'm trying to learn about cookies for ages (I learnt php, jquery, mysql and css3) but cookies are my nightmare, I just can't wrap my head around it.
Maybe what I'm trying to achieve is not even done by using cookies. How is this supposed to be done?
php mysql
add a comment |
I'm trying to build an online store as a project, and every visitor to the website will get a cookie with a randomly generated 'seed' as a value, which looks like this:
setcookie('pagination_seed', rand(), time() + (18000)); //5 hours
Then I'll use that value to create a mysql query to echo out the products, sorted by the 'seed' saved by the cookie.
SELECT
SQL_CALC_FOUND_ROWS
name,
id
FROM
products
ORDER BY
rand('$cookie_session_seed')
LIMIT
.........(php calculation to make the pagination work).........
The problem is that rand() cannot store a 'consistently random' value in the cookie session. Each page refresh will give the same user a 'new' random value, because the visitors will, by default, land on the products page, and according to how cookie sessions work, it only starts to work after a page reload.
What is a proper method to show fresh results after every x amount of time, per visitor?
For this project it doesn't really matter if all the visitors get the same seed (it would be better if they do get the same seed), as long as new and fresh results can be seen every x hours.
Please be lenient, I'm trying to learn about cookies for ages (I learnt php, jquery, mysql and css3) but cookies are my nightmare, I just can't wrap my head around it.
Maybe what I'm trying to achieve is not even done by using cookies. How is this supposed to be done?
php mysql
1
It seems you should be checking if the cookie existsisset($_COOKIE['pagination_seed'])
before you set it again?
– Nick
Jan 20 at 12:04
Yes @Nick, I do that, but it still gives a 'new' cookie value each time the page is refreshed. That is the problem I'm faced with ... (because the cookie only starts working after a page reload), so it's like a continuous 'loop' that basically never sets a cookie to one value.
– anna
Jan 20 at 12:05
If you check if it exists then it shouldn't be creating a new value just because the page is refreshed as the cookie will already exist when the page is refreshed.
– Nick
Jan 20 at 12:06
Warning: You are wide open to SQL Injections and should really use parameterized prepared statements instead of manually building your queries. They are provided by PDO or by MySQLi. Never trust any kind of input, especially that which comes from the client side. Even when your queries are executed only by trusted users, you are still in risk of corrupting your data.
– Dharman
Jan 20 at 12:19
I would recommend to use sessions to store the data instead of cookies. You can never trust cookies.
– Dharman
Jan 20 at 12:20
add a comment |
I'm trying to build an online store as a project, and every visitor to the website will get a cookie with a randomly generated 'seed' as a value, which looks like this:
setcookie('pagination_seed', rand(), time() + (18000)); //5 hours
Then I'll use that value to create a mysql query to echo out the products, sorted by the 'seed' saved by the cookie.
SELECT
SQL_CALC_FOUND_ROWS
name,
id
FROM
products
ORDER BY
rand('$cookie_session_seed')
LIMIT
.........(php calculation to make the pagination work).........
The problem is that rand() cannot store a 'consistently random' value in the cookie session. Each page refresh will give the same user a 'new' random value, because the visitors will, by default, land on the products page, and according to how cookie sessions work, it only starts to work after a page reload.
What is a proper method to show fresh results after every x amount of time, per visitor?
For this project it doesn't really matter if all the visitors get the same seed (it would be better if they do get the same seed), as long as new and fresh results can be seen every x hours.
Please be lenient, I'm trying to learn about cookies for ages (I learnt php, jquery, mysql and css3) but cookies are my nightmare, I just can't wrap my head around it.
Maybe what I'm trying to achieve is not even done by using cookies. How is this supposed to be done?
php mysql
I'm trying to build an online store as a project, and every visitor to the website will get a cookie with a randomly generated 'seed' as a value, which looks like this:
setcookie('pagination_seed', rand(), time() + (18000)); //5 hours
Then I'll use that value to create a mysql query to echo out the products, sorted by the 'seed' saved by the cookie.
SELECT
SQL_CALC_FOUND_ROWS
name,
id
FROM
products
ORDER BY
rand('$cookie_session_seed')
LIMIT
.........(php calculation to make the pagination work).........
The problem is that rand() cannot store a 'consistently random' value in the cookie session. Each page refresh will give the same user a 'new' random value, because the visitors will, by default, land on the products page, and according to how cookie sessions work, it only starts to work after a page reload.
What is a proper method to show fresh results after every x amount of time, per visitor?
For this project it doesn't really matter if all the visitors get the same seed (it would be better if they do get the same seed), as long as new and fresh results can be seen every x hours.
Please be lenient, I'm trying to learn about cookies for ages (I learnt php, jquery, mysql and css3) but cookies are my nightmare, I just can't wrap my head around it.
Maybe what I'm trying to achieve is not even done by using cookies. How is this supposed to be done?
php mysql
php mysql
edited Jan 20 at 12:04
anna
asked Jan 20 at 12:01
annaanna
6418
6418
1
It seems you should be checking if the cookie existsisset($_COOKIE['pagination_seed'])
before you set it again?
– Nick
Jan 20 at 12:04
Yes @Nick, I do that, but it still gives a 'new' cookie value each time the page is refreshed. That is the problem I'm faced with ... (because the cookie only starts working after a page reload), so it's like a continuous 'loop' that basically never sets a cookie to one value.
– anna
Jan 20 at 12:05
If you check if it exists then it shouldn't be creating a new value just because the page is refreshed as the cookie will already exist when the page is refreshed.
– Nick
Jan 20 at 12:06
Warning: You are wide open to SQL Injections and should really use parameterized prepared statements instead of manually building your queries. They are provided by PDO or by MySQLi. Never trust any kind of input, especially that which comes from the client side. Even when your queries are executed only by trusted users, you are still in risk of corrupting your data.
– Dharman
Jan 20 at 12:19
I would recommend to use sessions to store the data instead of cookies. You can never trust cookies.
– Dharman
Jan 20 at 12:20
add a comment |
1
It seems you should be checking if the cookie existsisset($_COOKIE['pagination_seed'])
before you set it again?
– Nick
Jan 20 at 12:04
Yes @Nick, I do that, but it still gives a 'new' cookie value each time the page is refreshed. That is the problem I'm faced with ... (because the cookie only starts working after a page reload), so it's like a continuous 'loop' that basically never sets a cookie to one value.
– anna
Jan 20 at 12:05
If you check if it exists then it shouldn't be creating a new value just because the page is refreshed as the cookie will already exist when the page is refreshed.
– Nick
Jan 20 at 12:06
Warning: You are wide open to SQL Injections and should really use parameterized prepared statements instead of manually building your queries. They are provided by PDO or by MySQLi. Never trust any kind of input, especially that which comes from the client side. Even when your queries are executed only by trusted users, you are still in risk of corrupting your data.
– Dharman
Jan 20 at 12:19
I would recommend to use sessions to store the data instead of cookies. You can never trust cookies.
– Dharman
Jan 20 at 12:20
1
1
It seems you should be checking if the cookie exists
isset($_COOKIE['pagination_seed'])
before you set it again?– Nick
Jan 20 at 12:04
It seems you should be checking if the cookie exists
isset($_COOKIE['pagination_seed'])
before you set it again?– Nick
Jan 20 at 12:04
Yes @Nick, I do that, but it still gives a 'new' cookie value each time the page is refreshed. That is the problem I'm faced with ... (because the cookie only starts working after a page reload), so it's like a continuous 'loop' that basically never sets a cookie to one value.
– anna
Jan 20 at 12:05
Yes @Nick, I do that, but it still gives a 'new' cookie value each time the page is refreshed. That is the problem I'm faced with ... (because the cookie only starts working after a page reload), so it's like a continuous 'loop' that basically never sets a cookie to one value.
– anna
Jan 20 at 12:05
If you check if it exists then it shouldn't be creating a new value just because the page is refreshed as the cookie will already exist when the page is refreshed.
– Nick
Jan 20 at 12:06
If you check if it exists then it shouldn't be creating a new value just because the page is refreshed as the cookie will already exist when the page is refreshed.
– Nick
Jan 20 at 12:06
Warning: You are wide open to SQL Injections and should really use parameterized prepared statements instead of manually building your queries. They are provided by PDO or by MySQLi. Never trust any kind of input, especially that which comes from the client side. Even when your queries are executed only by trusted users, you are still in risk of corrupting your data.
– Dharman
Jan 20 at 12:19
Warning: You are wide open to SQL Injections and should really use parameterized prepared statements instead of manually building your queries. They are provided by PDO or by MySQLi. Never trust any kind of input, especially that which comes from the client side. Even when your queries are executed only by trusted users, you are still in risk of corrupting your data.
– Dharman
Jan 20 at 12:19
I would recommend to use sessions to store the data instead of cookies. You can never trust cookies.
– Dharman
Jan 20 at 12:20
I would recommend to use sessions to store the data instead of cookies. You can never trust cookies.
– Dharman
Jan 20 at 12:20
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54276214%2fcreate-a-cookie-with-a-random-value-in-php-use-that-random-value-inside-a-my%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54276214%2fcreate-a-cookie-with-a-random-value-in-php-use-that-random-value-inside-a-my%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
It seems you should be checking if the cookie exists
isset($_COOKIE['pagination_seed'])
before you set it again?– Nick
Jan 20 at 12:04
Yes @Nick, I do that, but it still gives a 'new' cookie value each time the page is refreshed. That is the problem I'm faced with ... (because the cookie only starts working after a page reload), so it's like a continuous 'loop' that basically never sets a cookie to one value.
– anna
Jan 20 at 12:05
If you check if it exists then it shouldn't be creating a new value just because the page is refreshed as the cookie will already exist when the page is refreshed.
– Nick
Jan 20 at 12:06
Warning: You are wide open to SQL Injections and should really use parameterized prepared statements instead of manually building your queries. They are provided by PDO or by MySQLi. Never trust any kind of input, especially that which comes from the client side. Even when your queries are executed only by trusted users, you are still in risk of corrupting your data.
– Dharman
Jan 20 at 12:19
I would recommend to use sessions to store the data instead of cookies. You can never trust cookies.
– Dharman
Jan 20 at 12:20