Click to See Complete Forum and Search --> : What's wrong with this php script?
Energon
03-05-2002, 06:10 PM
I downloaded a really simple session based counter in PHP. It does a session_start() and then checks if(!$PHPSESSID) // add to count, otherwise don't add. In order to figure who's refreshed and who's re-visited. But, it's not working. My error_log is saying that $PHPSESSID is an undefined variable. I personally don't have the foggiest about sessions, and the PHP documentation didn't give me much of a lead in fixing this problem.
Does anyone know what I should be using instead of $PHPSESSID? I just want to make sure that people hitting refresh aren't re-counted.
Taizong
03-05-2002, 06:39 PM
This running on a Nix box or a Windows system? I have had trouble with registering sessions on NT machines, you have to modify the variable in session.save_path in the php.ini file to a valid Windows directory like C:\WinNT\Temp or something.
Are you placing the register at the very top of the script?
example:
<?php
session_register("var1");
Where var1 is a session you want to register as persistent. Otherwise, when you try to call the session ID later in the script as if it were a global variable, it won't find it, and give you the message you are recieving.
Post the script, or point me to a download for it.
Energon
03-05-2002, 07:09 PM
It's on a Linux box. There's no session_register() at all in the script. I was seeing that in the PHP documentation, but I wasn't sure how to fit it into what I downloaded. You can see the script at http://starscream.kunaschools.org/shane/counter.php.tar.gz (http://starscream.kunaschools.org/shane/counter.php.tar.gz) (I archived the version I've modified).
[ 05 March 2002: Message edited by: Energon ]
joelmon
03-05-2002, 10:20 PM
Undefined variables are solved by
typing
$variable = '';
before using it
That's what I do to solve that problem
Unless there is more to the error. I can try to use the code later to help you solve the problem
Taizong
03-06-2002, 12:19 PM
The script looks fine to me. It worked for me too on my test box. All I had to do was touch the edit.me and count.txt file.
By the way, like the notes say, there are better ways of doing this. :D
Energon
03-07-2002, 01:35 AM
joelmon - unfortunately, this particular variable is defined by PHP, not the script... :(
Taizong - Did you set the one variable to visitors so that you don't log refreshes? And if you don't mind me asking, how does the better way go? just a couple hints would do me fine... :D
Fandelem
03-08-2002, 04:14 AM
since you're already writing to a file, you could make it completely 'unique ip addresses' by writing their IP address to the file only if it didn't exist already (and you could get the total number by just counting the lines..)
because here's the thing with session vars:
the session is only good until you close your browser. if they close their browser, and go back to your page later on, it will add another count. the method described above would not, because it's already marked that IP address..
but then again, i came in at the tail end of the discussion, and i don't know what your goal here is :)
cheers,
kyle
Energon
03-08-2002, 03:21 PM
hmm... well... I was thinking about recording the IP and the time, that way if they come back after say, 12 hours or something, I can record them again. But the problem is that on our network, everyone has to go through a proxy, which means I'd actually end up missing the majority of visitors if I counted IPs... but sessions should prevent that, shouldn't it?
Energon
03-08-2002, 05:28 PM
Okay, this is what I'm thinking, write up a file where the first line is the current count. Then the rest of the file is split into this:
IP
Unix timestamp
IP
Unix timestamp
and so on. Then I can read the file to see if the IP exists in it and then compare the written timestamp to the current one and if it's less than 12 hours or something then update their entry and update the count (otherwise do nothing). If the IP isn't in there, append it to the end and update the count at the top.
The only things I don't know how to do is get the client IP and how to search a file really fast without loading the whole thing into memory.
Does that sound like a pretty efficient counter system, or have I done a bad tradeoff somewhere?
EDIT - heh... figured out how to get the IP address... figures it'd be a constant and not a function.
[ 08 March 2002: Message edited by: Energon ]
Energon
03-11-2002, 06:39 PM
*scratches head*
Alright... so I've gotten everything working except the updating part. Is there a way to change certain bytes of a text file without loading the entire file into memory (this could, theoretically, be a very large file)? You'd think I'd have the answer to that coming from C, but I can't think of any way to do it. I can either update the record in-place or delete and re-add it, but both options in my head require me to load the file totally into memory.
Fandelem
03-14-2002, 04:56 PM
Is there a way to change certain bytes of a text file without loading the entire file into memory
*cough*MySQL*cough*
a simple update() call would be all you need :)
but, if you went with the file route,
http://www.php.net/manual/en/function.fscanf.php http://www.php.net/manual/en/function.sscanf.php
might help you out.
cheers,
kyle
Energon
03-14-2002, 05:22 PM
ahhh... MySQL... I didn't even think about that... haha... in fact, that would probably make it about 99.9% easier to do just about anything with the counter, wouldn't it?
Thanks for the idea! :)