Archive for December, 2004

A tale of two web servers

Thursday, December 30th, 2004 | Life

mworld.us is back baby! After the Audio Senate revivial on December 6th (turns out Nerd Federations track back system isn’t what I thought it was 😉 I didn’t think a revival could get much sweeter. But now M World is back it is a great feeling. Even if it did take so many hours.

To begin the story, I renewed by web hosting back at the end of October. My web host, onedollarhost.net, didn’t seem to notice though and deleted everything. Files, databases, apparently they don’t even keep backups. Hmm, so as you can image I wasn’t happy. M World has like a hundred sub-sites so I can’t just upload them all again.

Anyway I emailed them several times asking if they had backups and if so use them to restore the files from that. The response I got over and over was:

“Files were deleted. You will need to re-upload the files.”

Another thing I wanted from that host was stats as I had none. It is a shame I didn’t as it was quite a high traffic site with over 6,000 pages in Google so I would be interested to see what the traffic was like. That is no longer a problem though, as I took the decision to move over to my new Windows host.

I have been with them ages although I didn’t want to move M World because of the data loss (and there has been a lot of data loss). Still it didn’t seem to matter now, so I moved it over and so from now on I will be able to track everything from Awstats. There is some good in every situation I guess.

I have also spend hours last night and most of today putting together all of the sub-sites. I found all but one although there may be a load not listed. Still, I have put all the others online and with no content, hopefully M World will eventually regain it’s former glory.

So although it has not been a good experiance – two months of missing files, most of it is back online now. There are still a few bits I keep remembering but overall, I think M World can now move forward, safe on a new server.

Find your IP address in Windows

Thursday, December 30th, 2004 | Life, Tech

To find out your ip address:

Select start and click run.

Type “command” and click ok.

Once the command promt appears, type ipconfig and press return. This will then bring up a list of different ip address’s. You will usually be looking for the port you connect to the internet through.

Removing fonts to boot Windows quicker

Thursday, December 30th, 2004 | Life, Tech

When you first boot Windows up, all the fonts must be loaded. If you delete or move unwanted fonts, Windows will load up quicker.

Prevent applications launching automatically on Windows boot up

Thursday, December 30th, 2004 | Life, Tech

If you don’t want all your programmes to automatically boot up when Windows starts, hold control key down to skip loading them.

Stop Windows automatically launching applications on boot

Thursday, December 30th, 2004 | Life, Tech

Sometimes you will have applications running when Windows starts up and some which you may not even be aware of. If you can’t find the application in Start > Programs > Start Up or you just want to see what exactly is running then try your MS Config box.

Goto Start > Run
Type ‘msconfig’ into the box
Hit the ok button
Wait for the box to load
Click the start up tab

This displays a list of all the applications that run of start up. Many are Windows applications which should generally be left alone. You can disable them from starting up by unticking the box next to it.

A simple permissions system in ASP

Wednesday, December 29th, 2004 | Programming, Tech

Building an admin log in system is pretty simple for s small site. You don’t need a username so a password is acceptable for log in and you can writee your password into the code. However because it is an admin section it needs to be fairly secure – no having the password available for any old joe to find in the source code.

The solution – a simple server side script. The password will be in the code but because it is server side is will never reach the end user and so they cannot get hold of it.

For this you will need three pages. A main page, a log in and page and log out page.

index.asp
login.asp
logout.asp

Because we need it to be fairly secure I am going to use a session cookie for the password. Let’s start with the main page.

<%
If Session("adminpassword") <> "dog" Then
Response.Redirect ("login.asp")
End If
%>

<html>
<head>
<title>Admin Homepage</title>
</head>

<body>
<p>Welcome to the admin secction.</p>
</body>
</html>

For this I have chosen the pasword “dog.” If this is not present in a session cookie called adminpassword, the user will be redirected to login.aso. Lets look at that now.

<html>
<head>
<title>Admin Log In</title>
</head>
<p>Please enter the admin password:</p>
<form action="login.asp" method="post" name="login_form" id="login_form">
<input name="password" type="password" id="password" size="40">
<input type="submit" name="Submit" value="Log In">
</form>
</body>
</html>

The first thing I have done is to add a form called “login_form” to allow the user to log in. In the form I have placed a text field called “password” and a submit button so they can type in the password and click submit to log in. This sends the user and the form variable, “password” to login.asp (the same page but reloaded). Now we need to add some server side scripting to the top of the game above the <html> tag.

<%
' checks to see if the password has been submitted
If Request.Form ("password") <> "" Then
' it has so writes in the session cookie
Session ("adminpassword") = Request.Form("password")
' if the user's password is correct they should now be able
' to gain access to the main page
' if they entered an incorrect password they will be
' redirected back here. Because the form variable
' is not sent when they are redirected back here
' they will not be redirected back to index.asp
Response.Redirect ("index.asp")
%>

If the password is incorrect, eg, they entered a password that is different from “dog” eg they entered “cat” it will still be saved in the session variable and the user will still be redirected to index.asp, but because the password is not “dog” they will be redirected back here again.

The fact that the incorrect password is saved in the session cookie allows us to give the user some more information when they are redirected back to login.asp because index.asp won’t give them access.

<%
' if the user is on login.asp but still has a password
' in the session cookie, they must have entered
' and incorrect password
If Session ("adminpassword") <> "" Then
%>
<p>You entered an incorrect password.</p>
<%
End If
%>

You can then insert the script that we placed at the top of index.asp to all the pages you want protecting. To save yourself having to change the script on every protected page when you want to change the password, you could also save the script, by itself, in a separate file and use file include to all the pages you want protecting.

<!--#include file="passwordcheck.asp" -->

You can then just update the script in passwordcheck.asp and all the protected pages would now use the new password.

Finally we need to create a log out page for the user to logout, to stop anyone else getting in after the user is done. This maybe not be needed if you are on a home pc which nobody else has access to but you might want to build one anyway. The log out page is amazingly simple.

<%
Session.Abandon ()
Response.Redirect ("index.asp")
%>

This should log the user out. If the user has not been logged out for some reason they will know because they will gain access to index.asp when they are redirected to it. If the user has been logged out successfully, index.asp will redirect them to login.asp and so they will know they have been logged out.

Now just o make it easier on you I will include the full source code including links, html and asp code, ready for you to copy and paste into your text editor and save as the appropriate files.

index.asp

<%
If Session("adminpassword") <> "dog" Then
Response.Redirect ("login.asp")
End If
%>

<html>
<head>
<title>Admin Homepage</title>
</head>

<body>
<p>Welcome to the admin secction.</p>
<p><a href="logout.asp">Click here to log out.</a></p>
</body>
</html>

login.asp

<%
' checks to see if the password has been submitted
If Request.Form ("password") <> "" Then
' it has so writes in the session cookie
Session ("adminpassword") = Request.Form("password")
' if the user's password is correct they should now be able
' to gain access to the main page
' if they entered an incorrect password they will be
' redirected back here. Because the form variable
' is not sent when they are redirected back here
' they will not be redirected back to index.asp
Response.Redirect ("index.asp")
%>

<html>
<head>
<title>Admin Log In</title>
</head>
<p>Please enter the admin password:</p>
<form action="login.asp" method="post" name="login_form" id="login_form">
<input name="password" type="password" id="password" size="40">
<input type="submit" name="Submit" value="Log In">
</form>
</body>
</html>

logout.asp

<%
Session.Abandon ()
Response.Redirect ("index.asp")
%>

A simple hit counter in ASP

Wednesday, December 29th, 2004 | Programming, Tech

This tutorial will show you how to build a simple hit counter. It does not use any SQL or databases; it stores the hits in a text file.

Allou need to create for this script is your ASP file and a text file. In the text file, simply enter the number 0 and save it in the same directory as count.txt. Take a look at the basic source code.

<%@ Language="VBScript" %>
<% Response.Expires= -1
Response.AddHeader "Cache-Control", "no-cache"
Response.AddHeader "Pragma", "no-cache" %>
<%
if Session("ct") = "" then
fp = Server.MapPath("db\count.txt")
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.OpenTextFile(fp)
ct = Clng(a.ReadLine)
ct = ct + 1
Session("ct") = ct
a.close
Set a = fs.CreateTextFile(fp, True)
a.WriteLine(ct)
a.Close
Set a = Nothing
Set fs = Nothing
else
ct = Clng(Session("ct"))
end if 
%>

Now lets break it down into three sections.

<%@ Language="VBScript" %>

This just states that the page is a VB script page.

<% Response.Expires= -1
Response.AddHeader "Cache-Control", "no-cache"
Response.AddHeader "Pragma", "no-cache" %>

This section stops the user refreshing the page to clock up hits.

<%
if Session("ct") = "" then
fp = Server.MapPath("count.txt")
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.OpenTextFile(fp)
ct = Clng(a.ReadLine)
ct = ct + 1
Session("ct") = ct
a.close
Set a = fs.CreateTextFile(fp, True)
a.WriteLine(ct)
a.Close
Set a = Nothing
Set fs = Nothing
else
ct = Clng(Session("ct"))
end if 
%>

This is the main section which adds the hits.

fp = Server.MapPath("count.txt")

This tells the server where to find the file. You can modify the file location by changing count.txt. So for instance if you wanted to to be called hitcounter.txt and in the directory db you would use:

fp = Server.MapPath("db\hitcounter.txt")

All you have to do is alter the file path in the quote marks.

Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.OpenTextFile(fp)
ct = Clng(a.ReadLine)

This section opens the file using FileSystemObject and reads the first line. It then sets the variable ct to the amount of hits it has already had.

ct = ct + 1

This line adds one hit to the total number of visitors.

Session("ct") = ct
a.close

This part saves a session variable as the new click through with the new amount of visitors and closes the text file.

Set a = fs.CreateTextFile(fp, True)
a.WriteLine(ct)
a.Close

This code creates a new text file over the old one and adds in the new amount of visitors to it. Then it closes the text file.

Now you have a working hit counter. All you need to do is add the hit counter into your page:

You are a visitor number <%=ct%>!

This would display the amount of visitors. Now to save confusion, here is the full source code for the page:

<%@ Language="VBScript" %>
<% Response.Expires= -1
Response.AddHeader "Cache-Control", "no-cache"
Response.AddHeader "Pragma", "no-cache" %>
<%
if Session("ct") = "" then
fp = Server.MapPath("db\count.txt")
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.OpenTextFile(fp)
ct = Clng(a.ReadLine)
ct = ct + 1
Session("ct") = ct
a.close
Set a = fs.CreateTextFile(fp, True)
a.WriteLine(ct)
a.Close
Set a = Nothing
Set fs = Nothing
else
ct = Clng(Session("ct"))
end if 
%>
<html>
<body>
You are a visitor number <%=ct%>!
</body>
</html>

ASP Request function

Wednesday, December 29th, 2004 | Programming, Tech

The request function, surprisingly, requests a value. This could be a URL variable, form variable, or some other value. For example if you were at the url.

http://www.somepage.com/home.asp?ID=23

You would use the following code in your script.

Request.QueryString("ID")

The line would then return the value 23.

An example of where this can be used is on the M World News channel. The news is stored dynamically in a database so when you click on a link to the full story from the news homepage you are taken to a dynamic page which selects the story based on the ID of the story you want passed to the page as a url variable. For example the link could be as follows.

http://www.mworld.us/news/story.asp?ID=52

Examples of use

Request.QueryString("SomeURLVariable")
Request.Form("SomeFormElement")
Request.Cookies("SomeCookie")

Using Server.MapPath in ASP

Wednesday, December 29th, 2004 | Programming, Tech

Server.MapPath is used to create a file path from a virtual path.

Here’s a typical DNS-less database connection:

Connection = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=C:\wwwroot\users\someaddress\db\data.mdb"

This isn’t much good though if you don’t know the full file path and if you are using a web host the chances are that you won’t know it. So you can use Server.MapPath instead.

Lets say that database is on the internet at the following address. Not that it is a good idea to have your database web accessible of course.

http://www.someaddress.com/db/data.mdb

Instead of having to use the file path as the top you could use a virtual path:

Connection = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" + Server.MapPath("/db/data.mdb")

This would get the server to “map” a file path to the database from the virtual path you give it. Notice that after DBQ= the tag (“) is closed as you don’t want the server to think + Sever.Map… is part of the file path.

Google Adsense actually making money?

Wednesday, December 29th, 2004 | Life

Well I appear to have lost all my channels on Google Adsense. None of them display any data when I select a channel to view, which really sucks as I had them all configured to different websites. Still, some good has come of my long term chance that I took in the program.

Although I can’t say how much I earned per click (due to the terms and such), my income yesterday was awesome. Well not mega amounts but let’s say like 10, 20 times what I normally could. Could have been a fluke but I really hope not. I added the code to a new site yesterday so I think that is the reason.

If so then I can finally understand why everyone likes Adsense. I must have been running it like forever now and never really made anything substantial although this changes things. I earned more off Adsense yesterday than I did using Mamma, which has traditionally been a far, far bigger earner.

Still, that said, that hasn’t earned much either recently. I am getting a good rate but over 50% of the ads are unsold – most of the ads I display for them are default redirects back to my own ads.