2017-03-01 81 views
2

我試圖創建一個具有用戶輸入他們的憑證,然後針對MySQL數據庫檢查登錄環境JS((在適當的錯誤,如果錯誤的憑據返回)使用jQuery的阿賈克斯,並Perl的),經成功登錄,將用戶(用戶門戶顯示動態頁)傳遞JSON字符串的Perl CGI

我無法通過用戶ID到最後CGI頁面。

下面

是我的代碼:

的login.html

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <title>login</title> 
    <link rel="stylesheet" type="text/css" media="screen, projection" href="http://www.blueprintcss.org/blueprint/screen.css" /> 
    <link rel="stylesheet" type="text/css" media="screen, projection" href="http://www.blueprintcss.org/blueprint/plugins/buttons/screen.css" /> 
    <link rel="stylesheet" type="text/css" media="print" href="http://www.blueprintcss.org/blueprint/print.css" /> 
    <!--[if IE]><link rel="stylesheet" type="text/css" media="screen, projection" 
     href="http://www.blueprintcss.org/blueprint/ie.css"><![endif]--> 
    <script type="text/javascript" src="js/jquery-3.1.1.min.js"></script> 
    <script type="text/javascript" src="login.js"></script> 
    <style type="text/css"> 
    #loginContent { width: 350px; margin: 100px auto; } 
    button[type] { margin: 0.5em 0; } 
    #loginForm{ 
     width:25%; 
     padding: 5px 5px; 
     margin:auto; 
    } 
    </style> 
</head> 
<body> 
    <h2>Please have pop-ups enabled for this site</h2> 
    <div id="loginResult" style="display:none;"></div> 
    <form id="loginForm" name="loginForm" method="post" action=""> 
     <fieldset> 
     <legend>Enter Information</legend> 
      <p> 
       <label for="username">Username</label> 
       <br /> 
       <input type="text" id="username" name="username" class="text" size="20" /> 
      </p> 
      <p> 
       <label for="password">Password</label> 
       <br /> 
       <input type="password" id="password" name="password" class="text" size="20" /> 
      </p> 
      <p> 
       <button type="submit" class="button positive">Login</button> 
      </p> 
     </fieldset> 
    </form> 
</body> 
</html> 

login.js

$(document).ready(function(){ 
$("form#loginForm").submit(function() { // loginForm is submitted 
var username = document.getElementById("username").value; //$('#username').attr('value'); // get username 
var password = document.getElementById("password").value; //$('#password').attr('value'); // get password 

if (username && password) { // values are not empty 
    $.ajax({ 
    type: "GET", 
    url: "/cgi-bin/login.pl", // URL of the Perl script 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    // send username and password as parameters to the Perl script 
    data: "username=" + username + "&password=" + password, 
    // script call was *not* successful 
    error: function(XMLHttpRequest, textStatus, errorThrown) { 
     $('div#loginResult').text("responseText: " + XMLHttpRequest.responseText 
     + ", textStatus: " + textStatus 
     + ", errorThrown: " + errorThrown); 
     $('div#loginResult').addClass("error"); 
    }, // error 
    // script call was successful 
    // data contains the JSON values returned by the Perl script 
    success: function(data){ 
     if (data.error) { // script returned error 
     $('div#loginResult').text("data.error: " + data.error); 
     $('div#loginResult').addClass("error"); 
     } // if 
     else { // login was successful 
     $('form#loginForm').hide(); 
     $('div#loginResult').text("data.success: " + data.success 
      + ", data.userid: " + data.userid); 
     $('div#loginResult').addClass("success"); 
     window.open("/cgi-bin/portal.pl"); 
     } //else 
    } // success 
    }); // ajax 
} // if 
else { 
    $('div#loginResult').text("enter username and password"); 
    $('div#loginResult').addClass("error"); 
} // else 
$('div#loginResult').fadeIn(); 
return false; 
}); 
}); 

login.pl

#!C:\Strawberry\perl\bin\perl.exe 
use CGI; 
use DBI; 
use strict; 
use warnings; 

# read the CGI params 
my $cgi = CGI->new; 
my $username = $cgi->param("username"); 
my $password = $cgi->param("password"); 

# connect to the database 
my $db = 'bakery_users'; 
my $host = 'localhost'; 
my $user = 'root'; 
my $pass = '[email protected]'; 
my $dbh = DBI->connect ("DBI:mysql:database=$db:host=$host", $user, $pass) or die "Can't connect to database: $DBI::errstr\n"; 
# check the username and password in the database 
my $statement = qq{SELECT id FROM users WHERE username=? and password=?}; 
my $sth = $dbh->prepare($statement) 
    or die $dbh->errstr; 
$sth->execute($username, $password) 
    or die $sth->errstr; 
my ($userID) = $sth->fetchrow_array; 

# create a JSON string according to the database result 
my $json = ($userID) ? 
    qq{{"success" : "login is successful", "userid" : "$userID"}} : 
    qq{{"error" : "username or password is wrong"}}; 

# return JSON string 
print $cgi->header(-type => "application/json", -charset => "utf-8"); 
print $json; 

而且這是一個需要知道用戶,所以我可以服務於一些特定的信息垃圾門戶CGI頁面

#!C:\Strawberry\perl\bin\perl.exe 
use CGI; 
use strict; 
use warnings; 

my $cgi = CGI->new; #new CGI routine 

print $cgi->header('text/html'); #create HTTP header 

print "<html> <head>\n"; 
print "<title>Hello, world!</title>"; 
print "</head>\n"; 
print "<body>\n"; 
print "<h1>Hello, world!</h1>\n"; 
print "</body> </html>\n"; 

大多數的登錄文件從IBM開發者頁面修訂於simple CGI user login page with mySQL

+1

沒有人應該通過150(或不管它是什麼)LOC試圖幫助你。你能把我們的問題提煉成一個[mcve]嗎? –

+0

我描述了我的問題在介紹中。我的代碼在那裏可以理解我迄今爲止設置的內容。我想將用戶傳遞給另一個頁面。 – PrettyNeet

+2

如果您嘗試過,您可以更簡潔地重現問題。代碼轉儲和爲您調試代碼的請求以及弄清楚什麼是錯誤是懶惰的。 –

回答

-1

我能夠通過另一個Perl模塊CGI:Session傳遞CGI參數。

加入

my $session = new CGI::Session(undef, undef) or die CGI::Session->errstr; 
$session->param("username", $username); 
my $cookie = $cgi->cookie(CGISESSID => $session->id); 
print $cgi->header(-cookie=>$cookie); 

login.pl文件並添加以下塊到我的portal.cgi

my $sid = $cgi->cookie('CGISESSID') || $cgi->param('CGISESSID') || undef; #loads previous cookie 
my $session = load CGI::Session(undef, $sid); 
my $user = $session->param("username"); 

這會在臨時目錄的本地餅乾和閱讀以找到我在login.pl頁面上傳遞給它的「用戶名」變量