This topic is locked

Create Access Log after login

4/14/2007 12:59:58
PHPRunner General questions
P
powerchuck author

I am trying to create an access log that records each successful login in a separate table called _access_log

The table contains the following fields: Field1, ID, username, access_time, ip_address.
After reading through various forum posts I created this event - but it doesn't work right and I get the error:
[indent]" Parse error: syntax error, unexpected T_STRING in /usr/local/pem/vhosts/831/webspace/httpdocs/Geared4Fun/roster/include/events.php on line 7 "[/indent]
upon entering the login screen. Any Ideas? here's the event script.
[codebox]function AfterSuccessfulLogin(&$values)

{

//** Insert a record into another table ****

global $conn;

$strSQLInsert = "insert into _access_log (Field1,ID, username, access_time, ip_address) values ("login",

{$_SESSION["ID"]= mysql_insert_id();}

$username,".$values["Date"].",$values['ip_address'] = $_SERVER['REMOTE_ADDR'];
)";

db_exec($strSQLInsert,$conn);
}[/codebox]

kujox 4/14/2007

[codebox]

$strSQLInsert = "insert into _access_log (Field1,ID, username, access_time, ip_address) values ("login",

{$_SESSION["ID"]= mysql_insert_id();}

$username,".$values["Date"].",$values['ip_address'] = $_SERVER['REMOTE_ADDR'];

)";

}[/codebox]


It looks like your $strSQLInsert is wrong, you don't need the = signs and need to seperate the fields and values, try this, I have broken it down so you can see a little better what is happening
$strSQLInsert = "insert into _access_log (Field1,ID, username, access_time, ip_address) values (";// this is the field list

$strSQLInsert .= "login,";// this is Field1, would this be fixed text

$strSQLInsert .= mysql_insert_id().",";//I suspect this is the id in this table, might not be needed like this

$strSQLInsert .= $username.",";// might be better to use $_SESSION['UserID']

$strSQLInsert .= date('Y-m-d H:i:s').",";// this is the current timestamp

$strSQLInsert .= $_SERVER['REMOTE_ADDR'].")"; // the ip address and close bracket

P
powerchuck author 4/15/2007

Kujox, thanks for the help! That's the hardest part for me.

You are right the ID in the table isn't needed as that is auto incremented for me.
So this is the event script, i also tried it without the username field all togther.
I don't get any errors, but I also don't get any data in the _access_log table. I'm not sure why nothing is being written to the access log?
[codebox]function AfterSuccessfulLogin()

{

//** Insert a record into access_log table ****

global $conn;

$strSQLInsert = "insert into _access_log (Field1, username, access_time, ip_address) values (";// this is the field list

$strSQLInsert .= "login,";// this is Field1, would this be fixed text

$strSQLInsert .= $_SESSION['UserID'];// tried $username."," but didn not work so used $_SESSION['UserID']

$strSQLInsert .= date('Y-m-d H:i:s').",";// this is the current timestamp

$strSQLInsert .= $_SERVER['REMOTE_ADDR'].")"; // the ip address and close bracket
}[/codebox]

J
Jane 4/16/2007

Hi,
you need to use single quotes around text values in your query:

$strSQLInsert = "insert into _access_log (Field1, username, access_time, ip_address) values (";

$strSQLInsert .= "'login',";

$strSQLInsert .= "'".$_SESSION['UserID']."'";

$strSQLInsert .= date('Y-m-d H:i:s').",";

$strSQLInsert .= "'".$_SERVER['REMOTE_ADDR']."')";

P
powerchuck author 4/16/2007

Thanks for your help Jane,
I copied and pasted your code in - this is the event now:
[codebox]function AfterSuccessfulLogin()

{

//** Insert a record into access_log table ****
global $conn;

$strSQLInsert = "insert into _access_log (Field1, username, access_time, ip_address) values (";

$strSQLInsert .= "'login',";

$strSQLInsert .= "'".$_SESSION['UserID']."'";

$strSQLInsert .= date('Y-m-d H:i:s').",";

$strSQLInsert .= "'".$_SERVER['REMOTE_ADDR']."')";
}[/codebox]
Unfortunately nothing is writing to the _access_log table, not sure why.
Thanks

L
larsonsc 4/16/2007

