homepage Welcome to WebmasterWorld Guest from 54.204.142.143
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
cURL
dcool86




msg:4424734
 4:50 pm on Mar 4, 2012 (gmt 0)

Hello,

I'm trying to check if a website has a piece of code on it. The code is below.

<!-- BEGIN TEST CODE -->

I want to see if that is in the source code. If it is it will echo yes if not it will echo no.


Thank you

 

dcool86




msg:4424735
 4:53 pm on Mar 4, 2012 (gmt 0)

What will be the quickest and most reliable way of doing that to. Thank you

brotherhood of LAN




msg:4424828
 11:00 pm on Mar 4, 2012 (gmt 0)

Do you need a method to fetch the page or a method to match the string?

strpos [php.net] is good for the comparison

PHP has support for cURL [php.net] and there are examples on that page.

dcool86




msg:4424874
 4:06 am on Mar 5, 2012 (gmt 0)

I was wondering if you could put together a code to look for

<!-- BEGIN TEST CODE -->


What I have written/tried doesn't work.


Thank you

dcool86




msg:4425351
 2:52 am on Mar 6, 2012 (gmt 0)

This is what I have so far

$ch = curl_init("http://www.example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$text = curl_exec($ch);
$test = strpos($text, "<!-- BEGIN TEST CODE -->");
if ($test==false)
{
echo "yes";
}else{
echo "no";
}

I'm always getting a yes on any page even the one that has

<!-- BEGIN TEST CODE -->

what am I'm doing wrong?

dcool86




msg:4425366
 3:23 am on Mar 6, 2012 (gmt 0)

i got it to work but it doesn't seem to work on certain domains. Thanks

lostdreamer




msg:4428613
 2:38 pm on Mar 13, 2012 (gmt 0)



if(checkSite("http://www.google.nl"))
echo "Found";
else echo "not Found";

function checkSite($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$text = curl_exec($ch);
$test = strpos($text, "<!-- BEGIN TEST CODE -->");
if ($test === false)
return false;
else return true;
}


With strpos you should allways do a strict comparison ( === instead of == )

If the text would start with the text you are searching for, strpos will return 0 (since the search text starts at char 0)

if 0 == false returns TRUE
if 0 === false returns FALSE

This just might be why you script fails
On top of that, in your above script, the If Else logic is the wrong way around ;)

Regards,
LostDreamer

dcool86




msg:4429187
 7:45 pm on Mar 14, 2012 (gmt 0)

Thank you

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved