Spike121 Posted April 9, 2009 Share Posted April 9, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/153328-solved-checking-if-website-is-real/ Share on other sites More sharing options...
Maq Posted April 9, 2009 Share Posted April 9, 2009 I've seen posts in these forums about this but here's a tutorial I found in 2 seconds with Google that looks promising: Check If URL Exists Quote Link to comment https://forums.phpfreaks.com/topic/153328-solved-checking-if-website-is-real/#findComment-805541 Share on other sites More sharing options...
Spike121 Posted April 9, 2009 Author Share Posted April 9, 2009 Hmm, does look promising. Guess you need to know what to search . I was looking for about half an hour. Thank you! I'll test it now. Quote Link to comment https://forums.phpfreaks.com/topic/153328-solved-checking-if-website-is-real/#findComment-805543 Share on other sites More sharing options...
Spike121 Posted April 9, 2009 Author Share Posted April 9, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/153328-solved-checking-if-website-is-real/#findComment-805555 Share on other sites More sharing options...
Maq Posted April 9, 2009 Share Posted April 9, 2009 Change this line to: if ($res == 1) { Quote Link to comment https://forums.phpfreaks.com/topic/153328-solved-checking-if-website-is-real/#findComment-805559 Share on other sites More sharing options...
Spike121 Posted April 9, 2009 Author Share Posted April 9, 2009 Now it shows everything as "WRONG". Should I change it to else if ($res == 0) { echo "WRONG"; } Edit: Wait, that wouldn't matter. I'm trying it with all variations of google.com. http://google.com. http://www.google.com. www.google.com. None work. Quote Link to comment https://forums.phpfreaks.com/topic/153328-solved-checking-if-website-is-real/#findComment-805563 Share on other sites More sharing options...
Maq Posted April 9, 2009 Share Posted April 9, 2009 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"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/153328-solved-checking-if-website-is-real/#findComment-805568 Share on other sites More sharing options...
Spike121 Posted April 9, 2009 Author Share Posted April 9, 2009 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 "; ?> Quote Link to comment https://forums.phpfreaks.com/topic/153328-solved-checking-if-website-is-real/#findComment-805580 Share on other sites More sharing options...
Maq Posted April 9, 2009 Share Posted April 9, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/153328-solved-checking-if-website-is-real/#findComment-805585 Share on other sites More sharing options...
Spike121 Posted April 9, 2009 Author Share Posted April 9, 2009 Thank you very much Quote Link to comment https://forums.phpfreaks.com/topic/153328-solved-checking-if-website-is-real/#findComment-805588 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.