Archive for June, 2007

Displaying A Random Image From A Directory Via PHP

Recently asked on Yahoo! Answers:

How would one code a rotating image (using PHP) that can be hosted on Geocities Pro for, say, a forum?

In the past, I’ve been able to host a rotating image using another method by naming a folder ’something.png’ to fool a forum into thinking the folder is a picture and it reads the index.php file, which in turn pulls up one of the images I have coded. Now that I’m on Geocities Pro, I cannot make such a folder but would like to still use my rotating image. Is there another way I can code this and still be able to use it with forum BBCode?

Basically, this questioner wants a PHP script to look through the contents of an image directory, randomly select one of the images in that directory, and pass the script itself off as the image.

That’s fairly simple to do in PHP, but it does require a number of steps and a few tricks to overcome some limitations of PHP’s built-in filesystem functions.

This question also gives me a chance to explain working with the filesystem in PHP, and to demonstrate how loops and arrays can be used to produce some really neat results, so I’ll expand on that in a follow-up post.

As always, a working demo and source code will be available for download at the end of this article.

Read the rest of this entry »

Going To A New URL Via A Drop-Down List: JavaScript And DOM, PHP, ASP.NET

Recently, a visitor to this site asked me to expand on changing URLs via JavaScript, after reading my post on changing an IFRAME’s source via a drop-down list.

As a quick aside, I’ll do my best to answer any question that is posted in Yahoo! Answers if you e-mail it to me. There’s an envelope icon in the question; just use that to get to Yahoo! Answers’ e-mail form, send your question to dhvrm@yahoo.com and I’ll check your question out as soon as I can.

I won’t answer questions privately, meaning I will not e-mail an answer; I’ll only respond in Yahoo! Answers, on this Web site or in my Geocities adjunct site.

I answer questions because when I started out in programming, I needed a lot of help and I got it on the Internet. Now, I’m putting information back out on the Internet for other people who need it. Therefore, if you’d like my help, you need to share it with others. No private questions and no private answers, thank you.

That said, I got the person who asked me to post this question in Yahoo! Answers:

HTML code on How to link a drop down box item to a web page?

Check this url {snipped}
When somebody clicks on the name, it need to be linked to a existing web page in the site.
please provide me help.

It appears from the sample URL provided that this person only wants to go from one static HTML page to another. Which is easy enough, if we simply pirate the earlier iframe code and make some minor modifications. But to be thorough, I’m also going to address how to do this with server-side technologies, too.

Read the rest of this entry »

Displaying The Days Between Two Dates Via JavaScript / DOM

Recently asked on Yahoo! Answers:

What are the MS Office Frontpage JavaScript codes needed to calculate the number of days between two dates?

I am not a programmer, but i need the codes and add it to an existing webpage format. What i need exactly is to show in the page a kind of table identifying a date in the future, the current date, and the number of days (the difference between the two dates) from the current date to the mentioned future day. Its like a countdown on how many days are left before that future date comes…

This is very easy to accomplish via JavaScript; because JavaScript treats time as an integer, it’s all just math, plus changing the values of a couple of DIVs via JavaScript’s DOM manipulation abilities. So let’s get right to it.

Read the rest of this entry »

Showing A Larger Image On Thumbnail MouseOver with JavaScript / DOM

A fairly common question on Yahoo! Answers centers on thumbnail image galleries, where a “master” image changes whenever you mouse over various thumbnails. Such as this one:

Mouseover thumbnail in one frame changes pic in different frame in Javascript-Need Help!?

{code snipped}

Good: image displayed in content frame
bad: image does not update to a new image (x var)
bad: writeContent() excutes repeatedly stacking images(mouseover)

Some time ago I had discussed swapping a “master” image’s source when someone clicked on a thumbnail, and had directed mouseover questions to that blog entry. But this gives me a chance to answer the mouseover question directly, and to add a bonus feature: pre-loaded images, so the image swaps occur almost instantaneously.

I’ll also provide two examples: One that leaves the new image until a different thumbnail is moused over, and another that restores the original image any time you mouseout of a thumbnail.

As always, the code and the sample images are available for download at the end of this article.

Read the rest of this entry »

The Dangers Of Allowing Public File Uploads To Your Web Site

A common question on Yahoo! Answers is how to allow users to upload files to a Web site. Unfortunately, allowing file uploads on a Web site is the single most dangerous thing you can do as a Webmaster.

In short, if you allow people to upload files to your site, you’re opening yourself up to a world of trouble, from having your site defaced to compromising the entire Web server you’re on, straight through to possibly trafficking in illegal materials. So if you’re new to Web programming, it’s wisest to not mess with file uploading at all, or at least use a canned solution that has taken some of the worry out of hosting illegal and dangerous files.

Let’s first talk about why public uploads to Web servers are dangerous, then get into how to mitigate those dangers.

Read the rest of this entry »