Jump to content

Recommended Posts

Okay, so I know this should be possible, and I haven't tried it yet, but I don't know.

 

What I want to do is let a user enter a website link, then I want the code to try to connect to the website, to see if it's real. I know it's possible if you're using the actually IP addresses of websites, but I don't know if I need the IP addresses, or if somehow the code can find them, and then check?

 

Appreciate the help  :)

 

I will update the post if/when I can find my old code of check the IP addresses

 

Edit: Okay, so I've found this:

@fsockopen($serverip, $serverport, &$errno, &$errstr, 10)

Not sure if it will work, I'll test it now. However, what port would be used? I remember FTP port is 21 or 22, but I doubt it would be those ports. Maybe port 80?

Link to comment
https://forums.phpfreaks.com/topic/153328-solved-checking-if-website-is-real/
Share on other sites

Okay, so I tried this...

 

<?php 
$url = "http://www.google.com";
	  	function test_url_file($url) {
		$res = (($ftest = @fopen($url, ‘r’)) === false) ? false : @fclose($ftest);
		return ($res == TRUE) ? 1:0 ;
		} 
		if ($res = 1) {
		echo "Connected.";
		} else {
		echo "WRONG.";
		}
		?>

 

And the page returned "Connected.", and I was happy. So I tried a different one, obviously not a website (I typed in random words, with spaces, which is impossible for a website to have), and it still said connected. I'm not sure if I did it right, I'm not very good with functions yet, or ftests (which I think are socket tests but I'm probably wrong, still learning).

 

Any ideas? Should I try the other method on that page?

You're supposed to get the value that's returned from the function.

 

Try this.

 

function test_url_file($url) {
   $res = (($ftest = @fopen($url, 'r')) === false) ? false : @fclose($ftest);
   return ($res == TRUE) ? 1:0;
} 

$url = "http://www.google.com";
echo (test_url_file($url)) == 1 ? "Connected" : "Not Connected";
?>

Perfect, works good. However, I don't quite understand that last line,

echo (test_url_file($url)) == 1 ? "Connected" : "Not Connected";

 

That's obviously where it's returning the words if it's connected or not, but what if I want it to display a huge amount of code, instead of one or two words? Would I just do...

 

<?php 

$url = "http://google.com";

   function test_url_file($url) {
   $res = (($ftest = @fopen($url, 'r')) === false) ? false : @fclose($ftest);
   return ($res == TRUE) ? 1:0;
} 

echo (test_url_file($url)) == 1 ? " ?> // End PHP here to write lots of lines of code
//This is
//lots of
//lines of code
<?php " : "?>
//more code
//
//
<?php ";
?>

This line (called a "ternary operator"):

 

echo (test_url_file($url)) == 1 ? "Connected" : "Not Connected";

 

Translates to:

 

if(test_url_file($url) == 1) {
   echo "Connected";
} else {
   echo "Not Connected";
}

 

As you can see it saves a lot of room when writing code, but in your case if you want to write and execute more code you have to use if/else statements.

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.