douren6874 2013-11-19 05:29
浏览 16

每次刷新页面后随机切换停放的域

Suppose i have the main domain 1.com and two parked domains of 2.com, 3.com. Now, how to randomly switch between each of the parked and main domains on each page refresh. how can this task done effectively? i am using php.

e.g. when the page is loaded for the first time, address bar is 2.com (it is selected randomly and can also be 1.com or 3.com), know if i refresh the page, the address bar will be 1.com (again, it can also be 2.com or 3.com), and so on..

  • 写回答

1条回答 默认 最新

  • doupengxie4195 2013-11-19 06:04
    关注

    Well if you are using a plain php site then it would become some work but I can think of two ways,

    In either cases you need a function that generates those domains dynamically

    <?php
        getDomain(){
            $domains = array('example.com','example2.com', 'example3.com');
            return $domains[mt_rand(0,count($domains)-1)];
        }
    ?>
    
    1. Either create all links on the page as relative links then add a base tag on the top of the page which is generated dynamically.

      <head><base href="<?= getDomain() ?>"></head>
      

      This way all relative links will be written relative to that base, this is the easier method but it's not really what I would recommend.

    2. Second method I would suggest is creating a function that creates all the links, or handle it manually if you don't want to create an extra function, for example:

      Instead of doing this

      <a href = 'http://mydomain.com/some/uri'>Go here</a>
      

      You would write it something like this

      <a href = '<?= getDomain() ?>/some/uri'>Go here</a>
      

      Or create a URL generating function

      <?php
          generateLink($uri){
               return getDomain().$uri;
          }
      ?>
      

      Then call it like this

      <a href = '<?= generateLink('/some/uri') ?>'>Go here</a>
      

      Keep in mind that not all links on the same page would have the same domain, you need to handle that your self if you want all links to point to the same domain in each request.

    Of course the whole thing is just messy, but then the feature it self is strange so I don't expect any thing much cleaner to do this.

    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题