如何有一个重定向使用头(位置:url)在php当点击<a>文本</a>


How to have a redirect using header(location: url) in php when click on a <a>text</a>?

我想使用这个,因为我必须维护一个会话。我尝试过这种方式,但是当页面打开时,函数正在立即执行:

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

功能:

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

我也尝试了表单和按钮,但不工作。

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
   }
?>