You can get the current customer ID with the following code:
include($prestashopBasePath . '/config/config.inc.php');
$context = Context::getContext();
$customerId = $context->customer->id;
The reason why you are redirected is that you execute the script from a URL that is different from the shop URL defined in back-office -> Preferences -> Set shop URL. For example the shop URL is example.com/presta and your script is in example.com/other/script.php.
The solution is to move your script to the PrestaShop directory.