بانک کد asp

1. دوستانی که کد جالب و بدرد بخوری دارند ( هرچند ساده ) به همین شکلی که در زیر آمده است ( عنوان٬ مورد استفاده ٬ فایل‌های مورد نیاز ٬ توضیحات٬ نمونه کد + ترکیب رنگی استفاده شده و خطوط بین هر موضوع ) کد مربوطه را در همین تاپیک ارسال کنند .
2. درصورتی که کد معرفی شده از سایتی برداشت شده لطفاً لینک سایت مربوطه را در ادامه پست‌تان قرار دهید .

موفق و پیروز باشید



تاریخ هجری شمسی


فایل‌های مورد نیاز : Shamsi.asp
توضیحات : تا اونجایی که یادمه مشکل سال کبیسش حل شده
نمونه کد :
کد:
<%
FMonArray= array (0,31,31,31,31,31,31,30,30,30,30,30,30)
EMonArray= Array(0,31, 28,31,30,31,30,31,31,30,31,30,31)
 
W = Array  ("یک‌شنبه", "دوشنبه", "سه‌شنبه", "چهار‌شنبه", "پنج‌شنبه", "جمعه", "شنبه")
Mon = Array ("فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند")
EYear= Year(Date)
EMon= Month(Date)
EDay = Day(Date)
 
ELeap=0
 
if ((EYear mod 4))= 0  Then
    ELeap =1
End if
 
Cnt=EMon-1
Temp=0
While Cnt<>0
if ((Cnt=2)and(ELeap=1)) Then
    Temp= Temp+29
else
    Temp= Temp + EMonArray(Cnt)
end if
Cnt=Cnt-1
Wend
 
EDayOfYear= Temp+EDay
' Convert to Farsi
 
Temp= EDayOfYear-79
if Temp>0 Then
    FYear= EYear-621
else
    FYear= EYear-622
 
    if ((FYear mod 4)=3) then
        Temp= Temp+366
    else
        Temp= Temp+365
    End if
End if
 
if (FYear mod 4)=3  Then
    FLeap=1
else
    Fleap=0
End if
 
Cnt= 1
 
While( (Temp<>0) and (Temp>FMonArray(Cnt)) )
if Cnt=12 Then
    if (FLeap=1) Then
        Temp=Temp-30
    else Temp= Temp-29
    end if
else  Temp= Temp-FMonArray(Cnt)
end if
 
Cnt= Cnt+1
Wend
 
if Temp<>0 Then
    FMon = Cnt
    FDay= Temp
else
    FMon= 12
    FDay=30
End if
 
    DateShamsi = W(WeekDay(Date) - 1) & " " & FDay& " " & Mon(FMon - 1) & " " &FYear
%>
<%
    response.write(DateShamsi)
%>

نمایش تصادفی یک خط از فایل


مورد استفاده : نمایش Tip های مختلف در صفحه
فایل‌های مورد نیاز : text.txt, rndline.asp
توضیحات :
1. در این مثال فایل text.txt می‌بایست شامل 20 خط باشد .
2. پوشه‌ی ذخیره‌ی این فایل باید اجازه ( permission ) لازم را برای اعمال تغییرات داشته باشد .
کد نمونه :
کد:
<%
Randomize
' Number of 1-line entries in the text file. 21 here
RandNo = Int(Rnd*20)
 
' Now open the file with the 20 entries
userFile = "text.txt"
userFile=Server.Mappath (userFile)
Set fs = CreateObject("Scripting.FileSystemObject")
Set thisfile = fs.OpenTextFile(userFile, 1, False)
 
 
' If you wish to skip lines, enter that number here
For i = 1 to RandNo -1
      thisfile.SkipLine()
Next
 
' Now assign the variable RandomLineItem to the line selected
RandomLineItem = thisfile.ReadLine
Set fs = Nothing
 
' Display the Random Line
Response.write RandomLineItem
%>


روز شمار


فایل‌های مورد نیاز : countdown.asp
کد نمونه :
کد:
<%
dim strDateTime
strDateTime =  CDate("2/9/2000")
strFutureDay = #12/31#
Response.write "There are" & INT(strFutureDay - strDateTime) & " more days till December 31st."
%>

بدست آوردن IP Address کاربر


مورد استفاده : ثبت آی پی و استفاده از آن برای جلوگیری از ثبت بیش از یک رأی در نظرخواهی
فایل‌های مورد نیاز : ip.asp

نمونه کد:
کد:
آی پی شما :  <%=Request.ServerVariables("REMOTE_ADDR")%>

بدست آوردن نام فایلها


