2014-02-18 171 views
0

我有一段代码可以生成一个介于1和1000之间的随机数。然后它将该数字存储为一个会话。然后,您将收到一封电子邮件,其中包含打印到表单中的会话编号并提交,但问题在于每当您提交表单时(页面上有两个表单),它会刷新随机数并重置会话,导致您的代码无效。我怎么能阻止这个?停止会话被覆盖

下面是我目前的问题;

HTML

<form action="cantaccess.asp" method="post"> 
     <p>Email:<input type="text" name="inputtedEmail" value="" /></p> 
     <input type="submit" name="submitEmail" value="submit" /> 
    </form> 

    <form action="cantaccess.asp" method="post"> 
     <p>Code:<input type="text" name="inputtedCode" value="" /></p> 
     <input type="submit" name="submitCode" value="submit" /> 
    </form> 

ASP

'Declares the variable for the random number which will be sent and stored 
    Dim uniqueCode 

    'initialising the randomize generator 
    Randomize() 

    'genarating a random number between 1 and 1000 
    uniqueCode = CInt(Int((1000*Rnd()) + 1)) 

    'writing it out for testing purposes 
    response.write(uniqueCode) 

    'store it as a session to save the code when the form is submitted 
    Session("generatedCode") = uniqueCode 
    Session.Timeout= 1 

回答

1

我觉得你的主要问题是,会话超时后(标准20分钟)你没有访问发送随机数因为你只将它存储在会话变量中。

现在,用户访问您的网页的唯一方法是当他在会话超时值内收到带有“代码”的电子邮件并在此期间重新访问您的网站时。

您必须以其他方式(例如数据库)保留该号码和相应的电子邮件地址。

如果你真的想要实现它,你描述你的解决方案的方式是检查表单字段“输入”值是否存在。如果存在,则不得生成随机数。

if request.form("inputted") <> "" and isnumeric(request.form("inputted")) and request.form("inputted") >= 1 and request.form("inputted") <= 1000 then 
    if request.form("inputted") = Session("generatedCode") then 
     ' the session value and the inputted value are equal but you are not sure that the user you sent the code has entered it in your form! 
    end if 
else 
    'generate random number and store it and send email 
end if 

另一种想法:您是否根据您发送给它的电子邮件地址检查输入的随机数?否则,我可以尝试填写表单并输入一些数字,并访问禁止页面,而不会收到带有“代码”的电子邮件。 1000种可能性并不多。

+0

在过去做了这样的事情,生成了一个[tag:rc4-cipher]加密的id,然后[tag:base64]对它进行了编码,因此可以通过电子邮件进行传输并且可以在URL中使用而不会被破坏,会话值刚刚获得不要削减它。 – Lankymart

+0

它最初是一个8位数字,但我希望它是用户友好的我现在意识到这一点,我打算使用一个生成器,使用字母表中的所有字母,然后数字1-9生成一个10位数的唯一代码:) – Kieranmv95

1
If request.form("submitEmail") = "submit" then 
'## Your code for generate random number 
Else 
'## Your code for verify previously generated random number 
End If