0

I am trying to generate a random number (between 1 - 151) so I can open a page based on a randomly generated ID. The issue here is with browser compatibility because the intended functionality works on Edge but no Chrome or FireFox.

pokedex.php:

<form action='randomPokemon.php' >
    <button type="submit" class="toolsBtn">Random</button>
</form>

randomPokemon.php:

<?php
$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'pokedex.php?pid=';
$randomid = rand(1, 151);
header("HTTP/1.1 303 See Other");
header("Location: http://$host$uri/$extra" . mt_rand(1, 151));
unset($randomid);
exit;
?>

Live Demo at: http://fraserprovan.co.uk/projects/pokedex/pokedex.php

I have no idea why it works on Microsoft Edge but not other browsers. On chrome it opens the same page everytime as if the varaible $randomid doesnt change. Any advice or links to other resources will be greatly appreciated.

UPDATE: decided to do it in Javascript instead using the following function, Thanks for the suggestions.

function randomPokemon(){
    var randomNum = Math.floor(Math.random() * 151) + 1;  
    document.location.replace('pokedex.php?pid=' + randomNum);
}
Fraser
  • 52
  • 4
  • When I try it in Chrome and Firefox gets the same result as you do. Very odd indeed. – Mr Glass Jun 23 '18 at 21:09
  • I dont know what the difference is about using it in edge, im scratching my head. – Fraser Jun 23 '18 at 21:12
  • When calling `randomPokemon.php` the URL changes to `pokedex.php` as expected. This happens even if I paste the URL with `randomPokemon.php`. – Mr Glass Jun 23 '18 at 21:16
  • Have you tried logging the random numbers generated to make sure something isn't whacky in the PHP? I'm getting odd, but very reproducible, behavior. I always get #82 when clicking `Random`, but I always get #39 if I access the page from the console using `location.href = "http://fraserprovan.co.uk/projects/pokedex/randomPokemon.php"`. – Mr Glass Jun 23 '18 at 21:30
  • How would you recommend logging? I know how to write to console in javascript but not in php – Fraser Jun 23 '18 at 21:35
  • Its cache. Refer [Non cached Redirect](https://stackoverflow.com/questions/12192590/how-to-do-a-non-cached-301-redirect) – SirPilan Jun 23 '18 at 21:40
  • @Pilan see original post for updated code. So I looked at what you sent but found it quite hard to follow. I found this post regarding 303 redirects: https://stackoverflow.com/questions/13640109/how-to-prevent-browser-cache-for-php-site I now get this in the chrome dev tools for the script: 303 See Other (from disk cache) but the from disk cache only appears on the 2nd+ time the button is pressed. Do you see anything wrong with the way I have implemented this? – Fraser Jun 23 '18 at 22:48
  • @Fraser tried `header('Expires: Sun, 01 Jan 2014 00:00:00 GMT'); header('Cache-Control: no-store, no-cache, must-revalidate'); header('Cache-Control: post-check=0, pre-check=0', FALSE); header('Pragma: no-cache');` ? – SirPilan Jun 23 '18 at 23:20

1 Answers1

0

Try to use absolute URI instead, also don't leave any space or new line before header-statement. See this note from php-docs

Note:

Most contemporary clients accept relative URIs as argument to » Location:, but some older clients require an absolute URI including the scheme, hostname and absolute path. You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself:

<?php
/* Redirect to a different page in the current directory that was requested */
$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'mypage.php';
header("Location: http://$host$uri/$extra");
exit;
?>
Jehad Nasser
  • 3,227
  • 5
  • 27
  • 39
  • I updated the randomPokemon.php: code. Thanks for the tip. I updated the original post with the updated code. Unfortunatly same error is happening. – Fraser Jun 23 '18 at 21:24
  • Try this: header("Location: http://$host$uri/$extra" . mt_rand(1, 151)); instead of $randomid = rand(1, 151); if it didn't work please try using fixed number and keep me posted. – Jehad Nasser Jun 23 '18 at 21:43
  • Updated this line of code, same issue. Another user mentioned caching, im just trying to understand how it works. Could be the issue. – Fraser Jun 23 '18 at 22:39
  • I can see it is working on chrome now, how you fixed it? – Jehad Nasser Jun 24 '18 at 06:25