How can I make a link load a random php ID on the

2019-09-14 03:57发布

Database row IDs are tied to the URLs like this:

The page for row 2 becomes site.com/index.php?id=2

If I manually refresh index.php it displays a random record, but naturally it doesn't display the record ID in the address bar.

Is there a way to make a link display a random record while showing the id change in the address bar?

Thank you for your help!

3条回答
地球回转人心会变
2楼-- · 2019-09-14 04:19

Instead of just showing the random record, choose the record's id and do a redirect to the version that shows the id.

$randomId = // whatever method you use to choose it
header( "Location: http://site.com/index.php?id=$randomId" );
查看更多
放荡不羁爱自由
3楼-- · 2019-09-14 04:37

There are multiple ways to do this.

You could like Juhana said, using the header function. This wil redirect you to another page by pagereloading.

You could use HTML5 to change the addressbar without pagereloading by using the window.history and jQuery for AJAX.

The safest option is the PHP header function. Keep in mind to NOT print/echo anything before you header. In case you still get "headers already sent" use ob_clean()

查看更多
我只想做你的唯一
4楼-- · 2019-09-14 04:41

Try this :

$randomId = rand(0,100); // if let's say your post IDs go from 0 to 100
header( "Location: http://www.yourdomain.com/post.php?id=$randomId" );
查看更多
登录 后发表回答