papaface Posted January 15, 2007 Share Posted January 15, 2007 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.comwww.domain.comwww.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 https://forums.phpfreaks.com/topic/34266-find-a-phrase-in-a-file/ Share on other sites More sharing options...
Orio Posted January 15, 2007 Share Posted January 15, 2007 [code]<?php$domains = file("checker.txt");$domain = "www.phpfreaks.com";if(in_array($domain, $domains) //existselse //doesnt ?>[/code]Orio. Link to comment https://forums.phpfreaks.com/topic/34266-find-a-phrase-in-a-file/#findComment-161149 Share on other sites More sharing options...
papaface Posted January 15, 2007 Author Share Posted January 15, 2007 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 https://forums.phpfreaks.com/topic/34266-find-a-phrase-in-a-file/#findComment-161155 Share on other sites More sharing options...
papaface Posted January 15, 2007 Author Share Posted January 15, 2007 Can anyone help me please? Link to comment https://forums.phpfreaks.com/topic/34266-find-a-phrase-in-a-file/#findComment-161277 Share on other sites More sharing options...
Jessica Posted January 15, 2007 Share Posted January 15, 2007 run str_replace() on the $domain to take out the www, and only store the address without it. Link to comment https://forums.phpfreaks.com/topic/34266-find-a-phrase-in-a-file/#findComment-161279 Share on other sites More sharing options...
papaface Posted January 15, 2007 Author Share Posted January 15, 2007 Thanks :) Link to comment https://forums.phpfreaks.com/topic/34266-find-a-phrase-in-a-file/#findComment-161288 Share on other sites More sharing options...
papaface Posted January 15, 2007 Author Share Posted January 15, 2007 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 https://forums.phpfreaks.com/topic/34266-find-a-phrase-in-a-file/#findComment-161301 Share on other sites More sharing options...
Orio Posted January 15, 2007 Share Posted January 15, 2007 Do this:[code]<?php echo "\"".$_SERVER['HTTP_HOST']."\""; ?>[/code]What's the exact output?Orio. Link to comment https://forums.phpfreaks.com/topic/34266-find-a-phrase-in-a-file/#findComment-161309 Share on other sites More sharing options...
papaface Posted January 15, 2007 Author Share Posted January 15, 2007 "localhost" Link to comment https://forums.phpfreaks.com/topic/34266-find-a-phrase-in-a-file/#findComment-161331 Share on other sites More sharing options...
papaface Posted January 15, 2007 Author Share Posted January 15, 2007 It works for every other domain. Just not localhost. Link to comment https://forums.phpfreaks.com/topic/34266-find-a-phrase-in-a-file/#findComment-161334 Share on other sites More sharing options...
Orio Posted January 15, 2007 Share Posted January 15, 2007 And are you sure you've added a line that says localhost, without any spelling mistakes or extra spaces etc'?Because there's no reason for it not to work.Orio. Link to comment https://forums.phpfreaks.com/topic/34266-find-a-phrase-in-a-file/#findComment-161335 Share on other sites More sharing options...
papaface Posted January 15, 2007 Author Share Posted January 15, 2007 Possitive:domainchecker.txt----localhostdomain.comsite.com---- ??? Link to comment https://forums.phpfreaks.com/topic/34266-find-a-phrase-in-a-file/#findComment-161338 Share on other sites More sharing options...
Orio Posted January 15, 2007 Share Posted January 15, 2007 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 https://forums.phpfreaks.com/topic/34266-find-a-phrase-in-a-file/#findComment-161350 Share on other sites More sharing options...
papaface Posted January 15, 2007 Author Share Posted January 15, 2007 Yes, that has worked :DWhat did you do? Link to comment https://forums.phpfreaks.com/topic/34266-find-a-phrase-in-a-file/#findComment-161355 Share on other sites More sharing options...
Orio Posted January 15, 2007 Share Posted January 15, 2007 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 :DOrio. Link to comment https://forums.phpfreaks.com/topic/34266-find-a-phrase-in-a-file/#findComment-161363 Share on other sites More sharing options...
papaface Posted January 15, 2007 Author Share Posted January 15, 2007 :)Thanks for your help Link to comment https://forums.phpfreaks.com/topic/34266-find-a-phrase-in-a-file/#findComment-161377 Share on other sites More sharing options...
Recommended Posts