Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 使用变量修改URL_Perl_Cgi - Fatal编程技术网

Perl 使用变量修改URL

Perl 使用变量修改URL,perl,cgi,Perl,Cgi,我想在URL中添加一个变量,如下所示 www.example.com/index.cgi?username=demo 我在index.html中有一个表单,它的action=login.cgi if (($cgi->param('username') eq 'demo') && ($cgi->param('password') eq 'demo')) { my cookie= CGI::Cookie->new(-name=>'username', -val

我想在URL中添加一个变量,如下所示 www.example.com/index.cgi?username=demo

我在index.html中有一个表单,它的action=login.cgi

if (($cgi->param('username') eq 'demo') && ($cgi->param('password') eq 'demo')) {
my cookie= CGI::Cookie->new(-name=>'username', -value=>$cgi->param('username'));
print $cgi->redirect(-uri => 'index.cgi', -cookie => $cookie);
}
else {
print $cgi->redirect(-uri => 'index.html');
}
在login.cgi中

if (($cgi->param('username') eq 'demo') && ($cgi->param('password') eq 'demo')) {
my cookie= CGI::Cookie->new(-name=>'username', -value=>$cgi->param('username'));
print $cgi->redirect(-uri => 'index.cgi', -cookie => $cookie);
}
else {
print $cgi->redirect(-uri => 'index.html');
}
在index.cgi中

my $username = $cookies{username}->value;
因此,我想修改index.cgi的URL,以便它也从变量$username添加用户名

www.example.com/index.cgi?username=demo

可以使用点运算符或双引号组合字符串

使用点运算符:my$url='www.example.com/index.cgi?username='$用户名

使用双引号:my$url=www.example.com/index.cgi?username=$username

您可以组合多个字符串:

my $base_url = 'www.example.com/index.cgi';
my $key = 'username';
my $value = $username;
my $url = "$base_url?$key=$value";
不要忘记转义字符串中的特殊字符,如&或%:

use URI::Escape;
$username = uri_escape $username;
如果您有密钥和值的散列,则可以使用:

use URI::Escape;
my %h = (username => 'demo', password => 'demo', topic => 'perl');
my $url = 'www.example.com/index.cgi';
$url .= '?' . join '&', map {uri_escape "$_=$h{$_}"} keys %h;

你到底遇到了什么问题?如何修改URL?表单的值在index.html中传递后,如果用户名为demo,index.cgi的url将变为index.cgi?usename=demo您是否在询问如何附加到字符串?试试我在问如何在URL后面加上变量,比如_GET[username]好的,你试过使用了吗。操作人员