Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

From php to perl + template solution

by Nik (Initiate)
on Jul 13, 2010 at 07:27 UTC ( [id://849175]=perlquestion: print w/replies, xml ) Need Help??

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

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

Replies are listed 'Best First'.
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.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: From php to perl + template solution
by scorpio17 (Canon) 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

        A reply falls below the community's threshold of quality. You may see it by logging in.
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

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (1)
As of 2024-03-19 03:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found