简体   繁体   中英

PHP doesn't see a cookie unless I refresh the page again

I know there are other questions like this on here, but I've read through the answers and I'm still not finding a solution to my issue.

All of these scripts are on the same page, search-results .

It shows results in data-tables, and lets the user select records to save to a list, which they can then print. The IDs of the selected records are stored in a records cookie. I can see that this works.

There's a link at the bottom of the page that fires this JS function:

function resultsPage(){
    window.location = "/search-results/?printqueue=true";
}

As you can see, this is the same page, but with a different parameter. This tells the page to display their saved results.

On the initial load with ?printqueue=true PHP cannot read the cookie, even tho the page has refreshed. I have to refresh (or click the link) AGAIN before PHP picks up the cookie. Why?

I'm getting the cookie data like this:

if( $_GET['printqueue'] == 'true' && $_COOKIE['records'] !== 'undefined' ) {
    $posts_to_show = explode(',',$_COOKIE['records']);
}

If that's just because of the order of operations, that's cool, nothing to do about it then. But any clean ways to "double refresh" the page without adding an extra parameter and refreshing again if it's present? I'd really hate to do that...

I beleieve its becouse $_COOKIE['records'] is not set yet..

Try:

if( $_GET['printqueue'] == 'true' &&  isset($_COOKIE['records']) ) {
    // your code
}

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