/ Published in: Perl
For database inserts must be made some site on "http://redirect_host:port/.../" with nice getip function: http://snipplr.com/view/10946/
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#!/usr/bin/perl # # # # CREATE TABLE `redirects` ( # `id` int(11) NOT NULL auto_increment, # `ip` varchar(15) collate utf8_polish_ci NOT NULL, # `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP, # `choice` varchar(10) collate utf8_polish_ci NOT NULL, # PRIMARY KEY (`id`), # UNIQUE KEY `ip` (`ip`) #) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci AUTO_INCREMENT=3395 ; # # use DBI; $dbase = DBI->connect("DBI:mysql:database=DBNAME;host=DBHOST","DBUSER","DBPASS", { RaiseError => 1 }); $utsfmt = "UNIX_TIMESTAMP()"; openlog("redirector"); setlogsock('unix'); syslog('info', 'Script started.'); $|=1; while (<>) { my $url = $X[0]; my $oul = $url; my $sdbq = $dbase->prepare("SELECT * FROM redirects WHERE ip='".$ip[0]."' AND choice IN ('0','1','3')"); $sdbq->execute(); if ($sdbq->rows == 0) { if (match_ip($ip[0], $net1)) { $url="http://redirect_host:port/net1/".$uri[3]; } elsif(match_ip($ip[0], $net2)) { $url="http://redirect_host:port/net2/".$uri[3]; } else { $url="http://redirect_host:port/rest/".$uri[3]; } $action ="New"; $all = "[".$ip[0]."] ".$url."[".$action."] - ".$oul." - ".$uri[3]."n"; syslog('info', $all); } else { $action ="In database"; $all = "[".$ip[0]."] ".$url."[".$action."] - ".$oul." - ".$uri[3]."n"; syslog('info', $all); } } closelog();