Home > Article > Backend Development > PHP page random jump method
php page random jump method
1. First use array to store all urls in the array;
2. Then use array_rand to randomly obtain an array subscript;
3. Finally, use the header combined with the randomly obtained URL to jump to the page.
The code is as follows:
<?php $arr = array( 'https://www.baidu.com', 'https://cn.bing.com/', 'http://google.com/' ); $key = array_rand($arr, 1); //输出随机内容 // echo $arr[$key]; header('Location: ' . $arr[$key]); exit;
Note:
● There cannot be any output before the header is executed
● There cannot be a space between location and:
● The php code after the header will still be executed, so you need to pay attention to using exit
Note: During browser testing, due to browser cache, it is not possible to jump to different addresses every time. Use during testing. http://localhost/test.php?id=1, and constantly changing the value of the id to change the url can solve the problem of invisible effects caused by browser caching.
For more PHP related knowledge, please visit PHP Chinese website!
The above is the detailed content of PHP page random jump method. For more information, please follow other related articles on the PHP Chinese website!