PHP遇到了反问题

I have 3 php pages.

Counter.php - I want this file to display how many clicks Counter 2, Counter 3 got.

<!DOCTYPE html>
<html>
  <head>
    <title>Counter</title>
  </head>
  <body>
    You visited page Counter2
    <?php include 'Counter2.php'; echo $Counter; ?>
    Times.
    You visited page Counter3
    <?php  include 'Counter3.php'; echo $Counter3; ?>
    Times.
  </body>
</html>

Counter2.php - I want to count the amount of click this page has had.

<?php

  session_start();

  if(isset($_SESSION["counter"]))
  {
    $counter = $_SESSION["counter"];
  }
  else
  {
     $counter = 0;
  }
  $_SESSION["counter"] = $counter + 1;

?>

<!DOCTYPE html>
<html>
  <head>
    <title>Counter</title>
  </head>
  <body>
    <?= $counter ?>     
  </body>
</html>

Counter3.php - I want to count the amount of click this page has had.

<?php

  session_start();

  if(isset($_SESSION["counter3"]))
  {
    $counter3 = $_SESSION["counter3"];
  }
  else
  {
    $counter3 = 0;
  }

  $_SESSION["counter3"] = $counter3 + 1;

?>

<!DOCTYPE html>
<html>
  <head>
    <title>Counter</title>
  </head>
  <body>
  You visted this page this many times:
  <?= $counter3 ?>
  </body>
</html>

Now, everything is working how it should be, HOWEVER, when I include the php files <?php include 'Counter3.php'; echo $Counter3; ?> to log the amount of clicks it continues to increase when I load the Counter.php file. I do not want it to, I just want Counter.php to log how many clicks Counter2 and Counter3 has had. And stop Counter.php increasing the click number.

You can use $_SERVER['REQUEST_URI'] to add an extra if to both counter2.php and counter3.php, and if the $_SERVER['REQUEST_URI'] points to counter.php, you can skip incrementing the counters

Including a page makes the code run, so it would increase all the counters in the pages. Instead just use the $_SESSION[] variable directly.

<?php
  session_start()
?>
<!DOCTYPE html>
<html>
<head>
<title>Counter</title>
</head>
<body>
You visited page Counter2 <?php  echo $_SESSION["counter2"]; ?> Times.
You visited page Counter3 <?php  echo $_SESSION["counter3"]; ?> Times.
</body>
</html>

Also, in PHP you can do the following:

$_SESSION["counter3"] = isset($_SESSION["counter3"]) ? $_SESSION["counter3"] : 0;
//$_SESSION["counter3"] = $_SESSION["counter3"] ?? 0; // only in PHP 7

Also on a side note, you don't have to create this code on every page as it might get messy in the long run.

<?php #counter.php
  if(session_status() == PHP_SESSION_NONE){
    session_start();
  }

  if(isset($_SESSION['counter'][$_SEVER['REQUEST_URI']])){
    $_SESSION['counter'][$_SERVER['REQUEST_URI']] = $_SESSION['counter'][$_SEVER['REQUEST_URI']] + 1
  } else {
    $_SESSION['counter'][$_SERVER['REQUEST_URI']] = 1;
  }

  function countpagetimes(){
    return $_SESSION['counter'][$_SERVER['REQUEST_URI']];
  }
?>

<?php #somepage.php
  include 'counter.php';

  echo 'You visited this page ' . countpagetimes() . ' times'.

?>

There are several options for this problem:

  1. Save both values in a database and access the values through counter.php, instead of including the files
  2. Use $_SERVER['REQUEST_URI'] to let the counter only increment if the URI does not contain counter.php