Hello Daniweb Community,

I was just pondering through thoughts as one does, when I couldnt answer my own question. How do you force adverts onto a page.

The scenerio is, I will have people uploading there sites to my site, as free hosting and free subdomain name. But, as it is all free for them, I wish to include advertisements.

Companies like Piczo force an advertisement at the top of the page.

Can anyone tell me how this is done.

Thank you,
Grant

Dani AI

Generated

Good question, — this comes up a lot. and flagged client-side wrappers and server config tricks; both paths are workable but have tradeoffs. Below are modern, practical options that keep hosted pages usable while letting the host inject a banner reliably.

A robust approach is server-side HTML insertion at upload or serve time. Workflow: validate and sanitize uploads, parse the HTML with a DOM library, insert a small ad container near the top of the document, isolate the ad (iframe or Shadow DOM) so page CSS/JS does not clash, then save/serve the modified output. Quick PHP example using DOMDocument:

libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$body = $doc->getElementsByTagName('body')->item(0);
$ad = $doc->createElement('div'); $ad->setAttribute('id','site-ad');
$iframe = $doc->createElement('iframe'); $iframe->setAttribute('src','/ads/banner.html');
$ad->appendChild($iframe);
$body->insertBefore($ad, $body->firstChild);
echo $doc->saveHTML();

If file edits aren’t possible, use a reverse-proxy rewrite to inject the banner on the fly (Nginx sub_filter or Apache substitute). That works for static content but needs handling for compressed streams and modern single-page apps; test thoroughly.

UX, security and legal notes: disclose the requirement in your terms, provide an opt-out or paid ad‑free tier, sandbox third-party ad scripts (iframe sandbox / CSP), and test many layouts (responsive, no-body pages, JS apps). Start with a small test set, monitor breakage, and iterate.

References: PHP DOMDocument, NGINX sub_filter docs, Shadow DOM overview (MDN).

Recommended Answers

All 4 Replies

They put the page into a frame, with the ads in another frame.

But it also makes any page that was so carefully crafted to validate on W3c fail to validate when displayed on the combined page. This makes web page makers mad.

Alternately, you could require that the page author provide an img tag with a link to the ad.

Please, no moving ads. They make me click Back faster than anything else.

Yeah, I knew about the Frame way of doing it.

But I hate putting things into frames unless they are designed to go in a frame.

It can alter the design of your site for the worse. You have to scroll unnecessarily.

I could try and get the author to provide a link, however, I wanted it done automatically...

Is there a way I can use a frameset but script it so changes height and width depending on the content?

If they send the files to you, you could edit them before putting them online.

If your ad is a banner across the top, everything else will move down.

Absolute positioning in a web page will mess up any attempt to add an ad.

Note that this site requires all ads for each position to be the same size.

I guess you can use .htaccess.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.