Can i do this?:
$(window).focus(function() {
<?php if(session....) ?>
...$.ajax...
<?php }else{
header('');
}
?>
});
Yes you can, but keep in mind that the PHP if-else is executed on the server and only once, so the resulting JS will contain either the $.ajax call or nothing at all - this won't change once the page has loaded.
Also, you can't usually call header after you've already sent some output (unless you're using output buffering).
if statement, then check it in JavaScript.