ASP入門(九) Request物件小案例

2021-09-08 11:14:34 字數 2434 閱讀 5351

我們將製作乙個能夠記住訪問者姓名的頁面,在這個小案例中,你將學會如何使用request物件的cookies、form以及servervariables集合的值,還可以學習到如何使用response物件來傳送cookies。

首先來看看程式**:

@language="

vbscript

"codepage="

65001

"%>

doctype html

>

<

html

>

<

head

>

<

meta

charset

="utf-8"

>

<

title

>用cookies記住訪問者的姓名

title

>

head

>

<

body

>

dimsusername

susername

=trim

(request.cookies(

"name"))

'判斷name是否為空,不為空則輸出name的值

ifsusername =""

then

'判斷是否是post剛提交了表單,是的話則獲取表單內容輸出cookies

ifucase

(trim

(request.servervariables(

"request_method

"))) ="

post

"then

susername

=trim

(request.form(

"name"))

response.cookies(

"name")

=susername

response.cookies(

"name

").expires

=dateadd("

d", 1

, now

)

'cookies一天後過期

response.write(

"我已經記住您的姓名了!")

else

'否則顯示表單,讓使用者提交表單

%>

<

form

method

="post"

action

="">

請告訴我您的姓名 :

<

input

name

="name"

type

="text"

/>

<

input

type

="submit"

value

="提交"

/>

form

>

endif

else

response.write(

"您好,"&

susername)

endif

%>

body

>

html

>

首次執行,無法獲取 cookies 資訊,顯示表單,讓使用者提交,如下圖:

提交表單,還是post到當前asp頁面,因為還是無法獲取cookies,所以顯示提交表單成功的頁面,如下圖:

再次重新整理當前頁面 requestcookies.asp ,因為可以獲取到cookies,直接顯示訪問者姓名了。

我們再詳細講解下讓使用者輸入姓名並儲存的部分,首先獲取servervariables變數request_method的值,這個值標識了當前頁面的請求方式,如果是post方式,說明現在正在想此頁面提交表單,這時就要獲取表單的值,並且將其使用response.cookies集合來向客戶端輸出cookies,否則顯示供使用者填寫姓名的html**。

trim函式刪除字串兩邊的空格,ltrim函式刪除字串左側空格,rtrim函式刪除字串右側空格。

ucase函式把指定字串轉化為大寫,lcase函式把指定字串轉化為小寫。

requestcookies.rar

asp內建之 Request物件

本為網路收集內容 解釋 如果客戶端使用了 伺服器,使用request.servervariables http x forwarded for 得到ip位址,如果沒用使用 伺服器,得到的是 則用request.servervariables remote addr 得到ip位址.request.se...

asp內嵌物件說明 Request

目前的 asp 版本總共提供了六個內建物件之一 request 可以使用 request 物件訪問任何基於 http 請求傳遞的所有資訊,包括從 html 用 post方法或get方法傳遞的引數 cookie 和使用者認證。request 物件使您能夠訪問客戶端傳送給伺服器的二進位制資料。語法 re...

ASP入門(十二) Application物件

website dim website website website response.write website 下面的例子,lock 方法可以防止乙個以上使用者訪問變數 visits,用 unlock 方法來對已鎖定的物件進行解鎖,這樣下乙個使用者就可以增加變數 visits 的值。visit...