PHP to PERL

Discussion in 'Co-op Advertising Network' started by rudi, Sep 4, 2004.

  1. General Grant

    General Grant Well-Known Member

    Messages:
    318
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    160
    #21
    I'm trying to use this with MrRat's Amazon Product Feed, and I've tried putting <!--#include virtual="http://www.clickhereforit.com/adnetwork.cgi?ad_count=5;ad_separator=|" --> in the template, but It's not working. Anything else I can do to get this to work with cgi?
     
    General Grant, Dec 21, 2004 IP
  2. a389951l

    a389951l Must Create More Content

    Messages:
    1,885
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    140
    #22
    Can't put it in the template - need to put it in the actual .cgi file. At least that is what works for me.
     
    a389951l, Dec 21, 2004 IP
  3. General Grant

    General Grant Well-Known Member

    Messages:
    318
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    160
    #23
    Well that sux, there is no html output with my cgi file, it's all template driven
     
    General Grant, Dec 21, 2004 IP
  4. a389951l

    a389951l Must Create More Content

    Messages:
    1,885
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    140
    #24
    Doesn't have to be. I'll post later how I do it with Mr. Rat's script (AWS3 - haven't upgraded to AWS4 yet).
     
    a389951l, Dec 21, 2004 IP
  5. DFL

    DFL Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #25
    Hi everyone.

    Could anyone help me out. Where do i have to put the long perl code? Do i have to do create a file called ad_network.cgi and put it there?
     
    DFL, Dec 22, 2004 IP
  6. clickdoc

    clickdoc Guest

    Messages:
    39
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #26
    Grant does your template processor parse other includes? Includes are typically parsed by the web server so by the time you get to the template it's passed the SSI parser. We have a version that is designed to be called from inside a perl script as a perl routine, however we have not released it publicly, and will not till after the first of the year if we do at all.

    DFL, yes you need to create a file named ad_network.cgi and place the perl code inside. Then set the variable for where your ad_networks_ads.txt file is located. Your web server process must have write privileges to this file. If the directory specified is writable by the web server process it will create the file on first load for you.

    Feel free to ask any questions, I am traveling tho until after the first of the year so response time may be delayed.
     
    clickdoc, Dec 22, 2004 IP
  7. DFL

    DFL Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #27
    Thanks for the reply clickdoc

    I created the ad_network.cgi file and put it to the cgi-bin folder, same for ad_network.php and ad_network_ads.txt.

    But how should i dispay the links. Do i have to put this to my templates (my templates are also in cgi-bin forder):
    <?php
    ini_set ("include_path", ini_get ("include_path") . ':../:../../:../../../:../../../../');
    include ('ad_network.php');
    echo $ad_network[0];
    ?>

    Im asking because it doesnt work, so im probably doing something wrong.
     
    DFL, Dec 22, 2004 IP
  8. General Grant

    General Grant Well-Known Member

    Messages:
    318
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    160
    #28
    I have it fixed, I found the workaround on MrRat's forum
     
    General Grant, Dec 22, 2004 IP
  9. a389951l

    a389951l Must Create More Content

    Messages:
    1,885
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    140
    #29
    Gen. Grant - can you post the link so newer members can find it?
     
    a389951l, Dec 22, 2004 IP
  10. General Grant

    General Grant Well-Known Member

    Messages:
    318
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    160
    #30
    Its in this thread here
     
    General Grant, Dec 22, 2004 IP
  11. garnetbailey

    garnetbailey Guest

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #31
    I think this is going to solve my problems, but as a newbie at this, I am unsure of how to implement the code. Sorry for the dumb question, but should it go in a separate file in my root directory and use the php include code on my cgi generated web pages? Or does this code get included with the html of my page?
    Thanks for the help!
    Garnet
     
    garnetbailey, Dec 24, 2004 IP
  12. DFL

    DFL Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #32
    Can someone please help, i have done eveything said in this thread, but i dont know how should i display the ads. What code to i have to add to my templates or cgi file?
     
    DFL, Dec 26, 2004 IP
  13. clickdoc

    clickdoc Guest

    Messages:
    39
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #33
    I looked into the wrapper that Shawn suggested because I didn't want people to have to rely on me whenever Shawn makes improvements and adds features. I still think sticking with his php version is the best route. With that said I could not find a way to call a php program from inside a perl script if the server is running a DSO version of php (built into the web server instead of being a binary application). Therefore here is the latest Perl version including all of Shawn's new requirements.

    For people who are not running it as an include and are executing it from within a script with ` ` it should now not print the Content-type: declaration.



    #!/usr/bin/perl

    # $Id: ad_network.cgi,v 1.9 2005/01/07 08:03:09 cvs Exp $
    # $Revision: 1.9 $

    use strict;
    use LWP::UserAgent;
    use Fcntl qw:)flock);
    use Socket;
    srand;

    use vars qw/$ad_file $ad_url $VERSION $ad_network $ad_count $ad_separator @ad_params/;

    # use ini subroutine with global vars, usefull for mod_perl
    sub init {

    ### User Variables

    # edit ad_file to set the path to the ad_network_ads.txt file
    # default is to look in the same directory as where the script runs
    # just create a blank file in that directory that is writable by the
    # web server process and this script will set it up properly
    $ad_file = 'ad_network_ads.txt';

    # change type= here to the type of link you wish to serve
    $ad_url = qq[http://ads.digitalpoint.com/network.php?b=$ENV{'HTTP_HOST'}&type=link];

    # set this to the number of ads you want to display on each page
    $ad_count = 5;

    # set this to the text or html you want to separate the ads ex: <br> |
    $ad_separator = '<br>';

    if($ENV{QUERY_STRING}) {
    my @tags = split(/;/,$ENV{QUERY_STRING});
    foreach(@tags) {
    my ($name,$value) = split(/=/,$_);
    $ad_count = $value if($name eq 'ad_count');
    $ad_separator = $value if($name eq 'ad_separator');
    }
    }
    $ad_count = 5 if($ad_count > 5);

    ### end User Variables

    $VERSION = sprintf "%d.%d", qq$Revision: 1.9 $ =~ /(\d+)\.(\d+)/;
    $ad_network = '';
    }
    init();

    # only print content type if called as cgi (include) for other perl scripts
    # may be called with `/path/to/file/ad_network.cgi` to get output without
    # Content-type: declaration
    print qq{Content-type: text/html\n\n} if($ENV{GATEWAY_INTERFACE} =~ /CGI/);

    if(-w $ad_file) {
    if(open(FILE,"$ad_file")) {
    # if we can't get a lock that means a process is writing, just serve an ad from the existing file
    # and don't check the timestamp, this prevents a slow response from the DP server from hanging
    # every web process waiting to read an ad, there is slim a chance we could end up with a partial ad
    if(!flock(FILE, LOCK_SH|LOCK_NB)) {
    my $adlist = join('',<FILE>);
    close(FILE);
    my @ads = split(/<ad_break>/,$adlist);
    @ad_params = split(/\|/,$ads[0]);
    for(my $i=0;$i<$ad_count;$i++) {
    $ad_network .= $ads[int(rand(@ads-1))+1]; # dont get control info found in position 0
    $ad_network .= $ad_separator if($ad_count > 1);
    }
    } else {
    my $adlist = join('',<FILE>);
    close(FILE);
    my @ads = split(/<ad_break>/,$adlist);
    @ad_params = split(/\|/,$ads[0]);

    # if we haven't hit our cache limit or
    # the timestamp is less than timeout
    if(@ads < $ad_params[3] + 1 && $ad_params[0] + $ad_params[5] < time() || $ad_params[0] + $ad_params[4] < time()) {
    if(open(FILE,">$ad_file")) {
    flock(FILE, LOCK_EX); # get an exclusive lock so we can write the ad_network_ads file
    my $ua = new LWP::UserAgent;
    $ua->agent("Ad-Network-Perl/$VERSION)");
    my $req = HTTP::Request->new(GET => $ad_url);
    my $res = $ua->request($req);
    if($res->is_success) {
    my $content = '';
    $content .= $res->content;
    my ($params,$newad) = split(/<ad_param>/,$content);
    my @new_params = split(/\|/,$params);
    for(my $i=0;$i<@new_params;$i++) {
    $ad_params[$i+2] = $new_params[$i];
    }
    push(@ads,'') if(@ads == 0);
    push(@ads,$newad);
    }
    if(@ads > $ad_params[3]) {
    shift @ads;
    shift @ads;
    }
    my $packedip = gethostbyname('validate.digitalpoint.com');
    my $ipadr = inet_ntoa($packedip);
    @ad_params[0] = time();
    @ad_params[1] = $ipadr;
    $ads[0] = join('|',@ad_params);
    print FILE join('<ad_break>',@ads);
    close(FILE);
    }
    for(my $i=0;$i<$ad_count;$i++) {
    if(@ads > 1) { # dont get control information
    $ad_network .= pop(@ads);
    $ad_network .= $ad_separator if($ad_count > 1);
    }
    }
    } else {

    for(my $i=0;$i<$ad_count;$i++) {
    $ad_network .= $ads[int(rand(@ads-1))+1]; # dont get control information
    $ad_network .= $ad_separator if($ad_count > 1);
    }

    } # end else on check for 100 ads or timestamp older than one hour
    } # end else on if flock
    } else {
    warn qq { could not open file $ad_file because $! };
    } # end else on opening ad file
    } else {
    $ad_network = 'You must set the "ad_network_ads.txt" file to be writable.';
    } # end else on writable ad file

    print $ad_network . qq{\n};
     
    clickdoc, Jan 7, 2005 IP
  14. chachi

    chachi The other Jason

    Messages:
    1,600
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    0
    #34
    Clickdoc, you are my hero
     
    chachi, Jan 7, 2005 IP
  15. a389951l

    a389951l Must Create More Content

    Messages:
    1,885
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    140
    #35
    Yes clickdoc you are awesome. I tested it on two of my perl sites and it works great. Just needed to flush out my ad_network_ads.txt file since it had the "ad_network.php needs updating..."
     
    a389951l, Jan 7, 2005 IP
  16. clickdoc

    clickdoc Guest

    Messages:
    39
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #36
    Yes you should empty out your ad_network_ads.txt file to use this new script.

    Also after a little testing I noticed some servers still pass the $ENV variable to the script when its run from inside another script with ` `. If you are not using it as an include and just running it in another script you may need to remove the Content-type: line. Here is how you can call it from inside another script:


    my $output = `./ad_network.cgi`;
    $output =~ s/Content-type: text\/html//;
    print $output;

    You may have to put the full path to the ad_network.cgi file.
     
    clickdoc, Jan 7, 2005 IP
  17. Nick0r

    Nick0r Peon

    Messages:
    49
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #37
    Ads not found on http://shopping.2and2.net/

    Account not not yet activated. | Site failed automatic validation as of 2005-01-07 (make sure URL and site type is correct - if everything is correct, make a request in the forum for assistance). Validate/Calculate Weight Now

    Clearly the ads are on there and working.

    Any ideas?
     
    Nick0r, Jan 7, 2005 IP
  18. a389951l

    a389951l Must Create More Content

    Messages:
    1,885
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    140
    #38
    I think there might be a problem with the .cgi script. I can see the ads showing for sure but it is failing validation for both of my sites that are using the script. :(
     
    a389951l, Jan 7, 2005 IP
  19. Nick0r

    Nick0r Peon

    Messages:
    49
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #39
    Same with me!
     
    Nick0r, Jan 7, 2005 IP
  20. chachi

    chachi The other Jason

    Messages:
    1,600
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    0
    #40
    Perl wrapper posted here
     
    chachi, Jan 7, 2005 IP