Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Link checking 1

Status
Not open for further replies.

craigey

Technical User
Apr 18, 2002
510
GB
Hi All,

How can I modify the 3 pages shown below to read from a database that contains the names of several websites. And then to output all the results to the checkdone.asp page. So that it would display all 8 results. EG.

WEBSITE STATUS

OK
OK
OK
Failed
OK
OK
OK
OK

Also how would i modify the code to e-mail the page name and status if it has failed.

Code:
    '**************************************
    ' Assumes:There are 3 pages:
    check.asp To post the url and receive the result of the check.
    checkurl.asp this page will Do the check
    checkdone.asp; this page will post the results back To the referer.
    '
    'This code is copyrighted and has    ' limited warranties.Please see [URL unfurl="true"]http://w[/URL]
    '     ww.Planet-Source-Code.com/xq/ASP/txtCode
    '     Id.6585/lngWId.4/qx/vb/scripts/ShowCode.
    '     htm    
    '**************************************


'****** check.asp ****
'part 1: the post form
'*********************
Code:
    <%
    strFrom=request.form(&quot;result&quot;)
    strResult=request.form(&quot;url&quot;)
    %>
    <HTML>
    <HEAD>
    <TITLE>URL check</TITLE>
    </HEAD>
    <BODY>
    <FORM method=&quot;post&quot; action=&quot;checkurl.asp&quot;>
    <P><INPUT type=&quot;text&quot; name=&quot;url&quot;></P>
    <P><INPUT type=&quot;submit&quot; value=&quot;submit&quot; name=&quot;submit&quot;></P>
    </FORM>
    <P><%= strFrom %></P>
    <P><%= strResult %></P>
    </BODY>
    </HTML>

'****** checkurl.asp ****
'part 2: the actual check
'************************
Code:
    <%
    strURL=request.form(&quot;url&quot;)
    if strURL<>&quot;&quot; Then
    if left(lcase(strURL),7)<>&quot;[URL unfurl="true"]http://&quot;[/URL] Then
    strURL=&quot;[URL unfurl="true"]http://&quot;[/URL] & strURL
    End if
    On Error Resume Next
    Dim objHTTP
    Dim sHTML
    Set objHTTP = Server.CreateObject (&quot;Microsoft.XMLHTTP&quot;)
    objHTTP.open &quot;GET&quot;, strURL, False
    objHTTP.send
    sHTML=objHTTP.statusText
    if err or sHTML<>&quot;OK&quot; Then
    sTxt=&quot;fail&quot;
    else
    sTxt=&quot;ok&quot;
    End if
    Set objHTTP=nothing
    else
    sTxt=&quot;fail&quot;
    End if
    strFrom=request.ServerVariables(&quot;HTTP_REFERER&quot;)
    p=instr(1,strFrom,&quot;?&quot;)
    if p>0 Then
    strFrom=left(strFrom,p-1)
    End if
    response.redirect &quot;checkdone.asp?result=&quot; & sTxt & &quot;&ref=&quot; & strURL & &quot;&return=&quot; & strFrom
    %>

'****** checkdone.asp ****
'part 3: post the results back to the re
' ferer
'************************
Code:
    <%
    strRes=request.querystring(&quot;result&quot;)
    strRef=request.querystring(&quot;ref&quot;)
    strRet=request.querystring(&quot;return&quot;)
    %>
    <HTML>
    <HEAD>
    </HEAD>
    <SCRIPT>
    function postit(){
    myform.submit();
    }
    </SCRIPT>
    <BODY onLoad=&quot;postit()&quot;>
    <FORM method=&quot;post&quot; action=&quot;<%= strRet %>&quot; name=&quot;myform&quot;>
    <INPUT type=&quot;hidden&quot; name=&quot;result&quot; value=&quot;<%= strRes %>&quot;>
    <INPUT type=&quot;hidden&quot; name=&quot;url&quot; value=&quot;<%= strRef %>&quot;>
    </FORM>
    </BODY>
    </HTML>

Please please help!

Thanks!
 
If you want it to read from the database, open a database connection and pull the fields that list the URL's. (say the field name is &quot;url&quot;).

Then, use a DoWhile loop to cycle through and check each record in the table listing the URL and check each url.

You don't have to go to another page to show the results, you'd just print the URL and then ok or fail.

Do While Not rs.EOF
Response.write(&quot;URL: &quot; & rs(&quot;url&quot;))
If check = &quot;ok&quot; Then
Response.write(&quot;&nbsp;&nbsp;OK<BR>&quot; & vbCrLf)
Else
Response.write(&quot;&nbsp;&nbsp;FAIL<BR>&quot; & vbCrLf)
End If
rs.MoveNext
Loop


Does this make sense? -Ovatvvon :-Q
 
OK. The loop seems to make sense to me, but I'm not quite too sure how to connect to a database and read/write to/from it.
I'm guessing the Response.write(&quot;URL: &quot; & rs(&quot;url&quot;)) part of the above is reading from the database. But how do I write the result to it for it to read the result.

Sorry to be a pain, but I'm very new to ASP. How would I incoporate the loop into the code that I've already got? Also how would I incoporate the reading/writing of the database as well?

I'm really greatful for all the help you have given so far, but I don't suppose you could post the revised code. Please.

For helping me this far I have given you a star and if it was possible I would give you another star because I apreciate all your help and hardwork.

Thanks.


 
First off, if you want to learn ASP, get a book like &quot;SAM's teach yourself Active Server Pages in 24 Hours&quot; or somthing like that. WROX are good books as well ( where you can sample their books online.

Additionally, you can learn more about connecting to databases at this FAQ which was written by Link9... faq333-618

