Wrap your code in appropriate tags: html, php or code tags.
reeshaz;10987113 wrote:
if an image is available, it will load it, and if not, it will display something else (either text or an image).
How often does this change? Having your server access another server every single time a user wants to view this page, when your server is NOT serving the image anyway (that's handled by the user's browser sending a get request to the server where the image is located) is a waste of resources.
Check once! Or once per day. or once per whatever. But if the image is up, you always serve the same stuff. If you can't rely on this, download the image to your server by a chron script or whatever, and then update the file on your server every whatever minutes/hours/days.
reeshaz;10987113 wrote:
<?php if(@fopen("http://images.google.com/intl/en_ALL/images/logos
You are using the error supression operator. As long as you do, you will run into problems and you will not know what goes wrong.
reeshaz;10987113 wrote:
is it ignoring the entire If statement
No, it really isn't ignoring anything. However, if your if statement always evaluates to false, just as if you'd written
if (false)
{
# do stuff
}
else
{
echo "I'm always true!";
}
Well, then your else block will always be executed, but it doesn't mean PHP is ignoring the if statement. It is however pointless to have it in the first place.
reeshaz;10987113 wrote:
, but it isnt loading either of the images correctly.
The code works as intended. Are you certain you have the exact same code as posted here?
Also, you really should stop jumping in and out of php mode. Reading code like that is hell. This is what it should look like (disregarding that I still urge you not to fopen on every page request)
if(fopen("http://images.google.com/intl/en_ALL/images/logos/images_logo_lg.gif", "r"))
{
echo '<img src="http://images.google.com/intl/en_ALL/images/logos/images_logo_lg.gif" />';
}
else {
echo '<img src="http://www.bing.com/fd/hpk2/YunnanProvince_EN-US2145746409.jpg" />';
}
Voial! Readable code.
Also note that since you aren't outputting variable values, swapping out of php parsing once per image would have been enough with your code.
if(file_exists("http://images.google.com/intl/en_ALL/images/logos/images_logo_lg.gif", "r"))
{
?>
<img src="http://images.google.com/intl/en_ALL/images/logos/images_logo_lg.gif" />
<?php
}
else
{
?>
<img src="http://www.bing.com/fd/hpk2/YunnanProvince_EN-US2145746409.jpg" />
<?php
}
But it's still nowhere near as readable as the other.
Also note that not supressing errors is not a problem if you only have display_errors on in a development environment but never in production environment. log_errors should always be turned on though.
file_exists doesn't work with http wrappers. The documentation clearly states:
[man]file_exists[/man]
Tip
As of PHP 5.0.0, this function can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers to determine which wrappers support stat() family of functionality.
Where stat() and Supported Protocols and Wrappers are links. The latter in turn has a link for http:// which is what you're trying to use, and that page states
If you really wish to check for the existence of that file without a warning occuring if it doesn't, use curl to send a head request.
But like I said, the image should be downloaded to your server and always displayed. When the image goes away or is replaced, you should have some script running once per [timeframe] to check for this and update your local image accordingly.
Also, you may not have the rights to download that image to your server, but in that case you don't have the right to display it as part of your webpage either. What you can do is display some small part of another web page if it concerns what your page deals with, if you also link to the place where that content comes from. But even this may be problematic depending on where your servers are found, since some greedy stupid money lovers tried prohibiting what is referred to as "deep linking" (and no, there is no technical difference between a "deep" link and any other link - that's money-moron speak for "you bypassed my ads cause I don't know how to order a system that can deal with the web").
And in your particular case, I don't believe google will very much mind if you have their logo on your page. It may very well be freely available to begin with.