2012-06-28 45 views
0

我ahve使用Apache httpd的2.2.15(Unix的)的mod_rewrite重寫規則,RewriteMap指令,設置Cookie的工作嚴重在一起

我有這樣的重寫goingon:

RewriteEngine On 
RewriteLogLevel 7 
RewriteMap encode-map prg:/var/www/centos6.local/encode.pl 
RewriteLog "/var/log/httpd/centos6.local-rewrite_log" 
RewriteCond %{QUERY_STRING} CID=(.*) 
RewriteRule .* - [L,CO=NDD_COOKIE:${encode-map:%1}:ndipiazza.test.local:50000] 

這樣做的目的是,如果你發現在查詢字符串的任何位置找到一個CID = xyz。如果你這樣做,創建一個xyz編碼的NDD_COOKIE自定義perl腳本/var/www/centos6.local/encode.pl

這隻會設置一次cookie,然後不會再次設置cookie,直到服務器重新啓動完全。

0)設置Hosts文件,以便ndpiazza.test.local指向192.168.2.7

1)啓動http://ndipiazza.test.local/test.html?CID=test123

結果:預期

2)切換到新的編碼cookie被寫入瀏覽器或清除瀏覽緩存。

3)啓動http://ndipiazza.test.local/test.html?CID=new567

結果:無Cookie是!

Here is a link to the encode.pl script

爲什麼會發生這種情況?

這裏是重寫日誌http://ndipiazza.test.local/test.html?UID=sfsfsfd888m當它不工作:在啓動Apache HTTPD

192.168.2.3 - - [28/Jun/2012:16:33:29 --0500] [ndipiazza.test.local/sid#7fc041c0c880][rid#7fc041d34608/initial] (2) init rewrite engine with requested uri /test.html 
192.168.2.3 - - [28/Jun/2012:16:33:29 --0500] [ndipiazza.test.local/sid#7fc041c0c880][rid#7fc041d34608/initial] (3) applying pattern '.*' to uri '/test.html' 
192.168.2.3 - - [28/Jun/2012:16:33:29 --0500] [ndipiazza.test.local/sid#7fc041c0c880][rid#7fc041d34608/initial] (4) RewriteCond: input='UID=sfsfsfd888' pattern='UID=(.*)' => matched 
192.168.2.3 - - [28/Jun/2012:16:33:29 --0500] [ndipiazza.test.local/sid#7fc041c0c880][rid#7fc041d34608/initial] (5) map lookup OK: map=encode-map key=sfsfsfd888 -> val= 
192.168.2.3 - - [28/Jun/2012:16:33:29 --0500] [ndipiazza.test.local/sid#7fc041c0c880][rid#7fc041d34608/initial] (5) setting cookie 'WS_CUID=ndipiazza.test.local; path=/; domain=50000' 
192.168.2.3 - - [28/Jun/2012:16:33:29 --0500] [ndipiazza.test.local/sid#7fc041c0c880][rid#7fc041d34608/initial] (1) pass through /test.html 

回答

0

回答:系統只會打開一次encode.pl。預計會不斷收聽stdin。

但你仍然可以使用prg rewritemap來做到這一點。

我錯過的大事是我需要在perl中的while循環。

這裏是我結束了與解決的問題:

httpd.conf中的條目:

RewriteEngine On 
RewriteLogLevel 6 
RewriteLog "/var/log/httpd/centos6.local-rewrite_log" 
RewriteCond %{QUERY_STRING} UID=(.*) 
RewriteMap encode-map prg:/var/www/centos6.local/encode.pl 
RewriteRule .* - [L,CO=NDD_COOKIE:${encode-map:%1}:ndipiazza.test.local:50000] 

encode.pl:

#!/usr/bin/perl 
sub trim($) 
{ 
    my $string = shift; 
    $string =~ s/^\s+//; 
    $string =~ s/\s+$//; 
    return $string; 
} 
    $| = 1; # Turn off I/O buffering  
    while (<STDIN>) { 
     s/-/0/g; # Replace dashes with 0's 
    $a = $_;  
    $s = ''; 
    for ($i=0; $i<length($a); ++$i) { 
     $c = substr($a, $i, 1);  
      if ($c eq 'Z') { 
       $s = $s . 'a'; 
      } elsif ($c eq 'z') { 
       $s = $s . 'A'; 
      } elsif ($c eq '9') { 
      $s = $s . '0'; 
     } else { 
       $s = $s . chr(ord($c)+1); 
      } 
    } 
    $s = scalar reverse trim($s); 
     print "$s\n"; 
    } 
1

http://httpd.apache.org/docs/current/rewrite/rewritemap.html#prg記載,該腳本只調用。應該爲每個請求返回不同值的RewriteMap可能應該使用dbd處理程序(僅在加載和配置mod_dbd時可用)。

您可以將腳本作爲CGI運行,甚至可以使用http://perldoc.perl.org/CGI/Cookie.html從Perl代碼中設置cookie,或者使用shell_exec從PHP調用腳本並使用該函數返回的值設置cookie。

+0

你可能是正確的。 ..但我認爲prg應該調用perl程序一次,並讓它開放,聽取輸入。我也會嘗試。 –