为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用正则表达式选项将我的一个页面重定向到另一个网站的随机页面?

如何使用正则表达式选项将我的一个页面重定向到另一个网站的随机页面?

PHP
qq_花开花谢_0 2022-12-30 16:00:47
我想将页面重定向到example.net/path路径是1 到 5127 之间的随机数的位置。例如: http://www.example.com/bin ----重定向到----> https://example.net/3417然后我想将每个重定向的页码增加 10,如本例中从 1000 路径开始:http://www.example.com/bin ----重定向到----> https://example.net/1010 http://www.example.com/bin ----重定向到----> https://example.net/1020 http://www.example.com/bin ----重定向到----> https://example.net/1030等等,据我所知,这可以通过htaccess或简单的php 页面来完成。
查看完整描述

1 回答

?
翻翻过去那场雪

TA贡献2065条经验 获得超14个赞

这将实现你所追求的。它利用 php 会话来跟踪您要重定向到的号码。


<?php

session_start();


// Check whether you've already redirected a user and if not, create a random number between min and max

$MIN = 1;

$MAX = 5127;

$rand = $_SESSION['last_redirect'] ?? rand($MIN,$MAX);


// Check whether you're within 10 of the maximum or not and if so, generate a new random number, if not add 10 on.

$rand = ($rand <= ($MAX-10)) ? $rand+10 : rand($MIN,$MAX);


// Store the value in a session for the next time the page is loaded

$_SESSION['last_redirect'] = $rand;


// Redirect ...

$redirect_to = 'https://example.net/'.$rand;

header("refresh:0; url=$redirect_to");

?>


这是一个实际的例子:

1) 导航到 php 页面:

//img1.sycdn.imooc.com//63ae9ac90001ac1605560465.jpg

2) 重定向到 example.net 并附加一个随机数:

//img1.sycdn.imooc.com//63ae9add0001f4fb05570467.jpg

3) 导航回 php 页面(第 1 步)并再次重定向但增加 10:

//img1.sycdn.imooc.com//63ae9aea000190fb05570465.jpg

这将一直增加 10,直到您清除 php 页面的浏览历史记录(或取消设置会话变量),或者直到达到您指定的最大数量。一旦您在最大值的 10 以内,它就会生成一个新的随机数并将您重定向到该随机数。



查看完整回答
反对 回复 2022-12-30
  • 1 回答
  • 0 关注
  • 81 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信