Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: From php to perl + template solution

by scorpio17 (Canon)
on Jul 13, 2010 at 13:39 UTC ( #849263=note: print w/replies, xml ) Need Help??

in reply to From php to perl + template solution

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!)

  • Comment on Re: From php to perl + template solution

Replies are listed 'Best First'.
Re^2: From php to perl + template solution
by Nik (Initiate) on Jul 13, 2010 at 16:26 UTC
    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 script to calculate the counter data, render the tempalte and display it?

      I was thinking of something like this:

      URL a user enters:


      mod_perl maps this into:


      or even:


      (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

      A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://849263]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2023-06-08 19:35 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (35 votes). Check out past polls.