IFrames are useful for a multitude of content and especially for in-site navigation where you can use on to be able to load pages or code snippets dynamically on the page without being an AJAX programmer and without resorting to any javascript. If you combine PHP with JavaScript you’re able to do many things which almost appear like AJAX – however are far safer to implement as the one knowledge you would want is HTML and just some PHP for tough one scripts.
Now if you are running a website and placing something using iframe then when user load that page and your that iframe page is not working then use will see a blank space there so to avoid this you can place an alternative to the iframe so if first one is not loading than you can load second backup iframe there using puire PHP so here we are sharing that PHP code so that your webpage will have some content not only blank spaces sometime. So now move below and garc the code.
<?php if (isDomainAvailible('http://www.maindomain.com')) { echo '<iframe src=\'http://www.maindomain.com/iframe\' name=\'MAINDomain\' scrolling=\'auto\' noresize=\'noresize\' framespacing=\'0\' border=\'0\' frameborder=\'NO\'></iframe>'; } else { echo '<iframe src=\'http://www.newdomain.com/iframe\' name=\'NewDomain\' scrolling=\'auto\' noresize=\'noresize\' framespacing=\'0\' border=\'0\' frameborder=\'NO\'></iframe>'; } //Returns True, If Domain Is Available, False If Not In 30 Sec function isDomainAvailible($domain, $timeout = '30') { //Check, If A Valid URL Is Provided if(!filter_var($domain, FILTER_VALIDATE_URL)) { return false; } //Initialize Curl $curlInit = curl_init($domain); curl_setopt($curlInit,CURLOPT_CONNECTTIMEOUT,$timeout); curl_setopt($curlInit,CURLOPT_HEADER,true); curl_setopt($curlInit,CURLOPT_NOBODY,true); curl_setopt($curlInit,CURLOPT_RETURNTRANSFER,true); //Get Answer $response = curl_exec($curlInit); curl_close($curlInit); if ($response) return true; return false; } ?>
Things To Know:
http://www.maindomain.com\
: This is the name of the first domain from where you will check the first iframe so that if that will not work then that iframe will not show..http://www.newdomain.com
: And this is the name of second iframe as if first will not work then this one will be shown on the same place.
Last Words:
This is what we have and shared in easy steps for newbies so that they can easily know how it works. Stay with us because we are going to share alot more about coding and make it easy for you. If you liked it then share it and be with us to get next awesome snippets. If you have any problem then feel free to ask us. We will help you with what we can or have.
Be the first to write a comment.