Jump to content

Find a phrase in a file


papaface

Recommended Posts

Hello,
I am currently writing a script that has to access a remote .txt file and find the domain it is hosted on in the text file.
When the domain is found in the text file the script will run. Each domain is on a new line in the text file.
The text file would be like this:
checker.txt
---------
www.phpfreaks.com
www.domain.com
www.site.com
---------

All of those domains in that text file would be registered to use the script.
How can I get the script to connect to that remote .txt file and find the domain is it registered on?
I have this so far:
[code]$file = file_get_contents("http://www.someurl.com/checker.txt");
$url = "www.domain.com";
$there = ereg("[$url]",$file);

if ($there = 1)
{
echo "Domain found<Br>";
}
else
{
echo "This URL is not licensed to run this script";
}

?>[/code]
^^ I dont think the above is correct.
Any help would be appreciated.

regards
Link to comment
Share on other sites

wow thanks that was simple :)
I do have one problem though.
if someone uses the script from "www.domain.com" but the domain in the text file is "domain.com" it still shows the domain as not being valid.
Basically I want to know how I can make it work for any address on that domain, as long as it has the main domain in it.
How can I get around this?
The code I have at the moment is:
[code]<?php

$domains = file("domaincheck.txt");
$domain = $_SERVER['HTTP_HOST'];
if(in_array($domain, $domains))
{
echo "Domain found<Br>";
//echo $there . "<br>";
//echo $_SERVER['HTTP_HOST'];
}
else
{
echo "This URL is not licensed to run this script";
}

?>[/code]
Link to comment
Share on other sites

I have a problem:
[code]<?php

$domains = file("http://www.domain.com/dir/validation/domaincheck.txt");
$domain = $_SERVER['HTTP_HOST'];
$domain = str_replace("www.","",$domain);
if(in_array($domain, $domains))
{
echo "valid";
}
else
{
echo "This URL is not licensed to run this script<br>";
echo $domain;
exit;
}

?>[/code]
I am running this from localhost. Therefore $_SERVER['HTTP_HOST'] = "localhost". I have entered "localhost" on a new line in the text file but it keeps returning "This URL is not licensed to run this script".
What could be causing this?

regards
Link to comment
Share on other sites

I must say I am clueless on this one. You say domain.com and site.com work, so this is really weird.
Running something like this, outputs "valid" for me:
[code]<?php

$domains = array("localhost", "domain.com", "site.com");
$domain = "localhost";
$domain = str_replace("www.","",$domain);
if(in_array($domain, $domains))
{
echo "valid";
}
else
{
echo "This URL is not licensed to run this script<br>";
echo $domain;
exit;
}

?>[/code]


Last thing I can possibly think of is adding trim() too all of the values. Try this:
[code]<?php

$domains = file("http://www.domain.com/dir/validation/domaincheck.txt");
$domains = array_map("trim", $domains);
$domain = $_SERVER['HTTP_HOST'];
$domain = str_replace("www.","",$domain);
if(in_array($domain, $domains))
{
echo "valid";
}
else
{
echo "This URL is not licensed to run this script<br>";
echo $domain;
exit;
}

?>[/code]


Orio.
Link to comment
Share on other sites

array_map() runs a function on all of the array's values. What the function trim() does, is remove any spaces (white-spaces, newlines and tabs) from the given values.

And now I understand why it worked. From the manual, about the function file():
[quote]Each line in the resulting array will include the line ending, so you still need to use rtrim() if you do not want the line ending present.[/quote]

You can replace "trim" with "rtrim" if you want to make things a little (very little) faster.
I should have thought about that newline thing... :)

Anyways, it works, and that's what important :D

Orio.
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.