diff options
author | Dennis Eriksen <d@ennis.no> | 2019-11-01 14:22:53 +0100 |
---|---|---|
committer | Dennis Eriksen <d@ennis.no> | 2019-11-01 14:22:53 +0100 |
commit | 152218ae18c000e8bb2e9a5982353e92cf6fa25c (patch) | |
tree | 0183dd5fb4ad6444ebefd232673499127f78ab6f /redirect.cgi | |
parent | Initial commit (diff) | |
download | purl-152218ae18c000e8bb2e9a5982353e92cf6fa25c.tar.gz |
Just giving the executables file-endings
Diffstat (limited to 'redirect.cgi')
-rwxr-xr-x | redirect.cgi | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/redirect.cgi b/redirect.cgi new file mode 100755 index 0000000..c471831 --- /dev/null +++ b/redirect.cgi @@ -0,0 +1,43 @@ +#!/bin/perl -wT +# (c) 2019 Dennis Eriksen <https://dnns.no> + +use strict; +use warnings; + +use CGI; +use DBI; + +my $q = CGI->new; # create CGI object + +# create database handler +my $dbh = DBI->connect("dbi:Pg:dbname=purl") or die $DBI::errstr; + +# set the short +my $short = $ENV{REQUEST_URI}; +$short =~ s/^\///; + +# SQL Query +my $query = qq(SELECT shorts.url FROM shorts WHERE shorts.short = ?;); + +# run the query, and do an if on it +if (my $url = $dbh->selectrow_array($query, undef, $short)) { + $query = qq(UPDATE shorts SET count = count + 1, last_visited = now() WHERE short = ?;); + my $sth = $dbh->prepare( $query ); + my $rv = $sth->execute( $short ) or die $sth->errstr; + print $q->redirect($url); +} +else { + print $q->header(-status=>'404 Not found',-charset=>'utf-8'); + print <<notfound +<html> +<head><title>404 Not Found</title></head> +<body> +<center><h1>404 Not Found</h1></center> +<hr><center>nginx</center> +</body> +</html> +notfound +} + +# disconnect from database +$dbh->disconnect(); |