How Can I Make A Link Load A Random Php Id On The Page?
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
Solution 1:
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" );
Solution 2:
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()
Solution 3:
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" );
Post a Comment for "How Can I Make A Link Load A Random Php Id On The Page?"