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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aprestong
Forum Newbie
Posts: 5
Joined: Thu Aug 03, 2006 2:47 pm

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

Post 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?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
aprestong
Forum Newbie
Posts: 5
Joined: Thu Aug 03, 2006 2:47 pm

Post 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"] ;
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
aprestong
Forum Newbie
Posts: 5
Joined: Thu Aug 03, 2006 2:47 pm

Post by aprestong »

I hadn't thought about that. Do you know how many cookies is too many?

dang.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
Post Reply