由于对 perl/MySQL 的了解不够,我希望这里有人可以提供帮助。该代码显然不起作用,但这是我到目前为止所拥有的,我仍在学习。
我想打开并搜索$ticket_id
从 php 页面获得的数据库,并获取电子邮件地址和/或电话电子邮件中的一个或两个(如果有的话)$ticket_id
。至少会有一个。
因为我似乎每隔一段时间就会被问到这个问题。(这不是学校项目。我从 75 岁起就辍学了)。
我的脚本在没有信息的数据库查找的情况下工作,但我必须使用ticket_id 和电子邮件地址对每个设置进行手动编码。很高兴我只有 5 个人在使用它,但一直被其他想要使用它的人询问。因此,我认为这是简化使用 MySQL 数据库的过程的好时机,如果我忙于使用 MySQL 数据库,除了填写表单页面来添加他们的信息之外,我不需要做太多工作。
在需要搜索的数据库中有 3 个表,我需要搜索 ticket_id,然后获取作为他们的电子邮件地址和/或电话电子邮件的用户名,但根据他们想要的通知类型,其中一个可能为空。
这可以根据需要进行更改。它只有一条记录可用于此新设置。
username: email(at)address(dot)com
ticket_id: 1-YS25UHRN3N9D
phone: 1234567890(at)cellphone(dot)com
有人告诉我这可以用 JavaScript window.onload 来完成,因此不需要将 HTML 页面更改为 PHP,但根本不知道 JavaScript。
回声脚本可能是错误的。$pg
此时脚本很好地接收了echo
它,不需要它。
PHP 页面顶部
<?php
$ip = $_SERVER['REMOTE_ADDR']; // Client IP address
$pg = $_SERVER['HTTP_REFERER']; // What page did they view
$ticket_id = '1-YS25UHRN3N9D'; // Ticket id
$message = '1'; // 1=email, 2=text, 3=email & text
$url = "/cgi-bin/log_it.cgi"; // back-end url
echo '<script type="text/javascript" src="' . $url . '?id=' . $id . '&ip=' . $ip
. '?ticket_id=' . $ticket_id . ' . '?message=' . $message . '"></script>';
?>
有人告诉我将数据库登录信息放在 cgi 中是不好的,但我不知道如何将其放入 php 文件并从 perl 或其他方式访问该文件。
#!/usr/bin/perl -W
use CGI;
use CGI param;
use POSIX qw/strftime/;
# database
$host = "localhost";
$database = "users";
$tablename = "tickets";
$user = "username";
$pw = "password";
# Obtain $id & $ip from web page.
$id = param("ticket_id");
$ip = param("ip");
# message
# 1 = email
# 2 = phone
# 3 = both
$message = param("message");
# Connect to database
$connect = Mysql->connect($host, $database, $user, $pw);
# DB
$connect->selectdb($database);
# MySQL QUERY
$myquery = "SELECT ticket_id FROM user";
# QUERY
$myquery = "SELECT ticket_id FROM $tablename";
# Need to be worked on.
# If record not found.
#
# "To Do" send a page or popup if the id is not valid.
# $file = "/var/log/invalid_id_iptrace.txt";
# open(FILE, ">$file");
# print FILE strftime("%A %B %d, %Y - %I:%M %p %Z\n", localtime(time) );
# print FILE " -- IP Address: $ip\n\t Accessed unknown $id:\n------\n";
# close(FILE);
# system("mail -s 'Page visited Unknown' admin\@mydomain.com \< /var/log
# /invalid_id_iptrace.txt");
# exit();
#check if we have a matching $id == $ticket_id
if ($id =~ /$ticket_id/) {
#if we have a match open the file to write.
$file = "/var/log/$ticket_id_iptrace.txt";
open(FILE, ">$file");
# lets put the time in the file.
print FILE strftime("%A %B %d, %Y - %I:%M %p %Z\n", localtime(time) );
# put the IP address and web page visited.
print FILE " -- IP Address: $ENV{REMOTE_ADDR}\n\t Accessed your page:
$ENV{HTTP_REFERER}\n------\n";
print FILE " -- Here is the IP information we found:\n------\n";
#close the file. Finished with the first part
close(FILE);
# Run the IP2Location script.
system("./iptrace.sh $ENV{REMOTE_ADDR} json city>>/var/log/$ticket_id_iptrace.txt");
# Lets send an email and or text with all the information.
# Send email
if($message =~ /1/) {
system("mail -s 'Web Page visited' $username \< /var/log/$ticket_id_iptrace.txt");
}
# Send text
if($message =~ /2/) {
system("mail -s 'Web Page visited' $phone \< /var/log/$ticket_id_iptrace.txt");
}
# Send email and text message
if($message =~/3/) {
system("mail -s 'Web Page visited' $username \< /var/log/$ticket_id_iptrace.txt");
system("mail -s 'Web Page visited' $phone \< /var/log/$ticket_id_iptrace.txt");
}
exit();
}
exit();
#end of script
谢谢
更新:代码的第二个(清理)版本:
#!/usr/bin/perl
use warnings;
use strict;
use CGI qw/param/;
use DBI();
# database
my $host = "remotehost";
my $database = "database";
my $tablename = "table";
my $user = "user";
my $pw = "pwd";
# Obtain $id & $ip from web page.
my $id = param("id");
my $ip = param("ip");
my $ticket_id = "ticket_id";
my $file = "/tmp/_iptrace.txt";
my $fileb = "/tmp/invalid_id_iptrace.txt";
my $fh = "FILE";
my $phone = "phone";
my $username = "username";
# message
# 1 = email
# 2 = phone
# 3 = both
my $message = param("message");
# Connect to database
my $connect = Mysql->connect($host, $database, $user, $pw);
# DB
$connect->selectdb($database);
# QUERY
my $myquery = "SELECT ticket_id FROM $tablename";
# EXECUTE
my $execute = $connect->query($myquery);
#check if we have a matching $id == $ticket_id
if ($id == $payer_id) {
#if we have a match open the file to write.
open(my $fh, '>', $file) or die $!;
# lets put the time in the file.
print $fh strftime("%A %B %d, %Y - %I:%M %p %Z\n", localtime(time) );
# put the IP address and web page visited.
print $fh " -- IP Address: $ENV{REMOTE_ADDR}\n\t Accessed your page:
$ENV{HTTP_REFERER}\n------\n";
print $fh " -- Here is the information we found:\n------\n";
#close the file. Finished with the first part
close($fh);
# Run the IP2Location script.
system("./iptrace.sh $ENV{REMOTE_ADDR} json city>>/tmp/$ticket_id$file");
# Lets send an email and or text with all the information.
# Send email
if($message == 1) {
system("mail -s 'Web Page visited' $username \< /tmp/$ticket
_id$file");
}
# Send text
if($message == 2) {
system("mail -s 'Web Page visited' $phone \< /tmp/$ticket
_id$file");
}
# Send email and text message
if($message == 3) {
system("mail -s 'Web Page visited' $username \< /tmp/$ticket
_id$file");
system("mail -s 'Web Page visited' $phone \< /tmp/$ticket
_id$file");
}
exit();
}
# "To Do" send a page or popup if the id is not valid.
open($fh, ">$fileb");
print $fh strftime("%A %B %d, %Y - %I:%M %p %Z\n", localtime(time) );
print $fh " -- IP Address: $ip\n\t Accessed unknown $id:\n------\n";
close($fh);
system("mail -s 'Page visited Unknown' admin\@mydomain.com \<
/tmp/invalid_id_iptrace.txt");
exit();
#end of script