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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

application variable

Status
Not open for further replies.

Jeremy21b

Technical User
Jul 7, 2002
127
CA
I have been using ASP for years, but am now just learning PHP. I'm creating a website which I want to use an application variable. Does PHP support this?

Here is the scenario:
The website is for my parents to use while living overseas. One feature that I wanted to include was a basic chatroom. Rather than loading the chatroom everytime, only to find it empty, I wanted to be able to display which users were currently on. I see a lot of foums displaying current users and I want the same idea.

Also does php use anything like a global.asa? In other words, where do I put the code to detect that they have closed their browser?
 
I can answer your first question.

Everytime somebody visits your website, tell the script to create a new text file in a separate folder. This file will contain the current unix timestamp. The file will be named "Users Ip" + ".txt", and will be overwritten whenever the user revisits the website.

When the page loads, you can create a script that will check each file in that folder, and compare it to the current timestamp. You can compare those to figure out whether the user has been active in the past 15 minutes. Set up a counter, and wallah! To save space on the server, you should also create a script that will check all text files, and delete all files with very old timestamps.

Sorry if I'm not making sense. I'm very tired right now!

I wont write the script for you, because you wont learn that way, but I'll tell you what functions you need.

- Foreach + Glob (To sort through the folder)
- time() (Will return current Unix timestamp)
- Fopen, Fread, Fwrite (To work with files)
- Unlink (To delete files)
Look up these functions at
To determine the user's Ip Address:

Code:
<?php
$ipadd = $_SERVER['REMOTE_ADDR'];
// The user's IP will be placed in variable 'ipadd'
?>

Of course, you will need to know basic PHP and the language structure. I say, start smaller.
 
Crap. You wanted the current users online, and not how many. This is why I don't program when I'm tired. Oh well. You will still need to use the same idea. :(
 
Hmm, I think what you need is:

Alternative A:
-> IRC chatroom
An IRC chatroom, uses the service IRC (Internet Relay Chat). You then need some sort of php IRC client to connect to an IRC server. You can then list users on the channel(s).

Alternative B:
-> PHP/Mysql chatroom
An PHP/Mysql chatroom, will work very easy.. Let the users "log in" with desired nick. You might want to preserve certain names, eg. your familys names?? So people need passwords for accessing those names. Then you can have some sort of "timeout", where you do something like selecting only users who have been active within the last 5 minutes..

I know PEAR has some IRC functions, but you should easily find php/mysql chatroom I guess.

It's almost like a guestbook/shoutbox.
ps. I think that maybe also a shoutbox might be what you are after here, only with a "Login" function.. (so there is no spam in your own nick).

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top