Im trying to right my first php script.

Im trying to get the script to look at a defined site's source to see if a length of code is present.

Ive been given this by a friend but im not sure its working as I get this error, and it always says it can see the code even though I made up some bogus search.

Warning: Wrong parameter count for strpos() in /home/#####/public_html/#######/test.php on line 3
Found it!

<?php
$source = file_get_contents("http://######.com/");
if(strpos('<a href="http://www.jimmy.com/"/>') !== false)
 echo "Found it!";
else
 echo "Didn't find it!";
?>

Any ideas on how I can progress this futher to a few textboxes and a button?

Thanks

I responded before looking at the code in depth. Let me look and I'll tell you more, I've already found three problems. :)

Member Avatar for diafol
$source = file_get_contents("http://######.com/");

You need to get the actual file if possible - but, it may default to the index.php, index.htm, main.php, main.htm, home.php, home.htm (or the html equivalents).

In addition your host may disable your ability to access cross-domain files in this manner (as a safety precaution).

Your conditional may work, but I advise using braces, { and }.

The strpos needs a haystack and needle, so you need to place $source into the strpos:

if(strpos($source,'<a href="http://www.jimmy.com/"/>') !== false){

Okay. This works.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>Page title</title>
</head>
<body>
<?php
$source = file_get_contents("http://placelogo.t35.com/index.php");
if(strpos($source,"function changeup()")!=false) {
 $begin=strpos($source,'function changeup()');
 $end=strpos($source,'</script>');
 $leng=$end-$begin;
 $result=nl2br(substr($source,$begin,$leng));
 echo "$begin, $end, $leng:<br />$result";
 }
else {
 echo "Didn't find it!";
 }
?>


</body>
</html>

Issues:

1. You need to specify the file you want to open, www.domain.com/ won't work.
2. I suspect the !== is what's causing your false positives, changing it to != seems
work much better.
3. strpos function needs two parameters. haystack, needle

That project looks like something a person could have a lot of fun with.

David

Now I'm curious as to why it isn't working when I put in the <title> tags or the <body> tags as a search. It returns the position numbers, but no text.

Does file_get_contents not pull the entire source file?

Member Avatar for diafol

strpos() only returns a number (char pos) if present. The $source contains the text. You need to use something like substr() to extract a specific piece of file. Either that or use regex.

Thanks, everyone.

<?php
$source = file_get_contents("http://bleepyevans.com/");
if(strpos($source,'bleepyevans') !== false)
{
echo "Present";
}
else
{
echo "Not Present";
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.