There is an HTML page. Here is a part of the code (iframe cannot be inserted here):

<iframe id="test" src="link" frameborder="0" width="100%"></iframe> 

I need to make it so that the link value is taken from the file link.txt, as a random string. Link.txt is in the same directory as the HTML file.

The link.txt file will contain 9 lines with URL links and one line with a variable that takes the value from the general_links.txt file in the root folder. Also 1 random line from file.

I understand that you need to process PHP or JS, but it does not work.

  • will you have a variable in the file? What is it like. In the file or there is a record or it is not. And why do you need variables in the context of this task? - torokhkun
  • From the file download links for iframe - Cyril
  • и одна строка с переменной, которая берет значение с файла general_links.txt . How is the variable in your static file? or you generate a file every time you access it. and what is the role of this variable? - torokhkun
  • See, there is a link. When you click on it, the iframe is loaded - now it is a static link to the iframe, I want to make it dynamic. So that when entering the same page a random iframe is loaded. - Cyril

1 answer 1

Try this:

 <?php $f = file("link.txt"); $random_line = $f[rand(0, count($f) - 1)]; ?> <iframe id="test" src="<?php echo $random_line; ?>" frameborder="0" width="100%"></iframe> 

It is also worth adding URL filter_var($random_line, FILTER_VALIDATE_URL); (I did not insert this line in the above code, because I don’t know what you want to do if the URL is incorrect).

  • It did not work, it gave an error that the requested URL was not found on the server. The validation of the URL is not added. because all urls are supposed to be working. I looked at the page code in the browser - the url did not load and the value "<? Php echo $ random_line;?>" Remained - Cyril
  • @ Kirill It seems that php does not work for you at all. Maybe you are trying to insert it into the .html file? - s976
  • Yes, that's exactly what I'm trying to do ... As far as I understand now, you need to create a php file and write this code into it - Cyril
  • Right. The only question is where will you run it (this php file). If you try to start it from your computer, it will not work. But if you have it on your server, it will most likely work. - s976
  • on server. Could you please tell me: Should I call him something especially? Or do you need to prescribe the path to it? - Cyril