is there any way to use printrand() or something similar to generate random numbers from开发者_如何学Python inside of a url call in a php script or html page? Basically, have a url, but on each load, I need the page to call url(dot)com/blah####blah with the #s being random each time.... How can I accomplish this?
When a page is requested by a URI (URL), the URI cannot be changed by a server-side language by any means other than redirection. You could, then redirect a request to http://mydomain.com
to http://mydomain.com/RANDOM
with code such as this:
$random = mt_rand();
header('location:http://mydomain.com/'.$random);
Be aware, however, that any random-number generating scheme is not going to be truly, 100% random, nor are you guaranteed that the random values will be unique for the same user or for different users. There is a possibility (and not a small one) that a random number will be duplicated.
Other than utilizing redirection, there is no way to change, server-side, the request URL after the request has been sent. That said, you can also push browser history states with javascript after the page has loaded. See the (docs), but be aware that this is new functionality; it has not been implemented on all user-agents, will NOT be implemented in older user-agents (Internet Explorer < 8), and is still in draft status (mechanics and API may change).
You can append strings in PHP with a period. And you can use PHP's rand(min, max) function. Something like this:
$url = "http://www.url.com/" . rand(1, 10);
Further, you probably only want whole numbers, so use ceil() or round():
$url = "http://www.url.com/" . ceil(rand(1, 10));
精彩评论