مورد استفاده : به هنگامی که نیاز داشته باشید نام فایل‌های موجود در یک پوشه را بدانید .
فایل‌های مورد نیاز : filename.asp
توضیحات :
برای استفاده از نمونه کد زیر می‌بایست پوشه‌ای با نام MyFolder در پوشه‌ی جاری ایجاد نمایید .
نمونه کد:
کد:
<%
Set MyDirectory=Server.CreateObject("Scripting.FileSystemObject")
Set MyFiles=MyDirectory.GetFolder(Server.MapPath("MyFolder"))
For each filefound in MyFiles.files
      Response.write filefound.Name
      response.write "<br>"
Next
%>

شمارنده‌ی دفعات مشاهده‌ی یک صفحه


مورد استفاده : ذخیره‌ی آمار مشاهده‌ی یک صفحه
فایل‌های مورد نیاز : counter.asp , asp_count.txt
توضیحات :
پوشه‌ی ذخیره‌ی این فایل باید اجازه ( permission ) لازم را برای اعمال تغییرات داشته باشد .
نمونه کد :

کد:
<%
on error resume next
 
' Create a server object
set fso = createobject("scripting.filesystemobject")
 
' Target the text file to be opened
set act = fso.opentextfile(server.mappath("asp_count.txt"))
 
' Read the value of the text document
' If the text document does not exist then the on error resume next
' will drop down to the next line
counter = clng(act.readline)
 
' Add one to the counter
counter = counter + 1
 
' Close the object
act.close
 
' Create a new text file on the server
Set act = fso.CreateTextFile(server.mappath("asp_count.txt"), true)
 
' Write the current counter value to the text document
act.WriteLine(counter)
 
' Close the object
act.Close
 
' Write the counter to the browser as text
Response.Write counter
%>

شمارنده‌ی تعداد جلسات استفاده از سایت


مورد استفاده : ذخیره‌ی آمار مشاهده‌ی سایت
فایل‌های مورد نیاز : session_counter.asp , session_count.txt
توضیحات :
پوشه‌ی ذخیره‌ی این فایل باید اجازه ( permission ) لازم را برای اعمال تغییرات داشته باشد .
نمونه کد :
کد:
<%
set fso = createobject("scripting.filesystemobject")
set act = fso.opentextfile(server.mappath("session_count.txt"))
counter = clng(act.readline)
if session("been_here_before") = "" then
   session("been_here_before") = "Yes"
   counter = counter + 1
' act.close
Set act = fso.CreateTextFile(server.mappath("session_count.txt"), true)
act.WriteLine(counter)
end if
act.Close
Response.Write counter
%>

شمارنده‌ی تعداد دفعات مراجعه به یک صفحه در روز


مورد استفاده : ذخیره‌ی آمار مشاهده‌ی روزانه‌ی یک صفحه
فایل‌های مورد نیاز : dailyhits.asp
توضیحات :
پوشه‌ی ذخیره‌ی این فایل باید اجازه ( permission ) لازم را برای اعمال تغییرات داشته باشد .
نمونه کد :
کد:
<%
on error resume next
 
' Create a server object
set fso = createobject("scripting.filesystemobject")
 
' Target the text file to be read.
' The text file is continually updated with the current date from the server
set act = fso.opentextfile(server.mappath("daily_count-"& month(date()) & day(date()) & year(date())&".txt"))
 
' Read the value contained in the current day hit counter
' If there is no file for the current day the on error resume next command above
' will force the program to the next line
counter = clng(act.readline)    
 
' Add one to the counter.  If there was no value the counter will be set to a value of one
counter = counter + 1    
 
' Close the text object.
act.close
 
' Create a new text file on the server with the current date as part of the name
Set act = fso.createtextfile(server.mappath("daily_count-"& month(date()) & day(date()) & year(date())&".txt"), true)
 
' Write the counter value to the text object
act.writeline(counter)    
 
' Close the text object
act.Close  
 
%>
<%= counter %> Total Hits for <%= date() %>


فرستادن ایمیل


کد:
<%@ LANGUAGE="VBSCRIPT" %>
<!-- remove this comment and end comment below before using 
<html>
<head>
<title>Simple Form E-mail Component for ASP</title>
</head>
<body bgcolor="#FFFFFF">
<h1>Form E-mail with ASP<br></h1>
<form method="POST" action="SENDFORM.asp">
  <p>From: <input type="text" name="From" size="40"><br>
  Email To: <input type="text" name="EmailTo" size="38"><br>
  Subject: <input type="text" name="Subject" size="55"></p>
  <p>Message<br>
  <textarea rows="6" name="Message" cols="55"></TEXTAREA><P>
  <p>Other: <input type="text" name="AnyFieldNameYouWant" size="49"></p>
  <p><input type="submit" value="Send Form" name="Submit"><input type="reset"
  value="Reset" name="Reset"></p>
