Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Temp Webpage While Creating Final PHP Webpage

by bobinyec (Acolyte)
on Apr 15, 2020 at 00:20 UTC ( [id://11115543]=perlquestion: print w/replies, xml ) Need Help??

bobinyec has asked for the wisdom of the Perl Monks concerning the following question:

Via a browser, a user can make a request to my perl script. The perl script does some lengthy processing to create a php webpage and then redirects to it.

I can have the original script create a temporary "please wait" page while it's doing its real work, but when it's done I can't figure out how to redirect to the new PHP page.

Does anyone have a suggestion of how to do this?

Thanks - jb

  • Comment on Temp Webpage While Creating Final PHP Webpage

Replies are listed 'Best First'.
Re: Temp Webpage While Creating Final PHP Webpage
by Your Mother (Archbishop) on Apr 15, 2020 at 05:27 UTC

    Assuming I understand you–

    1. Request to Perl to initiate creation of PHP page.
    2. Perl serves a very small dummy/placeholder page immediately saying “Please wait…” perhaps forking such that parent serves page and exits while child creates PHP–
      1. The new page has a small Ajax routine; I like jQuery, there are MANY other good libraries.
      2. The Ajax polls for the PHP resource every $x milliseconds.
      3. When the PHP page is ready, the JS replaces the location of the page, effectively redirecting; but not quite the same and there are other ways to do this and they have various implications in browser history.

    Barring JS, you can do this with meta refresh and redirect tricks but it’s messy and gross. :P Might/should include <noscript/> tags such that non-JS enabled browsers will see they cannot properly use the resource.

      You understand the problem perfectly. The file on the server that I would be looking for already exists, but would be old. I would need to poll to find out the date of the file. When it has been updated and has a current date and time, I would change the location.

      Can you recommend an AJAX routine to do this? A small code snippet would be very much appreciated.

      Thanks - jb

        Untested but semi-complete outline. I’m fair at this stuff but beware of cargo culting. Try to understand everything that’s going on if you’re going to use it. Any errors I make become your responsibility. :P

        #!/usr/bin/env perl use strictures; use CGI ":standard"; use Date::Calc "Today"; my $pid = fork(); if ( $pid ) { hold_page(); } elsif ( $pid == 0 ) { write_php(); } else { die "Couldn't fork\n"; } exit 0; sub hold_page { print header(), start_html("OHAI"), h1("Keep panties unbunched!"), noscript("It's", (Today())[0] . ", for the love of crabcakes!" +, "Enable JS!"); print <<'_HTML_'; # <- single quote matters. <script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJ +o=" crossorigin="anonymous"></script> <script> function checkPHP() { $.ajax({ url: "FILL THIS IN" ,timeout: 1000 }) .done( function( data ) { window.location.href = "FILL THIS IN"; }) .fail( function( jqXHR, textStatus, errorThrown ) { if ( textStatus === "timeout" ) { setTimeout(checkPHP, 2000); return; // Keep trying. } else if ( jqXHR.status == 404 ) { setTimeout(checkPHP, 2000); return; // Keep trying. } else { alert("WAT? " + jqXHR.status + " " + errorThrown); } }); } $(function(){ checkPHP() }); </script> _HTML_ print end_html(); } sub write_php { # Execute script that will write page # found at URL "FILL THIS IN" }
Re: Temp Webpage While Creating Final PHP Webpage
by marto (Cardinal) on Apr 15, 2020 at 07:29 UTC

    As a side note have you tried to profile your code to find out why it takes long enough to necessitate a 'please wait' page? See Devel::NYTProf. Is there a reason you use perl to generate php rather than just use a modern perl approach such as Mojolicious?

      The PHP code is complicated and I didn't write it. Actually, I'm just writing the data that the PHP routine uses. I wouldn't dream of trying to rewrite it in perl.

      Unfortunately, I have streamlined the background code, and it still has a great deal to do including several requests over the internet to go get what it needs.

      Thanks - jb

Re: Temp Webpage While Creating Final PHP Webpage
by jcb (Parson) on Apr 16, 2020 at 02:21 UTC

    What level of "freshness" is required? Could you put a Perl program in cron to refresh the PHP page on a schedule and have it always be "fresh enough" for your purposes? Maybe every 10 minutes?

      What I decided to do was present the page with all of the information at hand, but skipping the data that I need to go get from the internet. That shortens the waiting time up a lot. There already is an updating function running in the background which updates the page after 5 minutes or so, including the internet data.

      Thanks - jb

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11115543]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2024-04-23 18:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found