views:

167

answers:

4

My site is in flash. The login is also in flash. This is the login flow:

  1. Enter username and password

  2. Send to authentication.php page

  3. Here is my doubt on authentication.php page:

    (1) Check the post parameters, i.e username and password

    (2) Sha/hash the password

    (3) Q1(query 1), select username and password from users table

    (4) If matched, do another query(Q2), check if the user is still on ban status

    (5) If user is not in ban status, do a query (Q3) to check if the user is first log for today

    (6) If its first log, do update query (Q4) to rewards some money to the user

    (7) Create session, userid, a hashed session variable using md5. i.e

    $_SESSION['loggin'] = 1;

    $_SESSION['hash'] = md5(username.secret);

    (I use the $_SESSION['hash'] for another authentication in another page, just ignore this)

  4. Return result to flash.

I know its a very bad login flow, alots of mysql queries, 2 hashing(php calculations). I am still thinking/looking for a better way for this. Like combining the query etc. Any ideas on how to improve the flow?? (I must meet all the stuff i mentioned above)

[The current login is slow]

A: 

The process itself isn't that bad.

It's really hard to tell why it's slow without performance data. There's a 90% chance performance is lost in a completely different place than you think. Put a couple of lines in the code which print the current time (including the milliseconds) to see where it hangs around.

A join could certainly help but if the time isn't spent there, it will just make the code harder to understand and maintain. If you find the DB access takes long, I suggest to check that you have the right indexes for your queries. If the indexes are OK, post a question with the DLL of the tables if you need help to write the join. But first, measure the performance.

Aaron Digulla
How do you measure the performance? My method is through firebug. Enable the "Net" and see the time response. How to check the queries time??
mysqllearner
Well, mostly, my indexes are the primary key, userid or whateverid. Because those are the most I use to search for. Any help or advice would be most appreciate :)
mysqllearner
Measure performance: Put logging calls in the server code. As for the queries, "EXPLAIN" is your friend: http://dev.mysql.com/doc/refman/5.0/en/explain.html
Aaron Digulla
+1  A: 

Hi i would prefer to do an joins in one query.

SELECT u.user_id, u.name, u.password, u.last_last FROM users u 
LEFT JOIN banned b ON u.user_id = b.user_id
WHERE u.name = ? AND u.password = ?;
ArneRie
A: 

A few ideas regarding performance and security:

Steps 3 - 6: When reading from the database you should be able to select username, password, user status and last-login date in a single query, if your database is designed correctly (and as it seems ArneRie has just posted the pseudo-MySQL statement to do just that).

Updating the user (giving them reward) is a one-time INSERT/UPDATE and shouldn't effect performance that much.

Aside from cutting down on the number of MySQL queries (and optionally using salted hashes for passwords) the process looks ok.

pygorex1
Well, 1 thing I am hesitated doing combining is that, if the username and password didnt match, i can immediately return the result, login failed. If i do combine query, means it select more stuff, and it might took more milliseconds to respond. But, i also know that, the chances of username and password didnt match is very low, unless he is trying to hack. Maybe I am wrong though. Please advice
mysqllearner
The "combining", I mean doing combining query. Type too fast, and sorry for my bad english
mysqllearner
Using a JOIN as in ArneRie's example will be very, very close to the same (performance-wise) as doing a select for just the user's info. The fewer MySQL statements your script has to execute the better.
pygorex1
+1  A: 

To get a more abstract view of the problem the answers to the following questions would help:

  • What environment are you developing in? 100% Locally (local code and Db or local code and external Db? etc)?
  • Is this all 100% 'fresh' code, have you developed anything similar and not had these issues?

On a side note I use Chromes builtin resource monitor which shows the breakdown of a web pages size and time it took to download. I find this more useful than the one in FireFox/FireBug.

Have you tested each of your queries in something like PHPMyAdmin or similar to test query time? Add up the total time of each query and see what the difference is.

Good luck, Alex

Alex
Also when you say it's slow, how many seconds are you talking about? Is it always the same amount of time?
Alex
- I am test on local code and DB, and put it to real server to see the performance. Its fine on local, but on real time, its much slower- Not actually fresh, but this one so far has largest data records. Previously was few thousands records. This one more than that.Chromes bulletin resources? Okay, i will go and try using that.SOmetimes I did the query on PHPMyAdmin, it always return 0.00xx seconds, which i think very fast. But in website, the login is quite slow, take more than 5 secs. (Maybe its not mysql problem)??Thanks
mysqllearner
Um.. let me change to this. Based on the current login flow mentioned above and my current database users has 50K++ records, is it normal that the login takes more than 5 seconds? Because previous website I created, the login is fast, much faster than this one
mysqllearner
That is very slow. Do you have a dedicated server or who do you pay to host? If its a really cheap/low quality hosting company its likely to be because of them. Are you testing the full system locally or just the non-flash components?
Alex