Paradox123 Posted August 2, 2008 Share Posted August 2, 2008 Hello, basically i'm writing a php script which connects to my website and see's if the url is valid It has no real use but is just for practice as i'm new to php. it reads from website.txt to get the website ($url) and then reads from directory.txt to get the directory i want tested ($dir) e.g. "www.mysite.com/lol.php". if www.mysite.com/lol.php IS valid, it will save to output.txt and if (for example) www.mysite.com/notlol.php is NOT valid then doesn't save to output.txt. So far the read/write to file is working fine i'm just having problems with checking the site is actually valid. The code i was using to check this is as follows. $wtf = $url . $dir; //curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); <-- this is only commented out whilst i test the code curl_setopt($ch, CURLOPT_URL, $wtf); curl_setopt($ch, CURLOPT_HEADER, 0); $res = curl_exec($ch); if(!$res) die("Error Connecting To Target"); $res = strstr($res, $dir); if(!$res) die("Not found"); You will probably notice the problem is on the "$res = strstr($res, $dir);" this looks like this because i was getting desparate and was trying pretty much everything i could think of. This code gives a positive every time so saves to output.txt even when the site + url doesn't exist. My question is, what would i have to change to make it write to output.txt only if the site + url DOES exist? And is there a better (easier) way of accomplishing this than above? Thanks a lot in advance, any thaughts/ideas are welcome. Quote Link to comment https://forums.phpfreaks.com/topic/117831-helpadvice-needed/ Share on other sites More sharing options...
ohdang888 Posted August 2, 2008 Share Posted August 2, 2008 it might sound retarded me asking this, but sometimes this is the case: have you installed cURL yet? Quote Link to comment https://forums.phpfreaks.com/topic/117831-helpadvice-needed/#findComment-606066 Share on other sites More sharing options...
Paradox123 Posted August 2, 2008 Author Share Posted August 2, 2008 Hehe yes, if you look i've got "//curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);" commented out, so i can see that it's connecting to the site alright. Thanks anyway. Quote Link to comment https://forums.phpfreaks.com/topic/117831-helpadvice-needed/#findComment-606068 Share on other sites More sharing options...
cooldude832 Posted August 2, 2008 Share Posted August 2, 2008 small class a wrote a while ago to ping stuff <?php class ping{ var $url = "http://www.google.com"; function ping_it(){ $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL,$this->url); curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); curl_setopt($ch,CURLOPT_VERBOSE,FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 15); $this->page_data = curl_exec($ch); $url_info = curl_getinfo($ch); if(count($url_info)>0){ $this->returned = $url_info; } else{ $this->error_report("Could not't find url.",1); } curl_close($ch); } function print_return(){ echo "<ul class=\"ping_return\">\n"; foreach($this->returned as $key=> $value){ echo "<li>".$key.": ".$value."</li>\n"; } echo "</ul>\n"; } } ?> to use include the class and say <?php $ping = new ping; $ping->url = "YOUR URL HERE"; $ping->ping_it(); $ping->print_return(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/117831-helpadvice-needed/#findComment-606084 Share on other sites More sharing options...
Paradox123 Posted August 2, 2008 Author Share Posted August 2, 2008 Thanks, i'll try and give it a go. p.s. 1,2,3-trimethylbenzene (hemimellitene) Quote Link to comment https://forums.phpfreaks.com/topic/117831-helpadvice-needed/#findComment-606090 Share on other sites More sharing options...
DeanWhitehouse Posted August 2, 2008 Share Posted August 2, 2008 Can you not use an if statement .e.g. if(file_get_contents) { } or something similar? Quote Link to comment https://forums.phpfreaks.com/topic/117831-helpadvice-needed/#findComment-606092 Share on other sites More sharing options...
cooldude832 Posted August 2, 2008 Share Posted August 2, 2008 1,2,3-trimethylbenzene (hemimellitene) Yeah I can finally change it first person ever to call ti as it is hemimellitene (got 1,2,3-trimethylbenzene a million times) Quote Link to comment https://forums.phpfreaks.com/topic/117831-helpadvice-needed/#findComment-606095 Share on other sites More sharing options...
Paradox123 Posted August 2, 2008 Author Share Posted August 2, 2008 Can you not use an if statement .e.g. if(file_get_contents) { } or something similar? I don't really understand what you mean - i'm new to php - i know what if statement is but not sure how i can use that to test if the adress is valid. Yeah I can finally change it first person ever to call ti as it is hemimellitene (got 1,2,3-trimethylbenzene a million times) Chemistry student. Quote Link to comment https://forums.phpfreaks.com/topic/117831-helpadvice-needed/#findComment-606102 Share on other sites More sharing options...
DeanWhitehouse Posted August 2, 2008 Share Posted August 2, 2008 i believe it will be if(file_get_content("url")) { echo "Page exists"; } Quote Link to comment https://forums.phpfreaks.com/topic/117831-helpadvice-needed/#findComment-606301 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.