 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
06-23-2009, 02:06 AM
|
#1 (permalink)
|
|
The Contributor
Join Date: Apr 2008
Posts: 78
Thanks: 0
|
Simple script fails with php5
I have a script that reads the file contents of a site. My host recently upgraded to php 5 and the script doesn't work correctly. It still runs without errors but takes much longer and no results are found. I've narrowed down the code to this
Code:
$file = @fopen($filename, "r");
if (!$file)
{
echo "<p>Unable to open remote file $filename.\n";
}
else
{
while (!feof($file))
{
}
}
If I remove the while (!feof($file)) line, the script executes quickly. If I leave it in, it takes almost a minute before it returns. I checked the php manual to see if there is a difference betwen these functions for php 4 and 5 but didn't find anything. Does anyone have any ideas as to why this fails when php 5 is enabled or any suggestions on another method?
|
|
|
|
06-23-2009, 10:03 AM
|
#2 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
If you replaced that code with the code below, is it any faster?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
06-23-2009, 10:11 AM
|
#3 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
Since you say that the problem appears to lie with the feof line (the @ before fopen won't be helping at all) it appears to be a timeout problem. See the warning notes on the feof manual page. The second one may be the exact problem that you're facing (suppressed fopen error, infinite feof loop until timeout).
For a start, remove the @ before fopen and see what error is being generated as I suspect there will be one.
|
|
|
|
06-24-2009, 02:54 AM
|
#4 (permalink)
|
|
The Contributor
Join Date: Apr 2008
Posts: 78
Thanks: 0
|
Thanks for the replies. I tried both of the suggestions but got different results from before. I must not have commented out the next line in the code since that is where it is failing - on an eregi call. What I did was add some code to record the results of that call. In php 4, the results range from FALSE to some number. In php 5, they are always FALSE. The reason for the delay is that since the result isn't being found, it has to cycle through all of the pages. In php 4, the result is found on the first page so it stops. So the problem is with that call. The code looks like this:
Code:
$fp = fopen("track.txt","w");
$file = fopen($filename, "r");
if (!$file)
{
echo "<p>Unable to open remote file $filename.\n";
}
else
{
while (!feof($file))
{
//this records the result of the eregi call
if ($fp)
fputs($fp,(eregi("<cite>(.*)</cite>", $var, $out)."\n"));
//this is the normal code
if (eregi("<cite>(.*)</cite>", $var, $out))
{
}
}
}
I looked up the eregi command and there is a note that says,
Quote:
|
Note: As of PHP 5.3.0 the regex extension is deprecated, calling this function will issue an E_DEPRECATED notice.
|
but I don't see any other mention of "regex extension" on that page and I don't know what it refers to so I don't know if that applies here or not. Does nayone see a problem with the way that line is being used?
|
|
|
|
06-24-2009, 03:27 AM
|
#5 (permalink)
|
|
The Addict
Join Date: May 2009
Posts: 287
Thanks: 5
|
Code:
"<cite>(.*)</cite>" <-- Regular Expression [ The (.*) part ]
Also your last if() { } statement is left blank.
|
|
|
|
06-24-2009, 04:25 PM
|
#6 (permalink)
|
|
The Contributor
Join Date: Apr 2008
Posts: 78
Thanks: 0
|
Thanks, but I don't understand your reply. Is there something wrong with "<cite>(.*)</cite>"? It works find in php4. Does something need to be changed for it to work with php5?
The second if is empty because it doesn't affect this problem and would just clutter the example.
|
|
|
|
06-24-2009, 05:57 PM
|
#7 (permalink)
|
|
The Addict
Join Date: May 2009
Posts: 287
Thanks: 5
|
I don't think anything is wrong, its just that PHP 5.3 will not accept that regular expression/code block.
Code:
Note: As of PHP 5.3.0 the regex extension is deprecated, calling this function will issue an E_DEPRECATED notice.
|
|
|
|
06-24-2009, 06:44 PM
|
#8 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
Quote:
Originally Posted by benton
I looked up the eregi command and there is a note that says, … but I don't see any other mention of "regex extension" on that page and I don't know what it refers to…
|
The "regex extension" being referred to is the POSIX regex extension which is deprecated as of PHP 5.3.0, but any earlier versions should work for you. The extension is home to functions such as ereg, eregi, split and sql_regcase.
Can you call eregi in a basic script and get the desired results? For example, does the following script simply echo 3?
PHP Code:
<?php
echo ereg('php', 'elephpant', $match);
?>
Also, does $var contain the same value(s) when ran on both PHP versions?
|
|
|
|
06-27-2009, 04:08 PM
|
#9 (permalink)
|
|
The Contributor
Join Date: Apr 2008
Posts: 78
Thanks: 0
|
I tried
PHP Code:
echo ereg('php', 'elephpant', $match);
with both php versions and they both returned 3. To give a better idea of this problem, I created two sub-directories, one with php4 and one with php5 (both have phpinfo.php files if you want to see the server info). I edited my code to just include the relevant information but still enough to see the failure. That code is below and the same file has been uploaded to both of the directories. So if you go to http://link-my-site.com/version/php4/position.php, you'll see "opened" displayed and then a bunch of text, finally ending with the searched for site and "closed." If you go to http://link-my-site.com/version/php5/position.php, all that is displayed are the words "opened" and "closed."
Here's the code used for both of the above:
PHP Code:
<?php
$conditions = "<cite>(.*)</cite>";
$hits_per_page = 10;
$siteName = 'Google';
$query = "wholesale+candy";
$position = 0;
$real_position = 0;
$found = NULL;
$lastURL = NULL;
$searchurl = 'mycandysupplier.com';
$siteresults = array();
$out = array();
for($i=0;$i<10 && empty($found);$i+=$hits_per_page)
{
$filename = "http://www.google.com/search?as_q=$query".
"&num={$hits_per_page}&hl=en&ie=UTF-8&btnG=Google+Search".
"&as_epq=&as_oq=&as_eq=&lr=&as_ft=i&as_filetype=".
"&as_qdr=all&as_nlo=&as_nhi=&as_occt=any&as_dt=i".
"&as_sitesearch=&safe=images&start=$i";
$file = @fopen($filename, "r");
if (!$file)
{
echo "<p>Unable to open remote file $filename.\n";
}
else
{
echo 'opened<br>';
while (!feof($file)) // load the file into a variable line at a time
{
$var = fgets($file, 1024);
if (eregi($conditions,$var,$out)) // find the html code this SE uses to show the site URL
{
echo 'get '.$out[1].'<br>';
// highlight search terms within URLS
$out[1] = strtolower(strip_tags($out[1]));
// Get the domain name by looking for the first /
if (($x = strpos($out[1],"/")) !== FALSE)
{
if (strstr($out[1], "https"))
{
$x += 2;
$x = strpos($out[1],"/", $x);
}
}
else
$x = strlen($out[1]);
// and get the URL
$url = substr($out[1],0,$x);
$url = substr($url, 0, strlen($searchurl));
$position++;
// If the last result process is the same as this one, it
// is a nest or internal domain result, so don't count it
// on $real_position
if(strcmp($lastURL,$url)<>0)
$real_position++;
$lastURL = $url;
// Else if the sites match we have found it!!!
if(strcmp($searchurl,$url)==0)
{
$found = $position;
break; // quit - no need to go any further.
}
else
{
if (strpos($searchurl, "www") !== FALSE)
$search_alt = substr($searchurl, 4, strlen($searchurl) - 4);
else
$search_alt = sprintf("www.%s",$searchurl);
$url_alt = substr($out[1],0,$x);
$url_alt = substr($url_alt, 0, strlen($search_alt));
if(strcmp($search_alt,$url_alt)==0)
{
$found = $position;
break; // quit - no need to go any further.
}
}
}
}
echo 'closed';
fclose($file);
}
}
$result_google = "The site $searchurl is at position $found ".
"( $real_position ) for the term <b>$query</b>" . " on " . "$siteName";
?>
This isn't relevant to the above but when I tried adding the url using the url option, my virus scanner popped up so I add to add them manually.
|
|
|
|
07-12-2009, 12:26 PM
|
#10 (permalink)
|
|
The Contributor
Join Date: Apr 2008
Posts: 78
Thanks: 0
|
Can no one see the reason for the failure in this code?
|
|
|
|
07-12-2009, 01:02 PM
|
#11 (permalink)
|
|
The Prestige
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
|
I know this probably won't help you much, but the script seems to run fine on my PHP5 installation.
Try replacing the eregi with preg_match, of course you would have to change your expression somewhat:
PHP Code:
$conditions = '/<cite>(.*)<\/cite>/i';
Can't really find anything else that would cause a problem.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
|
|
|
|
07-23-2009, 06:23 PM
|
#12 (permalink)
|
|
The Contributor
Join Date: Apr 2008
Posts: 78
Thanks: 0
|
Thanks to all those who helped. I was finally able to determine that fopen was causing the problem, although I don't know why. I did a search on google and see others are having the same problem but no fix was mentioned. I ended up replacing fopen with a curl read and it is woking now.
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|