</form>
<%  
'************************************************************
' SENDFORM.ASP -- Simple Form E-mail Component for ASP
' Created 7/31/98 
' by Valentin Frixione
' e-mail: [EMAIL="[email protected]"][COLOR=#22229c][email protected][/COLOR][/EMAIL]
' -- Must have Microsoft Windows NT 4 Server
' -- running Option Pack 4, with IIS4 & simple SMTP server 
'************************************************************
If Request.Form("Submit")= "Send Form" then
  Set objSend = Server.CreateObject("CDONTS.Newmail")
   objSend.From = Request ("From")
  objSend.To = Request ("EmailTo")
   objSend.Subject = Request ("Subject")
'--------------------------------------------------
' Putting the Body of the e-mail together...
'--------------------------------------------------
  strBodyHeader= "This form was sent via e-mail on " _
  & Now & ". " & "<P>"
'---------------------------------------------------------------'Collecting the fields from the HTML Form
'--------------------------------------------------------------
  strBody =""
  For each item in Request.Form
    if item <> "Submit" then
      strLineItem = item &" :  " & _
      Request.Form(item)  & "<BR>"
      strBody = strBody & strLineItem
   end if
 Next
 
 strBody = strBodyHeader & strBody
 
 objSend.Body = strBody
'--------------------------------------------------
'Sending the Form
'--------------------------------------------------
 
  objSend.Send
 
  If err.number = 0 then
    'OK?    
    Response.Write "<p><strong>"
    Response.Write "Your message was sent. "
    response.write "It was:</strong><br><P>" & strBody
  else
   'Not OK!
     Response.Write "<p><strong>A problem was detected, please "
     Response.Write "contact the Webmaster with the following "
     Response.Write "error description.</strong><br>"
     Response.Write "Error:  " & objSend.Response 
  End if
set objSend = Nothing
End If
%>
remove end comment and comment on top before using
  -->
</BODY>
</HTML>

کدی جهت نمایش تعداد کاربران online

برای نمایش بازدیدکنندگان این لحظه سایت خود کافی است فایل Global.asa خود را به شکل زیر تنظیم کنید:
کد:
<SCRIPT LANGUAGE="VBScript" RUNAT="Server"> 

Sub Application_OnStart 
   application("activevisitors") = 0 
End Sub 

Sub Application_OnEnd 
End Sub 

Sub Session_OnStart 
   application.lock 
      application("activevisitors") = application("activevisitors") + 1 
   application.unlock 
End Sub 

Sub Session_OnEnd 
   application.lock 
      application("activevisitors") = application("activevisitors") - 1 
   application.unlock 
End Sub 

</SCRIPT>

حال کافی است در هر صفحه ای که می خواهید تعداد کاربران Online را نمایش دهید. کد زیر را قرار دهید:
کد:
<%=application("activevisitors")%>
 
تصویر تصادفی
کد:
<% dim array(9)
randomize
array(1)="rdm1.gif"
array(2)="rdm2.gif"
array(3)="rdm3.gif"
array(4)="rdm4.gif"
array(5)="rdm5.gif"
array(6)="rdm6.gif"
array(7)="rdm7.gif"
array(8)="rdm8.gif"
array(9)="rdm9.gif"
array(0)="rdm0.gif"
upperbound=ubound(array)
lowerbound=0
lrandom = Int((upperbound - lowerbound + 1) * rnd + lowerbound)
response.redirect "images/" & (Array(lrandom))
%>
 
اتصال به بانک اطلاعاتی اکسس
نمونه کد :
کد:
<%
set conn = server.CreateObject("ADODB.Connection")
conn.Open= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & server.MapPath("DB.mdb")
Set rs = conn.Execute("Select * from table")
%>
 
اتصال به بانک اطلاعاتی MS SQL Server


توضیحات : برای اتصال به بانک اطلاعاتی از نوع MS SQL Server از یکی از روش‌های زیر استفاده کنید .
نمونه کد :
1. استفاده از OLE DB
کد:
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open "Provider=SQLOLEDB; Data Source=YOUR_SERVER_NAME; Initial
Catalog=your_database_name; User ID=your_username; Password=your_password"
objConn.Close
Set objConn = Nothing

2. استفاده از SQL Driver
کد:
 Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open "Driver={SQL Server};" & _ 
"Server=YOUR_SERVER_NAME;" & _ 
"Database=your_database_name;" & _
"Uid=your_username;" & _ 
"Pwd=your_password;" 
objConn.Close
Set objConn = Nothing
 

جدیدترین ارسال ها

بالا