Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: From php to perl + template solution

by scorpio17 (Monsignor)
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
Re^2: From php to perl + template solution
by Nik 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 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

        Why use mod_perl and not just mod_rewrite with soemthign like this:

        RewriteCond %{REQUEST_FILENAME} -f RewriteRule ^/?(.+) /cgi-bin/index.py?page=$1 [L,PT]
        So every incoming html page request to the web server should redirect to cgi-bin/index.py
        Isn't it more easy?

        My rule although seems correct thought aint working because when a request an html page a get its output instead of redirection

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2014-12-21 17:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (106 votes), past polls