2013-03-25 63 views
-2

我有这个代码为我的项目,我希望用户被定向到他们的帐户页面,显示特定于他们的数据。即他们的供应商名单。我意识到我需要创建一个会话变量,但我不知道在哪里把它放在我的代码中,我不知道代码在帐户页面中指定用户。谁能帮忙?这是我的代码。登录后显示用户特定的数据传统的asp

<% 
'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 
%> 

回答

0

假设你的客户表的主键被称为客户编号,那么你可以有类似

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

然后你customeraccount页面上,你可以有一个查询

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")