【问题标题】:Navigate to different pages when link is clicked单击链接时导航到不同的页面
【发布时间】:2017-03-23 07:42:28
【问题描述】:

我有以下Index.php 文件的代码。每当用户单击 HTML 页面中的链接时,它就会重定向到 Index.php 页面。该页面随机重定向到三个不同的页面,即:“index_control.php”、“index_non_intrusive.php”、“index_intrusive.php”。 但是,现在,我想做的是:当用户首先点击链接时,它应该导航到数组中的第一个链接(index_control.php),当第二个用户点击链接时,然后数组中的第二个链接(index_non_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>

【问题讨论】:

  • 添加一个计数器,每次用户点击加 1,当达到 3 时,重新设置
  • 我想做序列导航,因为我想在每个页面上分配相同数量的用户(例如:总共 150 个用户,所以每个页面应该可供 50 个用户访问)。在随机导航中这是不可能的。
  • 如果您想知道重定向到哪里,计数器是一种方法。检查计数器是 1、2 还是 3。
  • 首先,您应该阅读此php.net/manual/en/session.examples.basic.php,然后您应该能够创建您的解决方案。但只有当您了解 Web 服务器和用户之间的通信是如何工作的。
  • 你想要达到的目标叫做A/B Testing。这个问题有无数的解决方案,用 PHP 或 Javascript 编码。此功能集成在 Google Analytics 中(您可以在行为/实验下找到它)并为您完成所有工作:它使用您配置的权重将传入流量拆分到您的索引页面,并向您显示统计信息以了解每个分支的执行情况。而且,额外的好处是,不需要编写一行代码(除了复制粘贴它提供的 Javascript 集成代码)。

标签: php arrays


【解决方案1】:

由于“会话是针对唯一会话 ID 为单个用户存储数据的一种简单方法”(可在 @Oliver 的 link 中找到)我相信会话不是平均分配您的(不同的!) 用户访问不同的 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();
?>

【讨论】:

    【解决方案2】:

    您必须在某处保存点击次数,在示例中我使用简单的JSON file。我使用jQuery 来监听点击事件并将数据发送到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>
    

    test.json

    {
        "clicked":1
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-09
      • 2018-01-03
      • 1970-01-01
      • 2023-03-31
      相关资源
      最近更新 更多