Article

Home » Design and Layout » Design Practice » Dr Design - Javascript to PHP

About the Author

Dr. Design

author_drdesign Dr Design answers design and development questions for SitePoint readers. Drop him a line today!

View all articles by Dr. Design...

Dr Design - Javascript to PHP

By Dr. Design

October 12th, 2002

Reader Rating: 6.5

Page: 1 2 3 Next

The waiting room is full! Join the queue -- and the Good Dr. Design will see you next time! But for now, he's rolling up his sleeves and lending a hand to today's patients...

Dream Rollovers

Hi!
Can you tell me why all the rollovers that I make in Dreamweaver don't work in Mac Explorer v.5? Is there any way to make them work? Thank you very, very much...
Amando from Mexico City.

Amando, I haven't looked at Dreamweaver's rollover Javascript. Sometimes simpler is better. I'm quite fond of Andy Mathews (Gravity Digital) simple rollover Javascript, which he posted some time ago on the SitePoint Forums. Perhaps you'll have more luck with this one.

<script language="javascript">
<!--

// Pre-load images
if (document.images) {
 image1on = new Image();
 image1on.src = "images/about_us_on.gif";
 image1off = new Image();
 image1off.src = "images/about_us_off.gif";
}

function changeImages() {
 if (document.images) {
   for (var i=0; i<changeImages.arguments.length; i+=2) {
     document[changeImages.arguments[i]].src =  
     eval(changeImages.arguments[i+1] + ".src");
   }
 }
}

// -->
</script>

Here's an example of its use in HTML:

<a href="about_us.shtml" onmouseover="changeImages('image1',  
'image1on')" onmouseout="changeImages('image1', 'image1off')">
<img name="image1" src="images/about_us_off.gif" border=0></a>

Tracking the Relatives

Hey Doc,
I've designed and set up a small Website exclusively for family pictures. I'm using the Apache server software on a home PC.

My question is: how can I track the people that come to the site? I have set up a login name and password, which should restrict access... however, as word of mouth gets around in the family I'm sure that I'll get lots of hits from distant relatives, and given that I'm a curious person I would like to know who does log in.

I've set up one login name and password so I can't distinguish the users in this way... I have also, through HTML code, set up a screen to ask for a name and then place a cookie on the user's PC, so that they know how often they've been to the site (and eventually, can see if there are any updates on the site).
Gary

Gary, I know where you're coming from. There's nothing more fun than keeping up with the family gossip and finding out what the relatives are up to. I'd probably opt for a bit of server side scripting in PHP or ASP for this one. However, you may not be using any server-side scripting, so let's see if we can hatch an ingenious plan to perform visitor monitoring from the client-side using Javascript!

The technique I'll use assumes you have a Web log analysis tool, or you're true geek and read over your log files. Check out this list of available log analysis tools. Popular packages that run on Linux servers include Webalizer, Analog and AWStats. You'll also find log analysis programs that will run under Windows.

Now, for the fun...

The technique is a little "edgy". Known as "clear gif spyware" or a "Web beacon", some people resent that online advertisers use this technique to track visitors. But that's exactly what we want to achieve. If you were running a commercial Website and used this method it would be advisable (and in tune with online privacy ethics) to disclose in your privacy policy that you track visitors using cookies.

A "Web beacon" uses Javascript to append a query string onto the address of a clear (transparent 1x1 pixel) gif, which is loaded into the Web page. The query string will be ignored by the Web server when it serves up the clear gif, except that the HTTP GET request will be logged in your Website access logs. When you analyse your logs, you'll be able to trace your visitors through the requests for the clear gif. For example, you will have entries such as:

[10/Oct/2002:03:16:42 +0000] "GET /clear.gif?name=Mary HTTP/1.1"

Here's the code for a page that tracks any "cookied" visitor through the function setGif(), and will also set a cookie with the user's name wn they submit the form that asks them for their name.

<html>
<head>
<script language = "javascript">

// set the cookie expiry date to be in twelve months
expireDate = new Date;
expireDate.setMonth(expireDate.getMonth() + 12);

// convert the expiry date to GMT format
cookieDate = expireDate.toGMTString();

// declare userName as global
var userName = "";

// setCookie() sets a cookie with the userName
// submitted by the form userForm
function setCookie() {
 userName = document.userForm.name.value;  
 cookieString = "userName=" + userName + "; expires=" +  
 cookieDate + ";";
 document.cookie = cookieString;
 alert('Welcome ' + userName + '!');

 // call function setGif() so that we track this visitor's page view
 // now that we know their name!
 setGif();
 return false;
}

// setGif() will embed the clear gif into the document
// and append the cookied userName so that we can track
// the user in our website access log.
function setGif() {
 if(document.cookie != "") {
   userName = document.cookie.split("=")[1];
   imageTag = '<img src="clear.gif?name=' + userName + '" width=1  
   height=1>';
   document.write(imageTag);
 }
}

</script>
</head>

<body>
<form name = "userForm">
Select a username:
<input type=text name="name" onBlur="return setCookie()">
<input type="submit" value="submit" onClick="return setCookie()">
</form>

<script language="javascript">
 setGif();
</script>
</body>
</html>

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links