Jump to content

Two Questions.


Lamez

Recommended Posts

First Question:

How would I force a function when the user closes the browser? Would that be javascript? When the user logs on to the website, it shows everyone they are online, but when they close the browser, they are still online. So I want to force my logOut(); function on them.

 

Second Question:

Is there a function that allows me to examine a string, and look for a number and return a certain value, or do I have to write one myself? If so, how would I go about doing that?

 

-Thanks for all the help!

 

Link to comment
Share on other sites

First Question:

How would I force a function when the user closes the browser? Would that be javascript? When the user logs on to the website, it shows everyone they are online, but when they close the browser, they are still online. So I want to force my logOut(); function on them.

 

Second Question:

Is there a function that allows me to examine a string, and look for a number and return a certain value, or do I have to write one myself? If so, how would I go about doing that?

 

-Thanks for all the help!

 

Yes, as a browser is a client (and PHP is serverside) JS is required to achieve this. Such as this function (Not every browser allows this, but FF>2 and IE7+ does), Note you will need to use an AJAX technique to call the logoff function:

< body onbeforeunload="alert('Closing');">

 

I'm sure there are other methods available if you search them. As for your second question, what are you evaluating? What are you wanting to do? and what is an example scenario? I'm sure with preg_match you can find a number out of a string, but there may be a simpler method.

Link to comment
Share on other sites

As for looking at the string for numbers: I don't want people registering with names like: Johnny12 or anything like that, so I want to return a error saying you may not have numbers. That is all.

 

You can use a simple regular expression match. I've written one that should work, here's an example:

$str = 'Johnny302';

if (!preg_match('/[^a-z]/i', $str)) { 
    die('Your username contains invalid characters!');
} else {
    echo 'Your name is valid';
}

 

That expression will allow only a-z A-Z. What most people don't know, You can use this function as well: ctype_alpha: It should provide a faster alternative if you wish not to use preg_match.

Link to comment
Share on other sites

oni-kun, I think you meant to leave out that !.

 

 

 

Anyway, in response to OP first question:

 

You can't rely on JavaScript.

 

You can do that if you want, but the AJAX object will be destroyed before the user leaves the page (unless you tried to do it synchronously, but the browser could ignore that, or it could piss people off).  Also, some people disable JS, so they would be logged in forever.

 

Anyway, you'll need to keep track of the last time a user actually made an action.  (So essentially every time a page is loaded, you'll need to update a timestamp somewhere.)  Then you pick a threshold, x for now, and find users where the last action time > time() - x.

Link to comment
Share on other sites

Most people have JS enabled, you can easily account for the rest of the situations where JS doesn't work, with server-sided timeouts. It still gives a nice picture of how many sessions actually are active, though i wouldn't do it myself, since it would just bombard the server with additional http request.

 

I would however never use it as an excuse to not use JS, that some people have JS disabled. Because it isn't really a problem in reality.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.