Page 1 of 1

Opening URLs in a browser automatically

Posted: Thu Oct 13, 2005 4:08 pm
by Gordicron
feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hi,
I'm relatively new to PHP coding so please excuse me if this is a stupid question! 

Well, I'm writing a piece of code that reads a list of URLs from a text file and then outputs them in a browser, one at a time at set intervals. Kind of like a slideshow but with websites instead of pictures. 

I'm using a while loop to process the URLs, so everytime the loop runs a variable acts as a counter and gets a 1 added to it, so that the next URL in the array can be fetched the next time. 

The program reads the URLs in the text list into an array just fine and the loop works too - I've just been getting it to output what it's doing to the screen at the moment, so that I can see it's working.

Well, this brings me on to the problem - I need to get the program to actually take the URLs in the array and fetch the actual website into the user's browser. I'm not too sure how to go about this. 

What I want is for the user to see a webpage with two frames. the top will have the PHP code and the dialogue the code generates now (this is just stuff like "now showing URL 1, URL 2, etc.") so that the user can see what the program is doing as it runs. The bottom frame will actually show the websites in it. So say, for example, URL 1 in the array is http://www.yahoo.co.uk - the top frame will say "Now showing URL 1" and Yahoo will be showing the bottom frame, and so forth.  As I said, I have the top frame working - it's just the bottom one that's the problem!

I've tried putting Javascript into the loop so that it would be executed everytime the loop ran, and would output the URL into the bottom frame but this doesn't seem to work - it just seems to be ignored and nothing happens - the top frame works but the bottom stays blank.

I've also wondered if it's possible to simply call the frame in HTML and somehow add the URL in as a variable - but not sure if this is possible?

So, I guess what I'm asking is: How do I get the PHP code to open a URL in the bottom frame and also is it possible to use a variable as the address for the URL to refer to - so say would you be able to have something like "HREF=$showthisurl" (I know that's not valid code but just as an illustration of what I'm jabbering on about)?

Also, is it possible to put Javascript code into a While loop in PHP code so it will execute everytime the loop is executed, and furthermore is it possible to take a variable from the PHP code and use it in that Javascript code? I've seen some examples while Googling but none of them are particularly clear on this.

Here's my While loop code as it is right now with my attempt at adding the Javascript in (which seems to do nothing!):

Code: Select all

while ( $arraycount >= $processcount ) {
print("Now processing url number ");
print $processcount;
print(" </BR>");
print("<SCRIPT LANGUAGE = JAVASCRIPT>");
print("parent.main.location.href = (<? $processurl; ?>)");
print("</SCRIPT>");
$processcount=$processcount + 1;
}
As you can see, I tried to use the variable from the PHP code which holds the value of the URL to be processed (the $processurl variable). It was from a tutorial I found on Google but it wasn't very clear so it may well be wrong! Also it's been a while since I used Javascript!

Anyway, I hope that someone can help or provide some pointers for this as I've been trying stuff out for about 4 hours now and my brain's gone numb! :D

- Gordon


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Fri Oct 14, 2005 9:46 am
by Gordicron
Any ideas? Anyone? Really need help with this! :?

- Gordon

Posted: Fri Oct 14, 2005 9:58 am
by foobar

Code: Select all

while ( $arraycount >= $processcount ) {
print("Now processing url number ");
print $processcount;
print(" </BR>");
print("<SCRIPT LANGUAGE = JAVASCRIPT>");
print("parent.main.location.href = (<?= $processurl; ?>)");
print("</SCRIPT>");
$processcount=$processcount + 1;
}
This, however, won't work because the while loop will be terminated when you go to the page you want to "fetch".

Here's a solution:

Code: Select all

<?php

  $urls = array();

  //You retrieve your array of urls somehow...
?>
<html>
<head>
  <title />
</head>
<frameset ... >
  <frame name="theframe" src="start_page.html" ... />
</frameset>
<script type="text/javascript">
<?php
foreach ($urls as $url) {
  echo 'theframe.src="'.$url.'";';
  flush(); //Flushes php output to browser
  sleep(5); //Time is in seconds, ie. 5 seconds.
}
?>
</script>
</html>
Hasn't been tested but should work.

Posted: Fri Oct 14, 2005 2:27 pm
by Gordicron
Thanks for your reply! :)

I'm a little confused about the placing of the <html> and <?php> tags in your code though - would I put the HTML, Frameset stuff after my PHP code (the stuff that creates the array with the URLs in it) and then follow it with the

Code: Select all

<?php
foreach ($urls as $url) {
  echo 'theframe.src="'.$url.'";';
  flush(); //Flushes php output to browser
  sleep(5); //Time is in seconds, ie. 5 seconds.
}
?>
?

I've tried to integrate it with my code and the output I get is:

Code: Select all

