Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8613235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:49:27+00:00 2026-06-12T04:49:27+00:00

I am trying to delay PHP execution until a cookie is set through JavaScript.

  • 0

I am trying to delay PHP execution until a cookie is set through JavaScript. The code is below, I trimmed the createCookie JavaScript function for simplicity (I’ve tested the function itself and it works).

<?php
if(!isset($_COOKIE["test"])) {
    ?>
    <script type="text/javascript">
        $(function() {
            // createCookie script
            createCookie("test", 1, 3600);
        });
    </script>
    <?php
    // Reload the page to ensure cookie was set
    if(!isset($_COOKIE["test"])) {
        header("Location: http://localhost/");
    }
}
?>

At first I had no idea why this didn’t work, however after using microtime() I figured out that the PHP after the <script> was executing before the jQuery ready function. I reduced my code significantly to show a simple version that is answerable, I am well aware that I am able to use setcookie() in PHP, the requirements for the cookie are client-side.

I understand mixing PHP and JavaScript is incorrect, but any help on how to make this work (is there a PHP delay? – I tried sleep(), didn’t work and didn’t think it would work, since the scripts would be delayed as well) would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:49:29+00:00Added an answer on June 12, 2026 at 4:49 am

    This can’t be done within a single request. PHP runs first, then JavaScript. Even incremental loading won’t help, because cookies are sent via headers and those have already been sent by the time PHP runs.

    However, you can let the JavaScript set the cookie and then reload the page once it’s done by using location.reload(). PHP would then only need to print the JavaScript, like so:

    <?php
    if(!isset($_COOKIE["test"])) {
        ?>
        <script type="text/javascript">
            $(function() {
                // createCookie script
                createCookie("test", 1, 3600);
                // reload the page, which would send the cookie at the next request
                location.reload();
            });
        </script>
        <?php
        // stop execution and wait for JavaScript to call you again.
        exit;
    }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Passing JavaScript Array To PHP Through JQuery $.ajax I'm trying to pass
I'm trying to find a way to delay all code that takes place after
I have a php / ajax / javascript form that I am trying to
I'm building a site in PHP and have javascript code for a 'fadeshow' (slideshow
I'm trying to implement the following code, which run successfully, but blows right through
I'm trying to make Jquery delete function, here is the code, that I wrote
I'm trying to make some sort of delay in the view controllers changing. I
I'm trying to build a tool for testing the delay of my internet connection,
I'm trying to get my application to perform an action after a delay, but
I'm trying to figure out what would be causing a 1 minute delay in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.