Cookies (like all HTTP Headers) need to be sent before any OUTPUT to the page is made, e.g. before you print() or echo() anything, and you're not allowed to have any text in front of your first <?php.
But
$visits= $_COOKIE['visits'] +1;
doesn't add any output to the page, so it's safe to use it.