Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript and PHP 1

Status
Not open for further replies.

4x4uk

Technical User
Apr 30, 2002
381
GB
I have a simple website stats script written in php used mainly to log IP address browser type etc as webhost logfiles don't provide this info. I wish to add some additional info ie javascript enabled/disabled, cookies enabled/disabled, screen size and screen colour depth. I know I can get this info from javascript, the question is how do I pass thsi info to PHP. The code for my current script is as follows:
Code:
<?php
session_start();
if(!session_is_registered('counted')){
$agent = $_SERVER['HTTP_USER_AGENT'];
$ip = $_SERVER['REMOTE_ADDR'];
$ref = $_SERVER['HTTP_REFERER'];
$dtime = date('r');
if ($ref == &quot;&quot;){
$ref = &quot;None&quot;;
}
if($user == &quot;&quot;){
$user = &quot;None&quot;;
}
$entry_line = &quot;\n$dtime - IP: $ip | Agent: $agent | Referrer: $ref&quot;;
$fp = fopen(&quot;logs.txt&quot;, &quot;a&quot;);
fputs($fp, $entry_line);
fclose($fp);
session_register('counted');
}
?>
Thanx
Ian
 
Javascript is client side, php is server side. You can probably find most of the functionality in PHP. Otherwise, you'll need to load the page with the javascript, and have it call a php page with a query string, or post variables or the like, and then you can do what you like with it.

-Rob
 
I best have another look at the PHP manual then, I can't remember seeing those bits in there but they probably are and it will make life much simpler if they are.
Ian
 
Couldn't find screen width/height or cookies vars in PHP manual. The plan was to send these variables from an external javascript file(called from the index page) to the PHP script (used only to write to a log file for my info). Since I cant find PHP vars for the info I want I'll have to get them from javascript. I just need some pointers on how to pass var info from javascript to php.
Thanx
Ian
 
Suppose the page where your java script is, uses javascript to determine that cookies are enabled and uses a variable cookieSW. Pass cookieSW in a query string to the PHP and retrieve it thus.

$ip = $_SERVER['REMOTE_ADDR'];
$ref = $_SERVER['http_REFERER'];

$cookies = $HTTP_GET_VARS['cookieSW'];

$dtime = date('r');
Clive
 
I guess you could do the same with screen width and height as well?

Many thanks.
Ian
 
A big thankyou to everyone who helped.
I've now got everything working on my local machine and since it's configured the same as webserver it should al work when uploaded. I will now be able to get the extra info I need to ensure the site is optimized for the majority of visitors.
Ian It's not a lie if you believe it!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top