Still comes through the URL indirectly, but this is the easiest way to get it: menu_get_object.
<?
if ($node = menu_get_object()) {
$nid = $node->nid;
}
?>
Note that if there is a node, it was already loaded by the menu system anyway and this is loaded from the static cache.
Refer this discussionthis discussion for more detail.