The FAQ area is a good place to get answers to Requently Asked Questions. May answer some of yours, and additionally teach you some new stuff you didn't know about.

On with your code:


If you simply post this code into a page (say: default.asp)...all you need to do is create a database using microsoft access. Create an AUTONUMBER field called somthing like &quot;urlPK&quot;, then create a text field called &quot;url&quot;.

Enter as many web addresses as you want...you could make 5 records if you want or so for testing the page. Then just hit the page in your browser and you'll see it at work.

Hope this helps...


*******************
*** default.asp ***
*******************

<% @ Language=VBScript %>
<% Option Explicit %>
<%
'Instead of retrieving the URL from the last page....you want
'to read it out of the database. So don't request it from the
'last page...instead, create a DB connection and read it from
'the DB.

'Declare the variables
Dim conn, connString, rs, sql

'Set the value to the sql variable to pull the data you want
'from the DB. Where &quot;myTable&quot; is the name of the table in
'your DB that contains the URL's. * is a wildcard to
'select all data fields.
sql = &quot;SELECT * FROM myTable;&quot;

'Set the connection object used to connect to the DB
Set conn = Server.CReateObject(&quot;ADODB.Connection&quot;)

'Set the driver to be used and declare where the DB is on the
'harddrive. (Driver will differ depending on the DB you use.
'IF you are using an MS Access DB, here is what it'd look like...
connString = &quot;DRIVER={Microsoft Access Driver (*.mdb)};&quot; & _
&quot;DBQ=D:\Inetpub\
'Open the DB connection
conn.Open connString

'Set the recordset object to hold the recordset retrived from the DB
Set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)

'Open the recordset and executing the sql statement to be held in the
'recordset. Says, open rs (which is the recordset object), execute
'the sql variable's value, using the conn object (which is the
'connection to the DB object), and the following numbers you can find
'out more about by reading the FAQ that I posted the link to above.
rs.Open sql, conn, 3, 3


'Now your DB is open. Let's start pulling data from it and cycling
'through it.
'declare other variables
Dim strURL, objhttp, sHTML, sTxt, strFrom, p


If not rs.EOF Then
Do while not rs.EOF
strURL = rs(&quot;url&quot;)
If left(lcase(strURL),7)<>&quot; Then
strURL=&quot; & strURL
End if
On Error Resume Next
Set objhttp = Server.CreateObject (&quot;Microsoft.XMLhttp&quot;)
objhttp.open &quot;GET&quot;, strURL, False
objhttp.send
sHTML=objhttp.statusText
If err or sHTML<>&quot;OK&quot; Then
sTxt=&quot;fail&quot;
Else
sTxt=&quot;ok&quot;
End if
Set objhttp=nothing
Response.write(strURL & &quot;  &quot;)
If sTxt = &quot;fail&quot; Then
Response.write(&quot;<font color='red'>&quot; & sTxt & &quot;</font><BR>&quot; & vbCrLf)
Else
Response.write(&quot;<font color='blue'>&quot; & sTxt & &quot;</font><BR>&quot; & vbCrLf)
End If
rs.MoveNext
Loop
Else
Response.write(&quot;There were no URL's listed in the Database.&quot;)
End If

%> -Ovatvvon :-Q
 
That's excellent. Exactly what I was after. I'll be sure to check out the links and the FAQ's as suggested. BTW thanks for the very informative comments.

Once again excellent work, I'm very pleased!
 
Whoops.....Craigey...I made a mistake. I can't believe I forgot this...

You need to close the database connection and destroy the objects. like so...


.....


If sTxt = &quot;fail&quot; Then
Response.write(&quot;<font color='red'>&quot; & sTxt & &quot;</font><BR>&quot; & vbCrLf)
Else
Response.write(&quot;<font color='blue'>&quot; & sTxt & &quot;</font><BR>&quot; & vbCrLf)
End If
rs.MoveNext
Loop
Else
Response.write(&quot;There were no URL's listed in the Database.&quot;)
End If



rs.Close
conn.Close
Set rs = Nothing
Set conn = Nothing

%>

Sorry...kinda upset at myself....that hasn't happened in who knows how long.

Glad I could be of service! :) -Ovatvvon :-Q
 
I'll give the code a go, when I get home and post back the results in about 19 hours from now!
 
OK. As promised........

The code works perfectly, I had to make a minor change. For some reason it didn't like the semicolans
Code:
          If left(lcase(strURL),7)<>&quot;[URL unfurl="true"]http://;&quot;[/URL] Then
              strURL=&quot;[URL unfurl="true"]http://;&quot;[/URL] & strURL
in the above part of the code. So I removed them and all is fine!

Actually I noticed that the TEK-TIPS Forum must have put the semi-colans in itself as when i used the bold TGML TAGS around them and then previewed the post the semi-colans appeared twice. (once in bold and once normal). See below (this is same as above, but without [ignore]
Code:
[/ignore] tags.  You'll notice 2 semi-colans[/color][COLOR=black]

If left(lcase(strURL),7)<>&quot;[URL unfurl="true"]http://&quot;;[/URL] Then
              strURL=&quot;[URL unfurl="true"]http://&quot;;[/URL] & strURL
[/color][COLOR=red]
I think when submitting code you need to use the [ignore][code] tags [/ignore] [/color]

[COLOR=black]
Below is a copy of the revised code, including putting the results into a table.


[b][center][u]once again I thank you for all your help[/u][/b][/center]
 
I know this is an old post, but I noticed you've still got it for e-mail notification.

I would like to know if there is a way to get more info from the page that is being checked? Also is it possible to use a different method to get the information from the page as the XMLhttp is not returning results when I run the script through a proxy/firewall setup. I'am using MSXML 3.0 (if that helps)!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top