...

View Full Version : Php + Js = ???



ArcticFox
03-05-2007, 01:00 AM
I'm wanting to add the ability to fputs($fp the visitors' browser information like available screen width, colours, resolution, etc.

I've searched all over and can't figure this out. Somehow there's a way to send it from JS into PHP?

Any ideas on this? Extreme Tracking does this quite well, I'm looking for something simple, or starter tips on where to look for this information.

Thanks.

_Aerospace_Eng_
03-05-2007, 01:23 AM
Create cookies using javascript then use php to read the cookies. For example save this page as test.php

<script type="text/javascript">
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

function eraseCookie(name) {
createCookie(name,"",-1);
}
createCookie('screenWidth',screen.width,9999);
createCookie('screenHeight',screen.height,9999);
</script>
<?php
if(isset($_COOKIE['screenWidth']))
{
echo "User screen width: ".$_COOKIE['screenWidth']."px<br>";
echo "User screen height: ".$_COOKIE['screenHeight']."px<br>";
}
else
{
echo "There is no cookie set.";
}
?>
Navigate to the page at least once and then refresh the page. PHP will echo the cookie created by javascript. Hmm the problem I see with this is the cookies aren't read by php until the next page load.

Inigoesdr
03-05-2007, 02:02 AM
You could also use XMLHTTPRequest (aka AJAX), but as the previous poster stated, anything other than just recording stats is useless because it wouldn't apply until the next pageload.

ArcticFox
03-05-2007, 02:53 AM
Cookies would be cool - I have yet to play with those. But, yes the problem would be of those people who only look at my first page.

Here's a sample of Extreme's code:


<div id="eXTReMe"><a href="http://extremetracking.com/open?login=momav21" target="_blank">
<img src="http://t1.extreme-dm.com/i.gif" style="border: 0pt none ;" id="EXim" alt="eXTReMe Tracker" height="38" width="41"></a>
<script type="text/javascript"><!--
var EXlogin='momav21' // Login
var EXvsrv='s10' // VServer
EXs=screen;EXw=EXs.width;navigator.appName!="Netscape"?
EXb=EXs.colorDepth:EXb=EXs.pixelDepth=;
navigator.javaEnabled()==1?EXjv="y":EXjv="n";
EXd=document;EXw?"":EXw="na";EXb?"":EXb="na";
EXd.write("<img src=http://e1.extreme-dm.com",
"/"+EXvsrv+".g?login="+EXlogin+"&amp;",
"jv="+EXjv+"&amp;j=y&amp;srw="+EXw+"&amp;srb="+EXb+"&amp;",
"l="+escape(EXd.referrer)+" height=1 width=1>");//-->
</script><img src="http://e1.extreme-dm.com/s10.g?login=momav21&amp;jv=y&amp;j=y&amp;srw=1280&amp;srb=32&amp;l="
height="1" width="1"><noscript><div id="neXTReMe"><img height="1" width="1" alt=""
src="http://e1.extreme-dm.com/s10.g?login=momav21&amp;j=n&amp;jv=n" />
</div></noscript></div>

From what I can tell, it captures the information on the first pageload. I see the parts I want, but don't know what's going on in that script.

And it's not just resolution and colour I'd look for, it's also the other visitors' information I can display easily with JS.

_Aerospace_Eng_
03-05-2007, 03:00 AM
Hmm it looks like the image is a dynamic image and sends stuff back to the server. Look into creating dynamic images with php.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum