Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/117831-helpadvice-needed/
Share on other sites

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();
?>

Link to comment
https://forums.phpfreaks.com/topic/117831-helpadvice-needed/#findComment-606084
Share on other sites

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. ;)

Link to comment
https://forums.phpfreaks.com/topic/117831-helpadvice-needed/#findComment-606102
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.