View Full Version : Php + Js = ???

03-05-2007, 02: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.


03-05-2007, 02: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();
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) {
echo "User screen width: ".$_COOKIE['screenWidth']."px<br>";
echo "User screen height: ".$_COOKIE['screenHeight']."px<br>";
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.

03-05-2007, 03: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.

03-05-2007, 03: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
EXd.write("<img src=http://e1.extreme-dm.com",
"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" />

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.

03-05-2007, 04: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.