Php Login with Log

Hi guys

I got dreamweaver to create my authenticated login system which works perfectly but I need a log of users who log into the system.

I have created a seperate table to hold the details: ID, Username and Date

Here is the code from the login page

[php]

<?php $colname_Recordset1 = "1"; if (isset($_SESSION['MM_Username'])) { $colname_Recordset1 = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']); } mysql_select_db($database_Harry, $Harry); $query_Recordset1 = sprintf("SELECT * FROM users WHERE Username = '%s'", $colname_Recordset1); $Recordset1 = mysql_query($query_Recordset1, $Harry) or die(mysql_error()); $row_Recordset1 = mysql_fetch_assoc($Recordset1); $totalRows_Recordset1 = mysql_num_rows($Recordset1); mysql_select_db($database_Harry, $Harry); $query_Recordset2 = "SELECT * FROM logs"; $Recordset2 = mysql_query($query_Recordset2, $Harry) or die(mysql_error()); $row_Recordset2 = mysql_fetch_assoc($Recordset2); $totalRows_Recordset2 = mysql_num_rows($Recordset2); session_start() ?> <?php mysql_query("INSERT INTO logs SET Date = NOW()"); $colname_Recordset1 = "1"; if (isset($_SESSION['MM_Username'])) { $colname_Recordset1 = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']); } mysql_select_db($database_Harry, $Harry); $query_Recordset1 = sprintf("SELECT BalanceDue, AccountStatus FROM users WHERE Username = '%s'", $colname_Recordset1); $Recordset1 = mysql_query($query_Recordset1, $Harry) or die(mysql_error()); $row_Recordset1 = mysql_fetch_assoc($Recordset1); $totalRows_Recordset1 = mysql_num_rows($Recordset1); ?>

[/php]

I managed to get this working [php]mysql_query(“INSERT INTO logs SET Date = NOW()”);[/php]

and it displays the Date properly but I cannot seem to get a username put in

First, why would you need a separate database table to log users logins? Surely you could setup addition columns, such as date_visited, number_of_visits or something like that in one database table. Then simply do a cron job or a simple script that counts visits then spits it out to you. Calculations like that would be very fast. An besides most popular websites allow to stay logged in for a long time, so even if you do something like what you’re talking about the number of visits would be kind of skewed anyways. By that I mean not very accurate, just like a hit counter for websites are.

Second this statement is completely useless:

[php] if (isset($_SESSION[‘MM_Username’])) {
$colname_Recordset1 = (get_magic_quotes_gpc()) ? $_SESSION[‘MM_Username’] : addslashes($_SESSION[‘MM_Username’]);
}[/php]

For the ternary statement will always return false, don’t believe me then visit this link : http://php.net/manual/en/function.get-magic-quotes-gpc.php that comes straight from the horses mouth.

Thirdly and lastly if you ever visited php.net you would know that mysql is obsolete and that you should either use mysqli or PDO (My Recommendation). It looks like you’re using a script that you found online, well that is probably 6-10 years old, my suggestion is to simply delete it and start from scratch. It would be faster and safer in my opinion, but if you don’t good luck in your quest.

Using obsolete MySQL is like getting on a single engine airplane that you know has engine problems and will die mid flight.

Ridge, I sort of agree with both Strider and Kevin! You need to update to a newer version. Seems that I
have been mentioning that a lot lately, quite often following up on other’s posts. So, PDO is the way to go
if you want to be completely up-to-date. BUT, I find that using the older code such as yours, it is very much
easier to just update to “mysqli” versions. It is simple and only your mysql lines need very minor changes.

Now, to answer your question. In your code you insert the log record, but, you only insert the date.
Therefore, you will only see the date inside the database. Assuming that your database is set up for the
ID to be auto-increment, you do not need to touch that field. So, you have this line:
mysql_query(“INSERT INTO logs SET Date = NOW()”);
This creates a new record in your table and sets the field “Date” to the current date/time/whatever…
You need to also set the username at the same time. In this type of query, you can set as many fields
as you need to fill in with data. You only put in your date field, so that is all you get out of it.

Since you appear to be a beginner, here is a great site for learning how to do, well, everything in all of the
best programming systems. (HTML, PHP, Javascript, etc…) This link is for how to set up multiple fields in
your insert query. On the left you can select other areas to learn more when you are ready. Hope this
answers your question and helps… good luck!
http://www.w3schools.com/php/php_mysql_insert.asp

Oh, if you are logging all times that a user logs in, a separate table is good. If you just want the last
time they used the site, just add a field to the user table as Strider64 mentioned. If you do that, you
will need to use the UPDATE mysql command to update the current user’s record with the current date.

Oh, if you are logging all times that a user logs in, a separate table is good.

This was going to be my reply to [member=57087]Strider64[/member]. To log every login, you would must have a separate table.

Sponsor our Newsletter | Privacy Policy | Terms of Service