Lamez Posted January 5, 2010 Share Posted January 5, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/187222-two-questions/ Share on other sites More sharing options...
oni-kun Posted January 5, 2010 Share Posted January 5, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/187222-two-questions/#findComment-988678 Share on other sites More sharing options...
Lamez Posted January 5, 2010 Author Share Posted January 5, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/187222-two-questions/#findComment-988685 Share on other sites More sharing options...
SuperBlue Posted January 5, 2010 Share Posted January 5, 2010 You can use regular expressions to create your own Function, if the existing ones won't cut it. See also: preg_match Simply check to see if the Function returns true, then give the user a suitable error explaining what went wrong. Quote Link to comment https://forums.phpfreaks.com/topic/187222-two-questions/#findComment-988689 Share on other sites More sharing options...
oni-kun Posted January 5, 2010 Share Posted January 5, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/187222-two-questions/#findComment-988690 Share on other sites More sharing options...
corbin Posted January 5, 2010 Share Posted January 5, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/187222-two-questions/#findComment-988696 Share on other sites More sharing options...
SuperBlue Posted January 7, 2010 Share Posted January 7, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/187222-two-questions/#findComment-990270 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.