0

I've put together this short little code that should randomly pick a url from "url.txt" and then redirect it through a javascript redirect. For whatever reason I can't get it to work. I'm aware of how to get it to work with other forms of redirection including meta refresh and php header, however I would like to know how to do with with javascript redirection.

<?php 
   $loadlist = explode("\n", file_get_contents('urls.txt'));
   $rand = rand(0,count($loadlist)-1);
   $picked = $loadlist[$rand];
?>
<html>
<head>
<script type='text/javascript'>
  window.location.href = '<?php echo $picked ?>'
</script>
</head>
</html>
bakingsoda
  • 57
  • 7
  • missing `;` - both in js and php - typo. Possible duplicate [window.location.href](https://stackoverflow.com/questions/7077770/window-location-href-and-window-open-methods-in-javascript) – lovelace Nov 09 '19 at 20:40
  • Both `;` are optional in this case (it's a matter of preference, though not having them in JS might not work in older browsers?). @bakingsoda This code should work, are you sure your redirect URL is valid/correct? Check the source. – Jeto Nov 09 '19 at 20:57
  • I would recommend always using the semi-colon, but it is optional in this case. – lovelace Nov 09 '19 at 21:03
  • most likely what's happening is.. you mention `url.txt`, but you're using `urls.txt`, prob got error reporting off so the array is empty and looking for `0` or `-1` index in an empty array. `$picked` then is obviously empty – Lawrence Cherone Nov 09 '19 at 21:04
  • it has nothing to do with semi-colons; but is it! ;p – Lawrence Cherone Nov 09 '19 at 21:04
  • `;` is optional in JS. The final `;` is optional in PHP when the closing tag is on the same line as the last statement. What is your PHP script outputting? Is the redirect blocked by the browser? Browsers sometimes block JS redirects to protect against potentially malicious scripts. – Josef Engelfrost Nov 09 '19 at 21:04
  • does url located outside your web site? – Ahmed Anter Nov 09 '19 at 21:19

1 Answers1

0
<?php 
   $loadlist = explode("\n", file_get_contents('urls.txt'));
   $rand = rand(0,count($loadlist)-1);
   $picked = $loadlist[$rand];
?>
<html>
<head>
<script type='text/javascript'>
  window.location.replace("<?php echo $picked ?>");
</script>
</head>
</html>

Try this