如何在点击<a>文本</a>时在php中使用标题(location:url)进行重定向?

I want to use this because i have to maintain a session. I tried in this way but the function is being executed immediately when the page is opened:

 <a onclick="myfunction();">text</a>

Function:

function myfunction()
{
  <?php
       header("location: page.php");
       exit;
  ?>
}

I tried also with forms and buttons but isn't working.

HTML

<a href="redirect.php">text</a>

redirect.php

<?php
   session_start();
   if($_SESSION['smth']){
       header("location: page.php");
   } else {
      // to do what you want
   }
?>