Jump to content

last_login in mysql is not updated if the username is Greek


hariskar

Recommended Posts

last_login in mysql is updated correctly when username is in Latin characters but not updated when in Greek.

Here is the code:

<?php
//include config
require_once('includes/config.php');
$username = $_POST['username'];
$password = $_POST['password'];
if($user->login($username,$password)){ 
$servername = "xxx";
$dbuser = "xxx";
$dbpass = "xxx";
$dbname = "xxx";
$conn = new mysqli($servername, $dbuser, $dbpass, $dbname);
$sql = "UPDATE members SET last_login=now() WHERE username='$username' ";
if ($conn->query($sql) === FALSE) {echo "Error updating record: " . $conn->error;}}
//check if already logged in move to home page
if( $user->is_logged_in() ){ header('Location: register'); } 
//process login form if submitted
if(isset($_POST['submit'])){
$username = $_POST['username'];
$password = $_POST['password'];
if($user->login($username,$password)){ 
$_SESSION['username'] = $username;
header('Location: /apotelesmata');
exit;
} else {
$error[] = 'Λάθος όνομα χρήστη ή κωδικός ή ο λογαριασμός σας δεν έχει ενεργοποιηθεί ακόμα.';
}}//end if submit
//define page title
$title = 'Login';
?>

and here is the included config.php

<?php
ob_start();
session_start();

//set timezone
date_default_timezone_set('Europe/Athens');

//database credentials
define('DBHOST','xxx');
define('DBUSER','xxx');
define('DBPASS','xxx');
define('DBNAME','xxx');

//application address
define('DIR','http://www.mikroviologos.gr/');
define('SITEEMAIL','xxx@xxx.xx');

try {

//create PDO connection
$db = new PDO("mysql:host=".DBHOST.";dbname=".DBNAME, DBUSER, DBPASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->exec("SET NAMES 'utf8';");
} catch(PDOException $e) {
//show error
echo '<p class="bg-danger">'.$e->getMessage().'</p>';
exit;
}

//include the user class, pass in the database connection
include('classes/user.php');
include('classes/phpmailer/mail.php');
$user = new User($db);
?>

In the included config.php there is a line

$db->exec("SET NAMES 'utf8';"); 

but the problem still exists.

Could you please help me on that?

Thank you!

Edited by hariskar
Link to comment
Share on other sites

Thank you but I can not understand what is happening: since there is

$db->exec("SET NAMES 'utf8';"); 

in the included file, the username registers correctly in mysql and login/php works fine.

The only problem is that when a user with Greek username logs in last_login in mysql does not update.

Is there anything wrong with this line? :

$sql = "UPDATE members SET last_login=now() WHERE username='$username' ";
Link to comment
Share on other sites

In included config.php I changed

$db->exec("SET NAMES 'utf8';"); 

to

$db->exec("SET CHARACTER SET utf8;");

but the problem remains: Greek username is correct in mysql with Greek characters being correctly displayed, but last_login still does not update for usernames in Greek.

 

Edit: I had to put it again, because I had a 2nd connection to the db.

Thank you!

Edited by hariskar
Link to comment
Share on other sites

I was not very clear: Problem solved. I have one db connection in included file and a second in the main file. I had to put

$db->exec("SET CHARACTER SET utf8;"); 

in both files after each connection to solve the problem.

Link to comment
Share on other sites

The recommended way is described here for each of the MySQL libraries: http://php.net/manual/en/mysqlinfo.concepts.charset.php

 

For MySQLi: $mysqli->set_charset()

For PDO, use the constructor: new PDO("mysql:host=localhost;dbname=world;charset=utf8", 'my_user', 'my_pass')

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...