Php 单击链接时导航到不同的页面

Php 单击链接时导航到不同的页面,php,arrays,Php,Arrays,下面是Index.php文件的代码。每当用户单击HTML页面中的链接时,它就会重定向到Index.php页面。此页面随机重定向到三个不同的页面,即:“index\u control.php”、“index\u non\u intrusive.php”、“index\u intrusive.php”但是,现在,我想做的是:当用户首先单击链接时,它应该导航到数组中的第一个链接(index\u control.php),当第二个用户单击链接时,然后是数组中的第二个链接(index\u non\u in

下面是
Index.php
文件的代码。每当用户单击HTML页面中的链接时,它就会重定向到
Index.php
页面。此页面随机重定向到三个不同的页面,即:“index\u control.php”、“index\u non\u intrusive.php”、“index\u intrusive.php”但是,现在,我想做的是:当用户首先单击链接时,它应该导航到数组中的第一个链接(index\u control.php),当第二个用户单击链接时,然后是数组中的第二个链接(index\u non\u intrusive.php),依此类推。当第四个用户单击链接时,它应该再次从数组重定向到第一个链接(index_control.php)。所以序列导航不是随机的。如果有人能给出一些提示或帮助实现这一点,那就太好了

Index.php

<html>
  <head>
    <title>
      abc
    </title>
  </head>

<?php
  $links= array("http://abc/index_control.php","http://abc/index_non_intrusive.php","http://abc/index_intrusive.php");
  $randomLink = $links[rand(0, count($links)-1)];
  header("Location: {$randomLink}");
  exit();
?>

<html>

abc

您必须将单击计数保存在某个地方,在示例中,我使用的是简单的
JSON文件
。我使用
jQuery
来监听click事件并将数据发送到
PHP

<?php
    if(isset($_POST['clicked'])){
        $links = array(
            "link1",
            "link2",
            "link3",
        );
        $clicked_count = json_decode(file_get_contents('test.json'),true)['clicked'];
        $clicked_count = ($clicked_count >= 2) ? 0 : $clicked_count+1;
        file_put_contents('test.json', json_encode(['clicked'=> $clicked_count]));
        header("location: {$links[$clicked_count]}");
        exit;
    }
?>

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <a href="#" id="button">Click</a>
</body>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
<script type="text/javascript">
    $(function(){
        $('#button').click(function(){

            // first parameter is your php file e.g. something.php, leave it blank if you use the same file
            $.post("", {clicked:true});  
        });
    });
</script>
</html>

您必须将单击计数保存在某个地方,在示例中,我使用的是简单的
JSON文件
。我使用
jQuery
来监听click事件并将数据发送到
PHP

<?php
    if(isset($_POST['clicked'])){
        $links = array(
            "link1",
            "link2",
            "link3",
        );
        $clicked_count = json_decode(file_get_contents('test.json'),true)['clicked'];
        $clicked_count = ($clicked_count >= 2) ? 0 : $clicked_count+1;
        file_put_contents('test.json', json_encode(['clicked'=> $clicked_count]));
        header("location: {$links[$clicked_count]}");
        exit;
    }
?>

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <a href="#" id="button">Click</a>
</body>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
<script type="text/javascript">
    $(function(){
        $('#button').click(function(){

            // first parameter is your php file e.g. something.php, leave it blank if you use the same file
            $.post("", {clicked:true});  
        });
    });
</script>
</html>
由于“会话是根据唯一的会话ID为单个用户存储数据的简单方法”(在@Oliver中找到),我认为会话不是将您(不同的!)用户均匀分布到不同URL上的工具。您需要一个全局可访问的“变量”。一个非常简单的方法是引用一个整型值,该整型值存储在文件中,并在每次使用后递增,如

<?php
  $f="filename_for_integer_value.txt"; // make sure you have write access!
  $links= array("http://abc/index_control.php",
                "http://abc/index_non_intrusive.php",
                "http://abc/index_intrusive.php");
  $n=file_get_contents($f);
  file_put_contents($f,($n+1)%count($links));
  header("Location: {$links[$n]}");
  exit();
?>

由于“会话是一种根据唯一会话ID为单个用户存储数据的简单方法”(可在@Oliver中找到),我认为会话不是将(不同的!)用户均匀分布到不同URL的工具。您需要一个全局可访问的“变量”。一个非常简单的方法是引用一个整型值,该整型值存储在文件中,并在每次使用后递增,如

<?php
  $f="filename_for_integer_value.txt"; // make sure you have write access!
  $links= array("http://abc/index_control.php",
                "http://abc/index_non_intrusive.php",
                "http://abc/index_intrusive.php");
  $n=file_get_contents($f);
  file_put_contents($f,($n+1)%count($links));
  header("Location: {$links[$n]}");
  exit();
?>


添加一个计数器,每个用户单击添加1,然后当它达到3时,将其重置回来我想进行序列导航,因为我想在每个页面上分配相同数量的用户(例如:总共150个用户,因此每个页面应可供50个用户访问)。在随机导航中这是不可能的。如果你想知道在哪里重定向,计数器是一种方法。检查计数器是1、2还是3。首先,您应该阅读此内容,然后才能创建解决方案。但只有当您了解了Web服务器和用户之间的通信是如何工作的时候,您想要实现的就是所谓的。这个问题有无数的解决方案,用PHP或Javascript编码。此功能集成在Google Analytics中(您可以在Behavior/Experiments下找到它),并为您完成所有工作:它使用您配置的权重将传入流量拆分到您的索引页面,并向您显示统计信息,以查看每个分支的执行情况。另外,不需要编写一行代码(除了复制粘贴它提供的Javascript集成代码之外)。添加一个计数器,每个用户单击添加1,然后当它达到3时,将其重置回来。我想进行序列导航,因为我想在每个页面上分配相等数量的用户(例如:总共150个用户,因此每个页面应可供50个用户访问)。在随机导航中,这是不可能的。如果您想知道重定向到何处,计数器是一种方法。请检查计数器是1、2还是3。首先,您应该阅读此内容,然后才能创建解决方案。但只有在您了解Web服务器和用户之间的通信是如何工作的情况下。您想要实现的是这个问题有无数的解决方案,用PHP或Javascript编码。这个功能集成在谷歌分析中(你可以在行为/实验中找到)并且为您做一切:它使用您配置的权重将传入流量拆分到您的索引页面,并向您显示统计信息以查看每个分支的执行情况。此外,无需编写一行代码(除了复制粘贴它提供的Javascript集成代码)。