Jump to content


Photo

Isn't working for some reason


  • Please log in to reply
5 replies to this topic

#1 EriRyoutan

EriRyoutan
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 11 April 2006 - 09:16 PM

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

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

Tried adding a
$res = $_GET['res'];
but it didn't work...

Any help would be appreciated, as well as any suggestions for making it a better logger. Thanks. ^^

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 11 April 2006 - 09:25 PM

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

#3 EriRyoutan

EriRyoutan
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 11 April 2006 - 10:38 PM

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
index.php?res=1024x768
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.

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 11 April 2006 - 11:40 PM

A few things come to mind while reading your code:
  • 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.
I'm playing with your code to see what is going wrong & when.

Ken

#5 EriRyoutan

EriRyoutan
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 12 April 2006 - 02:49 AM

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

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 12 April 2006 - 04:12 AM

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.
<?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;
   }
}
?>

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users