Page 1 of 1

how can I really set a php var from javascript var?

Posted: Thu Aug 10, 2006 2:15 pm
by aprestong
I am trying to get stats (browser, version, resolution) using javascript - and then trying to save them as php variables
for example

Code: Select all

<script language="JavaScript">
var res= (screen.width+"x"+screen.height)
var browser=(navigator.appName)
var version= (navigator.appVersion)
</script>

Code: Select all

$resolution = "?> <script language=javascript>document.write(res);</script><?php";
   $resolution = str_replace("?>", "", $resolution);
   $browser = "?> <script language=javascript>document.write(browser);</script><?php";
   $browser = str_replace("?>", "", $browser);
   $version = "?> <script language=javascript>document.write(version);</script><?php";
   $version = str_replace("?>", "", $version);
I suppose it works, but it doesnt really write the data--- it just holdes the javascript that does the check... so when I echo it, it works, but it only works because it is doing the javascript test.

Is there any way I can actually write that data to a variable?

I want to pass along the browser, version, and screen resolution to my FileMaker database- and since FileMaker isn't a browser, I pass along this for browser:

Code: Select all

<script language=javascript>document.write(browser);</script><?php
Does this make sense? How can I get it to actually get the browser and copy the actual text to a variable?

Posted: Thu Aug 10, 2006 2:37 pm
by Luke
Javascript can be output by php, but not the other way around. All php logic happens before the page is even sent to the browser, so a client-side code (runs in your browser) is not aware that php even exists.

Posted: Thu Aug 10, 2006 3:15 pm
by Ollie Saunders
Ninja is, of course, quite correct but despite that being true you can send data back to the server (PHP) once the page has loaded. That would be AJAX.

Posted: Fri Aug 11, 2006 10:26 am
by aprestong
I solved my problem - and as far as I know, it works well.
Since I am collecting information like browser, version, and screen resolution, I can set this information in a cookie using javascript on the login page.
The login page will POST to another page that will do a redirect based on resolution.
So, since I have this second "redirect" page, I can also use it to get the data from the cookies and write to my database.

I set the cookies on the login page by

Code: Select all

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

// ***********************************************
// AUTHOR: WWW.CGISCRIPT.NET, LLC
// URL: http://www.cgiscript.net
// Use the script, just leave this message intact.
// Download your FREE CGI/Perl Scripts today!
// ( http://www.cgiscript.net/scripts.htm )
// ***********************************************

// for NN4/IE4
if (self.screen) {     
        width = screen.width
        height = screen.height
}

// for NN3 w/Java
else if (self.java) {   
       var javakit = java.awt.Toolkit.getDefaultToolkit();
       var scrsize = javakit.getScreenSize();       
       width = scrsize.width; 
       height = scrsize.height; 
}
else {

// N2, E3, N3 w/o Java (Opera and WebTV)
width = height = '?' 
}
   
//-->
</script>
<script language="javascript">
 var browser = navigator.appName;
 var version = navigator.appVersion;
</script>

<script language="javascript">
setCookie("resolutionwidth", width);
setCookie("resolutionheight", height);
setCookie("browser", browser);
setCookie("version", version);
setCookie("ip", ipStr);
</script>
and then on the second page, I got the data in a php variable with

Code: Select all

$resolutionwidth = $HTTP_COOKIE_VARS["resolutionwidth"] ;
$resolutionheight = $HTTP_COOKIE_VARS["resolutionheight"] ;
$browser = $HTTP_COOKIE_VARS["browser"] ;
$version = $HTTP_COOKIE_VARS["version"] ;

Posted: Fri Aug 11, 2006 10:29 am
by feyd
I would be careful with creating so many cookies. Browsers limit the quantity (as well as size) of cookies they will keep for any given domain.

Posted: Fri Aug 11, 2006 11:01 am
by aprestong
I hadn't thought about that. Do you know how many cookies is too many?

dang.

Posted: Fri Aug 11, 2006 11:08 am
by Chris Corbyn
Why not send the data right away? Someone showed a cool little trick with images not so long back.

Code: Select all

var foo = '42';
var bar = '10';

var fakeImg = new Image();
fakeImg.src = 'yourscript.php?foo=' + foo + '&bar=' + bar;
You needn't do anything with the variable fakeImg after that since it'll just be a broken image anyway but as soon as you assign the src value the script will make that HTTP request, and thus yourscript.php can run.