简体   繁体   中英

AJAX Request to PHP script being cached?

I have a PHP script that shuffles and array and returns it in a random order based off some parameters. If I directly input the script / parameters into the url, the output of the PHP script is random like it should be. The returned array is shuffled (I use shuffle($array) to do this).

Whenever I call it via AJAX, I get the same result no matter what. It will always return the same order. I've tried changing my $.get to $.ajax and setting cache to false, it didn't work, I've sent a parameter along with the $.get request that had a value of $.now() to break the cache.

No matter what I do, when I call the script, it returns the same order, but if I go to it directly in the URL, no problems, random every time.

The request looks something like this:

$.get("scripts/findAnime.php", { q: JSON.stringify(params) }, function(data, status) {
    console.log(data);
});

That's without any cache busting, but, like I said, I even tried cache busting methods, and nothing is working.

PHP shuffle looks something like this. There are multiple arrays that get put into $generatedAnimeList, but they all go through a process like this.

If I make the ajax call and just return $least, it will return the same every time, instead of being randomly shuffled.

if(!empty($least)) {
    shuffle($least);
    for($i = 0; $i < sizeof($least); $i++) {
        array_push($generatedAnimeList, $least[$i]['id']);
    }
    unset($least);
}

I don't know what to do. I had similar code to this before and it worked perfectly.

Update

So I put in the script:

$temp = array(1,2,3,4,5);
shuffle($temp);
echo json_encode($temp);

And the request returns the same "random" order every time. So something with the request has to be incorrect.

Update

Cleared my browser cache, did nothing. Still receive the same answer. I did discover based off the parameter (q), I will get a different answer. So going based off the above example, where I am simply shuffling an array. If I send q with a value of "ALL", it will always return the same number, and if I send q with the value of "ACTION", it will always return the same number, but different from when q was "ALL".

Update

I've stripped this thing down to the following. A JS file that makes a get request:

$.get("scripts/findAnime.php"+$.now(), function(data, status){
    console.log(data);
}

and the php script (findAnime.php) is this:

$temp = array(1,2,3,4,5);
shuffle($temp);
echo json_encode($temp);

No matter what, always get the same "shuffled" result. In my case, [5,4,1,2,3], every time.

Update

I can confirm that the JS is making a request to the PHP script and is returning a 200 every time. So it's requesting a new copy, at least supposedly.

Add the following headers on the PHP script:

header("Cache-Control: private, max-age=1");
header("Expires: ".gmdate('r', time()+1));

Also, check your server for other caching methods like mod_PageSpeed and varnish

SOLVED!!!!!

So...I feel like an idiot now. It was not a caching issue, far from it, actually. On my page, I set the php srand function to have a seed, since I have a part of the page that changes based off the day...

Apparently the srand() function also affects the shuffle() function's randomness.

So, after using srand(mktime(0,0,0)), I reset it by calling it again, srand(), and TADA! Random numbers!

Thank you for everyone who helps me out. I have a lot less hair now. Sorry that the answer turned out to be unrelated to the question itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM