Jump to content

Archived

This topic is now archived and is closed to further replies.

EriRyoutan

Isn't working for some reason

Recommended Posts

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. ^^

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guess I missed explaining something.

This is in a file called index.php

if 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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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]<?php
session_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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.