Output session key of Moodle to add custom log out link

Here is how you can output the user's session key in Moodle:

<?php
global $USER;
$sesskey = $USER->sesskey;
$logout = '<a href="http://yourdomain.com/login/logout.php?sesskey='.$sesskey.'">Logout</a>';
?>

Reference: https://moodle.org/mod/forum/discuss.php?d=166086