Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Welcome to the Monastery
 
PerlMonks  

From php to perl + template solution

by Nik
on Jul 13, 2010 at 07:27 UTC ( #849175=perlquestion: print w/ replies, xml ) Need Help??
Nik has asked for the wisdom of the Perl Monks concerning the following question:

My perl program is about counter adding displayingcounters for every webpage of my website Every html page(like index.php) along with html code has near the bottom of the html code a php block of code that had a unique page identifier number like the following snippet, which when the execution of the program reached that part it gave that unique page number to MyCounter.php module to calculate the page's counter and thn print it.
<? } $PageID = 1; $counter = counter($PageID); echo "<center><h4><font color='orange'>Number of counters: $counter +"; ?>
Now with the use of Perl + templates how the same thing is suppose to work? The perl code when ready should dislay the following result as with php now: http://webville.gr/index.php I need sepeartion between design and code leaving html pages intact. But if a user clicks a random webpage how that webpage should invoke index.pl if the html page now has no php code in it? __________________

Comment on From php to perl + template solution
Download Code
Re: From php to perl + template solution
by Anonymous Monk on Jul 13, 2010 at 07:47 UTC
    But if a user clicks a random webpage how that webpage should invoke index.pl if the html page now has no php code in it?

    Why would a webpage invoke a program? php ne perl

      Html Pages that the user requests should give control to index.pl with that webpage request as url parameter, so that the perl program calculate the page's counter data and display them.

      Up until now i had the following approach: HTML page with php code in it that called a php module than calculated counter data and give the execution back to html page to display it.

      But now with pure htmnl code with no calls to perl program and no php data, how the counter should be calculated? I need some way of calling index.pl to do stuff with that html file(template). And the question is how.
        Html Pages that the user requests should give control to index.pl with that webpage request as url parameter, so that the perl program calculate the page's counter data and display them.

        Ha

        Up until now i had the following approach: HTML page with php code in it that called a php module than calculated counter data and give the execution back to html page to display it.

        Sounds like php programs to me, not HTML page

        But now with pure htmnl code with no calls to perl program and no php data, how the counter should be calculated?

        Call a program

Re: From php to perl + template solution
by moritz (Cardinal) on Jul 13, 2010 at 08:03 UTC
    Now with the use of Perl + templates how the same thing is suppose to work?

    You retrieve the previous counter value, increment it, and store it again. Then you pass the new counter value to the template engine, let it render the template, and print the result.

    Perl 6 - links to (nearly) everything that is Perl 6.
      That what i want to do, but those stuff take place inside perl script code. If the user request a web page from my website, how the index.py will be called with that webpaeg request as parameter so that it know for WHAT webpage he need to add, display counter?

      I have no php code any more inside my html pages, just pure html code made from dreaweaver that the uyser clicks on them. How all the above take place?
        If the user request a web page from my website, how the index.py will be called

        Are you sure you're still talking about the same problem as before? Do you really need to mix perl, python and php?

        And can you not actually avoid calling an external program, and do the counter updating in Perl? If not, what prevents you? <bllockquote> so that it know for WHAT webpage he need to add, display counter?

        Usually by passing command line arguments, or setting environment variables. But of course that depends on how the called script expects to get its arguments.

        Perl 6 - links to (nearly) everything that is Perl 6.

        Seriously, did I wake up in 1994 today? A page counter? You've posted several times over the years about such things. Again your post seems to be about basic coding problems which aren't language/technology specific, others have already given you the pseudocode you need to implement. Consider installing something like awstats if you are concerned with site usage statistics.

Re: From php to perl + template solution
by scorpio17 (Monsignor) on Jul 13, 2010 at 13:39 UTC

    If I understand your original question correctly, you want to implement the page counter functionality that you had previously with PHP, using perl instead - BUT, you don't want to have to convert every existing page into a script.

    I can think of two ways to do this:

    1) Write a script that takes a single parameter: the name of the page. The script uses the "page" parameter to find the correct template to render and display. The "templates" here can be your original html pages, with a special template tag inserted where you want the counter to go. I would implement this using CGI::Application and HTML::Template. If you also use CGI::Application::Plugin::AutoRunmode and Apache's mod_rewrite module, you can even make the URLs look the same as before.

    2) The other option is to keep your original html files, but add an IMG tag where you want the counter to be. Make the SRC attribute of this image the URL to your counter script. You can pass in different page ids via a URL parameter. You'll need to return the result as an image file, however. But you can easily find (or make) GIF files for each digit, then have your script tile the appropriate pieces together to make any number. Use something like GD to do this. And make sure to send the correct (image) header, else you'll only see a broken image link.

    Having said all that, be aware that page counters are, in general, frowned upon these days - especially in shared hosting environments, as they tend to waste a lot of computer resources (database read+update per user, per click!)

      Thanks. Can't i just use mod_rewrite telling the webserver NOT TO run html pages the user clicks because these are my actual templates waiting to be rendered and instead redirect those requests to index.pl script to calculate the counter data, render the tempalte and display it?

        I was thinking of something like this:

        URL a user enters:

        http://your_server/page1.html
        

        mod_perl maps this into:

        http://your_server/cgi-bin/myscript.pl?temp=page1.html
        

        or even:

        http://your_server/cgi-bin/myscript.pl/page1
        

        (this is how it will look if you use CGI::Application. I suggest get this working first, then add a .htaccess file in your cgi-bin directory to add the mod_perl mod_rewrite stuff afterwords.)

        The script reads in template "page1.html", figures out the counter value, inserts it, and displays the page.

        All the template files should be in a special directory, not under the document root, so there's no way a user can point their browser to the "real" page1.html.

        update: corrected typo

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2014-04-21 08:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (492 votes), past polls