How to add code for wp-comments-post.php over functions.php?

I modified the file wp-comments-post.php by adding this in there:

setcookie('addcomment', $  _SERVER['HTTP_REFERER'], (time()+300), "/"); 

After 54 lines, in the end it became like this.

...nsent Comment author's consent to store cookies.  */   setcookie('addcomment', $  _SERVER['HTTP_REFERER'], (time()+300), "/");   do_action( 'set_comment_cookies', $  comment, $  user, $  cookies_consent );  $  location = empt... 

How to do the same using functions.php?

Sorry for my English.