-2

我的项目有此代码,我希望将用户定向到他们的帐户页面,该页面显示特定于他们的数据。即他们的供应商名单。我意识到我需要创建一个会话变量,但我不知道将它放在我的代码中的哪个位置,而且我不知道在帐户页面中指定用户的代码。任何人都可以帮忙吗?这是我的代码。

<%
'Connection String
Dim Conn
'Query to be executed
Dim SQLQuery
'Recordset
Dim rs
'StudentNo Of Logged in user
Dim UserName
'Password of User
Dim Password

'Getting information from submitted form
UserName = request.form("username")
Password = request.form("password")
RememberMe = request.form("rememberme")

'If not blank Username password submitted
if UserName <> "" or Password <> "" then  

'Creating connection Object    
set Conn=server.createobject("ADODB.Connection")

'Creating Recordset Object    
set rs = Server.CreateObject("ADODB.Recordset")    

'Initialising Provider String    
connStr = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ="& Server.MapPath("database.mdb")&";"   


'Opening Connection to Database    
Conn.open  connStr        
'Query to be executed    
SQLQuery = "select * from customers_tbl where c_email = '"&UserName&"' AND c_password = '"&Password&"'"   
'Retrieving recordset by executing SQL   
set rs=Conn.execute(SQLQuery)    
'If no records retrieved    
if rs.BOF and rs.EOF then        
Response.Redirect "customerlogin.htm?username=" & UserName    
else         
'If remember me selected        
if RememberMe = "ON" then
'Writing cookies permanently            
Response.Cookies("UserName")=UserName            
Response.Cookies("Password")=Password            
Response.Cookies("UserName").Expires = Now() + 365            
Response.Cookies("Password").Expires = Now() + 365            
Response.Redirect "customeraccount.htm"
else
'writing cookies temporarily            
    Response.Cookies("UserName")=UserName            
    Response.Cookies("Password")=Password            
    Response.Redirect "customeraccount.htm"
end if        
'Closing all database connections        
Conn.Close       
rs.close          
set rs = nothing        
set Conn = nothing    
end if
else    
'Invalid User    
Response.Redirect "customerlogin.htm?UserName=blank"
end if
%>
4

1 回答 1

0

假设您的客户表的主键称为 CustomerId 那么您可以有类似的东西

if rs.BOF and rs.EOF then        
Response.Redirect "customerlogin.htm?username=" & UserName    
else
Session("CustomerId") = rs("customerid") 

然后在您的客户帐户页面上,您可以进行查询

SQLQuery = "select * from customers_tbl where customerid = "&Session("CustomerId")

关于您的代码,我注意到两件事。

首先,您似乎拥有带有 .htm 扩展名而不是 .asp 扩展名的 asp 页面。大概您已经更改了相关的 IIS 设置,以便它将 .htm 页面视为 ASP 而不是平面 html

其次,您使用的是 ODBC 连接字符串。这将起作用,但 OLEDB 驱动程序被认为更快 - 例如

connstr = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=""& Server.MapPath("database.mdb")
于 2013-03-26T00:16:03.227 回答