I haven't used the AfterSuccessfulLogin event before, so I may be completely off on this, but I don't see anything actually executing the SQL query in that event. Shouldn't there be a line that actually calls $strSQLInsert? If I'm correct, I would think the event would look like this:

function AfterSuccessfulLogin()

{

//********** Insert a record into access_log table ************
global $conn;
$strSQLInsert = "insert into _access_log (Field1, username, access_time, ip_address) values (";

$strSQLInsert .= "'login',";

$strSQLInsert .= "'".$_SESSION['UserID']."'";

$strSQLInsert .= date('Y-m-d H:i:s').",";

$strSQLInsert .= "'".$_SERVER['REMOTE_ADDR']."')";
db_exec($strSQLInsert,$conn);

}


I could most definitely be wrong on this though as like I previously said, I've never used this particular event before so I do not now if it needs a db_exec call or not.

Alexey admin 4/17/2007

Hi,
try using this code in your event:

global $conn;

$strSQLInsert = "insert into _access_log (Field1, username, access_time, ip_address) values (";

$strSQLInsert .= "'login',";

$strSQLInsert .= "'".$_SESSION['UserID']."'";

$strSQLInsert .= "'".date('Y-m-d H:i:s')."',";

$strSQLInsert .= "'".$_SERVER['REMOTE_ADDR']."')";

P
powerchuck author 4/17/2007

I get the error
[indent]PHP error happened
Technical information

Error type 256

Error description Column count doesn't match value count at row 1

URL www.WWW.GEARED4FUN.COM/roster/login.php?

Error file /usr/local/pem/vhosts/831/webspace/httpdocs/Geared4Fun/roster/include/dbconnection.php

Error line 26

SQL query select * from `_Members` where `username`='powerchuck' and `password`='3db8733d3c6debc74c3a22518782747b'

[/indent]
The script in its entirety is [codebox]function AfterSuccessfulLogin()

{

//** Insert a record into access_log table ****
global $conn;
$strSQLInsert = "insert into _access_log (Field1, username, access_time, ip_address) values (";

$strSQLInsert .= "'login',";

$strSQLInsert .= "'".$_SESSION['UserID']."'";

$strSQLInsert .= "'".date('Y-m-d H:i:s')."',";

$strSQLInsert .= "'".$_SERVER['REMOTE_ADDR']."')";
db_exec($strSQLInsert,$conn);
}

[/codebox]
The fields in the table "_access_log" are (in this order):
[indent]Field1 varchar 50

ID INT (Primary Key Checked and this field is auto-increment)

username varchar 50

access_time varchar 50

ip_address varchar 50[/indent]
The lack of info earlier was due to me forgetting the db_exec($strSQLInsert,$conn); instruction.
But the error is weird and prevents any futher access. When I go into myPHP admin panel of my server I see the exact same fields named the same plus 1 more field "CreatedbyPHPRunner" int(11) Null =Yes default = null. My server is running MySQL 4.0.18-Max with client version 4.1.11 on a linux server.
Thanks for your help so far - ( the code is an exact cut and past of Alexey's code below)
Any ideas? Should I scrap this and start over?

V
Vladimir 4/18/2007

Hi, powerchuck
just add one comma in your event:

function AfterSuccessfulLogin()

{

//** Insert a record into access_log table ****
global $conn;
$strSQLInsert = "insert into _access_log (Field1, username, access_time, ip_address) values (";

$strSQLInsert .= "'login',";

$strSQLInsert .= "'".$_SESSION['UserID']."',";

$strSQLInsert .= "'".date('Y-m-d H:i:s')."',";

$strSQLInsert .= "'".$_SERVER['REMOTE_ADDR']."')";
db_exec($strSQLInsert,$conn);
}

P
powerchuck author 4/18/2007

Wow, That fixed it - Awesome and thanks!
Any idea why each entry shows twice?
These are the first few entries:
[indent]ID username access_time ip_address Field1

1 powerchuck 2007-04-18 19:06:07 72.197.52.248 login

2 powerchuck 2007-04-18 19:06:07 72.197.52.248 login

3 chuckles 2007-04-18 19:06:40 72.197.52.248 login

4 chuckles 2007-04-18 19:06:40 72.197.52.248 login

5 powerchuck 2007-04-18 19:07:08 72.197.52.248 login

6 powerchuck 2007-04-18 19:07:08 72.197.52.248 login

[/indent]
I can live with the double entries but it is weird.