【问题标题】:PHP/MySQL table search by PerlPerl 的 PHP/MySQL 表搜索
【发布时间】:2023-03-29 23:00:01
【问题描述】:

由于对 perl/MySQL 不够了解,我希望这里有人可以提供帮助。 该代码显然不起作用,但这是我目前所拥有的,我仍在学习。

我想打开并搜索从 php 页面获得的 $ticket_id 的数据库,并获取电子邮件地址和/或电话电子邮件中的一个或两个,如果有一个 $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 . '&amp;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 因为让用户复制和粘贴超过 6 行的内容很容易出错。我必须为 5 分中的 2 分。如果我允许其他人使用该设置,我不希望他们在他们的网站上拥有一切。 php是有人给我的。所以你问了一个我没有答案的问题。
  • 那么这一切都运行在单个服务器上还是跨多个服务器?你最擅长的语言是什么?

标签: php mysql perl


【解决方案1】:

您的帖子中似乎没有问题。您说“代码显然不起作用”,但您没有告诉我们您看到的意外行为是什么样的。所以这里有一些关于你的代码的 cmets。其中之一可能会解决这个问题。

1/ 在 shebang 行上应该是“-w”,而不是“-W”。事实上,这些天你应该删除 '-w' 并在 shebang 行下方添加 use warnings;

2/ 您还应该在use warnings; 下方添加use strict;。这似乎会完全破坏您的程序,因为它会坚持要求您预先声明所有变量 - 您使用 mymy $host = 'localhost' 等)执行此操作。一开始这会很痛苦,但这是一个非常好的习惯。

3/ 你只需要一个use CGI 行。它应该是use CGI qw/param/;

4/ 您正在使用一个名为 Mysql 的类。但是您不会在任何地方加载该类。那个类是从哪里来的?你真的应该使用DBIDBD::mysql 的组合。

5/ 您将 SQL 查询放入 $mysquery,但随后您立即用另一个查询覆盖它。这不会导致错误 - 这可能只是表明您有些困惑。

6/ 你的if ($id =~ /$ticket_id/) 几乎肯定写成if ($id == $ticket_id) 更好。如果您只是比较两个数字,则无需使用正则表达式。

7/ 你真的应该检查你对open 的调用的返回值。

open(FILE, ">$file") or die $!;

事实上,我会重写它以使用 3-arg open 和自动激活的文件句柄。

open(my $fh, '>', $file) or die $!;

然后,您需要在整个代码中将 FILE 更改为 $fh

8/ 同样,您与$message 的三个比较可以只是简单的== 匹配,而不是使用正则表达式匹配。

如果您打算做很多这方面的工作,那么我真的建议您阅读一本好书,例如“Learning Perl”或“Beginning Perl”。

【讨论】:

  • 没有看到清理后的代码,我真的无能为力。也许您可以发布当前代码作为对原始帖子的更新。
  • 虽然我会指出,如果您花时间仔细阅读 Perl 的错误消息,它们通常会很有意义。如果没有,请尝试将use diagnostics 添加到代码中。
  • 发布清理后的代码版本时,最好将其添加到帖子末尾,而不是覆盖原始版本。这样,其他人的 cmets 看起来就不会错。我已编辑您的帖子以更正此问题。
  • 如果您的票证 ID 是字符串而不是数字,那么您需要使用 eq 而不是 ==
  • 如果你切换到使用 DBI/DBD::mysql 而不是 Mysql.pm 那么你需要改变你连接到数据库的方式。有关详细信息,请参阅 DBI 文档。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-24
  • 1970-01-01
  • 1970-01-01
  • 2010-12-26
  • 1970-01-01
  • 2010-12-25
  • 1970-01-01
相关资源
最近更新 更多