不理解Perl/MySQL足夠我希望有人在這裏可以伸出援助之手。 該代碼顯然不起作用,但這是我迄今爲止,我仍在學習。PHP/MySQL表搜索Perl
我想打開並搜索從php頁面獲得的$ticket_id
的數據庫,並獲取電子郵件地址和/或電子郵件中的一個或兩個電子郵件地址(如果有的話)$ticket_id
。至少會有一個。
因爲我似乎每隔一段時間都會問這個問題。 (這不是一個學校項目,我從75歲起就失學了)。
我有腳本工作與信息的數據庫查找,但我必須手工編碼每個設置與ticket_id和電子郵件地址(S)。很高興我只有5個人使用它,但一直被其他人想要使用它。所以我認爲現在應該是一個很好的時間來使用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
爲什麼不在PHP端做所有這些?你爲什麼使用cgi腳本?爲什麼用JS調用它......你應該只使用PHP的curl。 – prodigitalson 2013-02-20 21:10:41
@prodigitalson因爲讓用戶複製並粘貼超過6行的內容很容易出錯。如果我允許其他人使用設置,我不希望他們在他們的網站上擁有所有內容。 PHP是由某人給我的。所以你提出一個我沒有答案的問題。 – 2013-02-20 22:45:54
因此,這一切都運行在一臺服務器上或跨多臺服務器?你最適合的語言是什麼? – prodigitalson 2013-02-20 23:48:28