EriRyoutan Posted April 11, 2006 Share Posted April 11, 2006 For some reason this logger isn't working. It correctly logs Time, IP, and Agent, but it almost always refuses to log resolution. (out of like 10 logs, 1 had a resolution.)[code]<?php session_start();if ($log=="false"){session_register("counted");}if (!session_is_registered("counted")){ if($res==""){ ?> <script type="text/javascript"><!-- var res = window.screen.width + 'x' + window.screen.height; location = 'index.php?res=' + res --></script> <?php } $agent = $_SERVER['HTTP_USER_AGENT']; $ip = $_SERVER['REMOTE_ADDR']; $time = date('m/j/y h:i'); $query = "INSERT INTO `log` ( `Time` , `IP` , `Agent` , `Resolution` ) VALUES ( '$time', '$ip', '$agent', '$res');"; mysql_connect("host","user","pass"); mysql_select_db("dbname"); mysql_query($query); mysql_close(); session_register("counted");} ?><!-- rest of page here... -->[/code]Tried adding a [code]$res = $_GET['res'];[/code] but it didn't work...Any help would be appreciated, as well as any suggestions for making it a better logger. Thanks. ^^ Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 11, 2006 Share Posted April 11, 2006 PHP runs on the server, JavaSript runs on the browser. Your PHP won't see the what javascript finds out.How are you invoking the script?Ken Quote Link to comment Share on other sites More sharing options...
EriRyoutan Posted April 11, 2006 Author Share Posted April 11, 2006 Guess I missed explaining something.This is in a file called index.phpif you follow the tracing, if it dosen't find a resolution, it reloads the page, with the link being [code]index.php?res=1024x768[/code]or whatever, hopefully passing in the resolution.... the link looks okay (the resolution is there in the link) but for some reason it isn't being passed into the mysql db. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 11, 2006 Share Posted April 11, 2006 A few things come to mind while reading your code:[list][*]You seem to assume that register_globals in enabled. Do not assume that. Read the [a href=\"http://www.php.net/register_globals\" target=\"_blank\"]section[/a] in the manual on register_globals.[*]Do not use session_register() or session_is_resgistered(). Read the [a href=\"http://www.php.net/manual/en/ref.session.php\" target=\"_blank\"]section[/a] in the manual on sessions.[*]This could be done much better using AJAX from Javacript to start a small PHP script, which would do it's work outside the main flow of the program.[/list]I'm playing with your code to see what is going wrong & when.Ken Quote Link to comment Share on other sites More sharing options...
EriRyoutan Posted April 12, 2006 Author Share Posted April 12, 2006 I'm pretty sure register globals is on... I've done the rest of the php on my site using the same way (declare it in url, use it in code)... so...what's wrong with session_register() and _is_registered() ? ... What do you suggest using otherwise? I glanced at the manual (didn't have much time to -read- it...) and on an absolute sidenote, thank you for this much, and i bless you for dipping into the noob zone... Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 12, 2006 Share Posted April 12, 2006 It's always better to code your scripts as though register_globals is disabled that way they will work whether it is enabled or disabled plus you will be avoiding one type of security problem. Also, it looks like in PHP v6, register_globals will be permently disabled with no way of enabling it.I think I finally got your code working (maybe). At least it seems to work in my test environment.[code]<?phpsession_start();if (!isset($_SESSION["counted"])){ if(!isset($_GET['res'])){ ?> <script type="text/javascript"><!-- var res = window.screen.width + 'x' + window.screen.height; location = 'test_index.php?res=' + res --></script><?php }else { $res = $_GET['res']; $agent = $_SERVER['HTTP_USER_AGENT']; $ip = $_SERVER['REMOTE_ADDR']; // you realize that this field may not always be set? $time = date('Y-m-d H:i'); $query = "INSERT INTO `log` ( `Time` , `IP` , `Agent` , `Resolution` ) VALUES ( '$time', '$ip', '$agent', '$res')"; echo $query; // just to check mysql_connect("host","user","pass"); mysql_select_db("dbname"); mysql_query($query) or die('Problem with query: ' . $query . '<br>' . mysql()); mysql_close(); $_SESSION["counted"] = true; }}?>[/code]We were all new once, so I like to give back the knowledge and techniques I gathered from the different forums I participated in while learning PHP in 1999 & 2000...I'm still learning by reading other posts.Ken Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.