TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-23-2009, 02:06 AM   #1 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 76
Thanks: 0
benton is on a distinguished road
Default 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?
benton is offline  
Reply With Quote
Old 06-23-2009, 10:03 AM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,215
Thanks: 90
Wildhoney is on a distinguished road
Default

If you replaced that code with the code below, is it any faster?

php Code:
echo file_get_contents('http://www.google.com/');
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 06-23-2009, 10:11 AM   #3 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,239
Thanks: 3
Salathe is on a distinguished road
Default

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.
__________________
salathe@php.net
Salathe is offline  
Reply With Quote
Old 06-24-2009, 02:54 AM   #4 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 76
Thanks: 0
benton is on a distinguished road
Default

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?
benton is offline  
Reply With Quote
Old 06-24-2009, 03:27 AM   #5 (permalink)
The Addict
 
adamdecaf's Avatar
 
Join Date: May 2009
Posts: 308
Thanks: 5
adamdecaf is on a distinguished road
Default

Code:
"<cite>(.*)</cite>" <-- Regular Expression [ The (.*) part ]
Also your last if() { } statement is left blank.
__________________
My Site
adamdecaf is offline  
Reply With Quote
Old 06-24-2009, 04:25 PM   #6 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 76
Thanks: 0
benton is on a distinguished road
Default

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.
benton is offline  
Reply With Quote
Old 06-24-2009, 05:57 PM   #7 (permalink)
The Addict
 
adamdecaf's Avatar
 
Join Date: May 2009
Posts: 308
Thanks: 5
adamdecaf is on a distinguished road
Default

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.
__________________
My Site
adamdecaf is offline  
Reply With Quote
Old 06-24-2009, 06:44 PM   #8 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,239
Thanks: 3
Salathe is on a distinguished road
Default

Quote:
Originally Posted by benton View Post
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?
__________________
salathe@php.net
Salathe is offline  
Reply With Quote
Old 06-27-2009, 04:08 PM   #9 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 76
Thanks: 0
benton is on a distinguished road
Default

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($file1024);

              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($url0strlen($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($searchurl4strlen($searchurl) - 4);
                  else
                    
$search_alt sprintf("www.%s",$searchurl);

                         
$url_alt substr($out[1],0,$x);
                  
$url_alt substr($url_alt0strlen($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.
benton is offline  
Reply With Quote
Old 07-12-2009, 12:26 PM   #10 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 76
Thanks: 0
benton is on a distinguished road
Default

Can no one see the reason for the failure in this code?
benton is offline  
Reply With Quote
Old 07-12-2009, 01:02 PM   #11 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 836
Thanks: 31
sketchMedia is on a distinguished road
Default

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)
sketchMedia is offline  
Reply With Quote
Old 07-23-2009, 06:23 PM   #12 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 76
Thanks: 0
benton is on a distinguished road
Default

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.
benton is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating a Simple Currency Converter with Automatic Symbols Wildhoney General 11 03-16-2010 05:22 PM
Part 2: Giving our Currency Conversion Script some Responsibility Wildhoney General 15 03-17-2009 01:53 PM
My first script ever , critisize !! webtuto Show Off 11 02-21-2008 12:06 PM
hash() algorithm info script RobertK Script Giveaway 4 01-09-2008 02:00 PM
Script Execution Time Class Wildhoney Script Giveaway 2 09-14-2007 11:36 PM


All times are GMT. The time now is 12:21 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design