[PHP] Online/Offline Status

Well currently I'm creating includes to put in place of certain PHP code. Things such as connections, comments, messages and I already setup the friendlist like that.

Since I have to do all this anyway, I'm most def going to rename all my getdata123's to names that correspond to their actions. The idea of calling things userQuery123 and friendQuery123 instead of getdata swam through my mind. But I can just as easily say userDate, userQuery, userRow instead. I don't know why I use getdata.. I've used it since I first learned how to make a connection, and stuck with it. Obviously the guy just used getdata as an example of how to do a connection, but for some reason I never did rename any of my data fetchers untill I ran into problems repeating varriables. Then after that I named the second one getdata4,5,6.. Ha.. stupid me.

After I made that mistake, I should've just gave them all definitive names. I do actually write out getdata, I don't copy and paste usually.. well not always. So I really don't know why I didn't just say userData, userQuery, userRow... But I will from now on.. (for users, that is.)
Of corse friends would be friendData, friendQuery, friendRow etc.

I really should've saw that coming.. ha

My sig should say: There's a fly on my brain! - lol
 
Also, and I think FragFrog meant that as well, try not to use $variables inside strings, but rather use the append function. This makes it much more clear that you're inserting a variable, and requires less parsing for the interpreter. I always use single quote above double quotes, since single quotes cannot contain $variables, and thus would be marginally faster.

I would also suggest that you use less variables where it makes no sense.

PHP:
$getdata="SELECT * FROM `users` ";
$getdata2=mysql_query($getdata) or die(mysql_error());

Could as well be written on one line (and is just as clear, IMO):

PHP:
$result = mysql_query("SELECT * FROM `users`") or die(mysql_errno());

Notice I use mysql_errno() rather then mysql_error(). The latter can display some valuable information for possible hackers, while the former simply indicates what kind of an error occured. You can then check your server logs for the actual error (or create a log yourself).

Now lets take a look at this part:
PHP:
$last_action=$getdata3['last_action'];
$last_action=strtotime($last_action);
$Onehour = 3600 ;
$expired = $last_action + $Onehour; 
$time=time();
$elapsedTime=$time-$last_action;

It could be written much smaller and still have the same functionality:
PHP:
$last_action = strtotime($getdata3['last_action']); // must get rid of this line
$expired = $last_action + 3600; // time at which one is considered being logged off
$elapsedTime = time() - $last_action; // the last time an action occured

That is only half the number of rows, and a few variables less :)
 
Oh, and I forgot to mention that I did update my stuff, and everything seems to be working like a charm.. Except one problem with the guest getting old data that doesn't even exist! Ugh.. see my new post. :) lol..

And I changed most of my sloppy code at least to make it shorter.
I changed everything I found that says $getdata to something more like $userQuery or $profileQuery...

I also changed some things like
PHP:
$row['var']=strip_tags($row['var']); 
$var=$row['var']
to
PHP:
$var=strip_tags($row['var']);

Most things are running just as smooth. And now it's easier to update certain things because I made include files for data, post_form data, create_profile data.. etc.
 
Back