theframe.src="http://www.google.co.uk ";theframe.src="http://www.yahoo.com ";theframe.src="http://www.play.com ";theframe.src="http://www.zeropaid.com ";
which appears in the top frame with everything else. Looks like that part is working but it doesn't seem to actually put it into the intended frame. :?

Anyway, thanks again for your help so far - I seem to be getting near to getting this working now! :)

- Gordon

Posted: Fri Oct 14, 2005 2:34 pm
by foobar
Try this instead

Code: Select all

<?php

  $urls = array();

  //You retrieve your array of urls somehow...
?>
<html>
<head>
  <title />
</head>
<frameset ... >
  <frame name="theframe" src="start_page.html" ... />
</frameset>
<?php
foreach ($urls as $url) {
  echo '<script type="text/javascript">theframe.src="'.$url.'";</script>';
  flush(); //Flushes php output to browser
  sleep(5); //Time is in seconds, ie. 5 seconds.
}
?>
</html>

Posted: Fri Oct 14, 2005 4:24 pm
by Gordicron
Still doesn't seem to be working for some reason. :( I've stripped the code right down to the absolutely essential stuff and this is what I have:

Code: Select all

<?
$array=file('process.txt'); //This tells PHP to load the text file line by line as an array
?>

<html>

<frameset rows="*,450">
<frame name="theframe" src="start_page.html">
</frameset>

<?
foreach ($array as $url) {
  echo '<script type="text/javascript">theframe.src="'.$url.'";</script>';
  flush(); //Flushes php output to browser
  sleep(5); //Time is in seconds, ie. 5 seconds.
}
?>
</html>
Does this look correct or have I missed something? When I try and load the page in a browser (Firefox), I get the two frames but nothing seems to happen after that. On Internet Explorer, I get the same result except that the bottom frame is greyed out.

- Gordon

Posted: Sun Oct 16, 2005 12:33 pm
by Gordicron
I've been playing about with the above code over the weekend and have gotten to the point where I get the following when I click 'View Source' on Internet Explorer:

Code: Select all

<html>

<frameset rows="*,450">
<frame name="theframe" src="start_page.html">
</frameset>
</html>

<SCRIPT LANGUAGE = JAVASCRIPT>theframe.src = http://www.google.co.uk</SCRIPT>
So it looks like the code IS working - in so far as the browser receives the code with the correct URL to show. The problem is that the page (in this case http://www.google.co.uk) isn't appearing in the frame. All I see is two blank frames. I wait for the frame to change to http://www.google.co.uk but it doesn't do it.

How come the browser receives the code (as seen in 'View Source') but doesn't execute it? :? Anyone have any ideas?

- Gordon

Posted: Sun Oct 16, 2005 12:39 pm
by feyd

Code: Select all

<script language = "Javascript">
  document.frames['theframe'].src = 'http://www.google.co.uk';
</script>

Posted: Sun Oct 16, 2005 3:03 pm
by Gordicron
Thanks! :)

Okay, here's what is output from Internet Explorer when I do 'View Source' now:

Code: Select all

<html>

<frameset rows="*,450">
<frame name="theframe" src="start_page.html">
</frameset>


<SCRIPT LANGUAGE = "JAVASCRIPT">
 document.frames['theframe'].src  = 'http://www.google.co.uk';
 </SCRIPT>

</html>
Looks like the PHP is working and that the Javascript is getting the variable from the PHP array (hence the http://www.google.co.uk). Unfortunately, the Javascript still isn't being executed. :(

It's getting through to the browser, hence the output above from 'View Source' but it still won't run and open the http://www.google.co.uk webpage. Is there something I'm missing?

- Gordon

Posted: Sun Oct 16, 2005 3:18 pm
by feyd
may need to tell the window object the frame creates to change url's instead of the frame tag..

Code: Select all

document.frames['theframe'].location = 'something';
I can't quite remember how it's done since I never work with frames..

check to ensure that the javascript is in fact being run too (use an alert()), as the browser may be ignoring your code...

Posted: Mon Oct 24, 2005 2:49 pm
by Gordicron
Thanks again for the help :)

Well, I've spent the past week trying this and a number of different things and it just seems that the browser ignores whatever code the PHP script tries to get to the browser using 'echo' (or 'print'). I've tried the Javascript as posted here and also tried just getting PHP to 'echo' some html too but it just won't execute it. It's weird, as the browser seems to receive it (hence the fact that I can see the code when I 'view source' in the browser).

I've tried Firefox and Internet Explorer, made sure Javascript is enabled - also tried accessing from other PCs but it just won't go. :(

Anybody have any ideas on why this might be? Is there a setting I have to enable somewhere on the PHP server to allow this to work?

- Gordon

Posted: Tue Oct 25, 2005 9:00 am
by feyd
got a live version we can look at?