Refresh Page until...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rishid
Forum Newbie
Posts: 4
Joined: Thu Jun 24, 2004 7:26 am

Refresh Page until...

Post by rishid »

Hello,

I am trying to parse a PHP page for a specific String, and if it exists stop refreshing. If it does not exist, refresh X seconds. Anything like this possible in PHP? The page I am parsing is located on another server, so I would probably Include that into this page first. Any ideas on how to get started?

Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

[php_man]curl[/php_man] with some [php_man]preg_match[/php_man]
rishid
Forum Newbie
Posts: 4
Joined: Thu Jun 24, 2004 7:26 am

Post by rishid »

Does this look somewhat right? One thing I couldn't figure out was how to show the webpage inside the IF statement? Is echo $tmp correct? Don't have PHP installed on this computer just yet, so will test it soon.

Code: Select all

<?php
$ch = curl_init("http://www.example.com/");
$fp = fopen("example_homepage.txt", "w");

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

$tmp = curl_exec($ch);
curl_close($ch);

if (substr_count("Word", $tmp) => 2)
{
	// Show Webpage
	echo $tmp;
	$fclose($fp);
}
else
{
	// Refresh Page
	$fclose($fp);
	header( 'refresh: 600; url=refresh.php' );
}
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'd use CURLOPT_RETURNTRANSFER instead of CURLOPT_FILE
rishid
Forum Newbie
Posts: 4
Joined: Thu Jun 24, 2004 7:26 am

Post by rishid »

I got almost everything working, only problem is the site sets a cookie on the first time you visit the page. So I tried to use curl's cookie functions to emulate this sending of the cookie back to the server but isn't working. Not sure if I am even using it right. Here is the code, thanks for your time.

Code: Select all

<?php
$user_agent = "Mozilla/4.0";

$ch = curl_init("http://www.notsureyet.com/");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, "c:\cookie.txt"); 
curl_setopt($ch, CURLOPT_COOKIEFILE, "c:\cookie.txt");
curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);

// curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

$tmp = curl_exec($ch);
curl_close($ch);
//echo $tmp;

if (substr_count($tmp, "findme") == 1)
{
// Show Webpage
echo $tmp;
}
else
{
// Refresh Page
header( 'refresh: 1000; url=refresh.php' );
}

?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

what's the cookie used for?
rishid
Forum Newbie
Posts: 4
Joined: Thu Jun 24, 2004 7:26 am

Post by rishid »

Um.. all I know is that the first time you visit the page ever, it sets a cookie and then if you visit the page again, I am guessing it checks for that cookie and if it exists, displays a different page. Kind of like logging into an account at a website.
Post Reply