We all have come across web pages which take some time to show the content the user is waiting for and in the mean time display ads on the page.
I'm not talking about the pages that show full-page ad with 'skip this ad' button.
A typical example of what I'm referring to is: I visit a free plugin site. Click on the plugin I want to download it opens a new page which has the link to the plugin zip file. But the link does not appear immediately. When the page is loaded it is full of ads with other misleading (:P) download links. The actual link I'm interested in appears after say some 5 seconds, squeezed between two ads.
How can this be done for a PHP based website? Will a simple sleep() or usleep() do?
No.
When you issue a sleep() or a usleep() in a server side language (PHP) the sleep occurs on the server side, typically before output is sent to the user.
You would need to implement the functionality you desire using Javascript and the setTimeout() function.
You can have the link in a div and hide it initially and start a Javascript timer to show it.
Something similar to what you are looking for.
Something is hidden
<div id="hid" style="visibility: hidden">TADA!</div>
here
<script type="text/javascript">
function showIt() {
document.getElementById("hid").style.visibility = "visible";
}
setTimeout("showIt()", 1000); // after 1 sec
</script>
Source
With a setTimeout() in javascript to make a <div> containing the link visible after some amount of time.
Related
I want to load more images in my website when I reach the bottom of my page. I'm using php and postgresql as my database.
For this post I simply load some text instead of image. I can write the code for it's equivalent.
So currently, I'm using a button at the bottom of my page, which when pressed re-loads the page and gives you more images(I'm displaying 50 images at a time).
But there are 2 problems with it, one being that the user will have to press the button again and again while I want it to happen automatically.
And the second one being that when new images are loaded, the previous ones are gone. I don't want to happen. For eg., if currently 1-50 images are present, my page later changes it to 51-100 while I want it to have all 1-100. I'm unable to solve this.
Please help. Thanks!
What you are looking for is commonly referred to as "infinite scroll pagination", while what you're asking for is techniclly possible using only PHP it would be a terrible user experience, as each reload would take the user to the top, and they would constantly have to continuously scroll further and further just to reach the location they were previously at.
Alternatively, handle this with JavaScript, an example: https://infinite-scroll.com/demo/full-page.
Doing simple Google searches reveals a plethora of options for JavaScript and JQuery plugins to achieve this.
An alternative, without the need for a plugin you can implement the answer to this:infinite-scroll jquery plugin
Simply call your PHP code in the form of an AJAX request when the bottom of the page is reached and append your new results. (this could be easily achieved with vanilla javascript as well).
Hope this helps.
I was looking at some topics that is talking about my question but couldn't find or understand my specifics.
I have users, I want redirect that user to a website outside my web, stay there for 10 seconds, and then click a button that confirms that he has waited those seconds. I was thinking doing that like this :
<div id="shown_box_at_top"> you have to wait <div id="timer">10</div> more seconds. </div>
<div id="shown_box_after_timer"> Please click here</div>
<?php include('www.wesite.com');?>
Of course it's just an example but if I do it this way, and www.website.com have a visitor counter, the counter will add him as a visitor or not? Or maybe there is a better way to do it?
If you want to redirect the user to this external website, you can use PHP header function to do something like this :
header('Location: http://www.example.com/');
However, once the user clicks on this link, you will lose control over that user as he won't be on your website anymore. I think the better way to do this would be to use iframes to display the external website on your own webpage. The user will increment the external page's visitor counter and since the user will be on your web-page, you will still have control over this user to do what you want after (let's say) 10 seconds using jQuery.
First of all, include executes the HTML as php code locally, which is definitely not what you want, as it would create a security vulnerability. Instead, if at all, you want file_get_contents. Even better, consider using an iframe so that the website gets downloaded from the client.
So, I have a HTML page with links to video files on my server. What I need is, when someone clicks on the video links, a script should start loading up the advert from Google Adsense.
And after 10 seconds, it should be automatically redirected to the video file.
How can I do this?
Thanks
I'm sure that goes against the crazy adsense TOS. Read them again before getting banned.
Isn't that illegal?
You need to change the link to the video with a simple text (in ) and add
the "onClick" attribute which will activate a JS function.
<div onClick="javascript:showAdvertAndRedirect('http://www.video.com/id/23233')">Click to see the video</div>
The js function will show the ad (by changing another div's innerHTML)
and will also run a timer of 10 seconds (actually a delay) , eventually will redirect
the visitor to the mentioned url.
I have this problem to solve.
I gonna make an email campaign to my clients with some affiliate promotions.
The email will have some direct links to the promotions that goes through a php script in my site. (ex: http://mysite.com/promo.php?promoId=x)
In this script I will manage my database queries and cookie.
In cases that I detect through the cookie that this user has not made any click in the last x days I would like to present him with two html pages: my main website page and the external site referring to the promo.
How can I do this? I currently use header( "Location: $promo_url" ) to redirect to the promo url... but in same cases stated before I would need two urls to open.
Thanks in advance for all your answers
GS
You can either use frames (ugh), an iframe within the main page to display the second page (somewhat less ugh), or use popup windows (truly ugh, and probably not possible due to everyone having popup blockers these days).
You could have the script output JavaScript with window.open instead.
<script type="text/javascript">
window.open("link1"); // open link1 in a separate window
window.location.href = "link2"; // point this window to link2
</script>
If you can, however, it would be much better to explicitly show the user a link to the second location somewhere on the page instead of forcefully opening another window / tab for them.
Show them your site, and in your site have a hidden a link tag, which will link to the affiliate site with the attribute _blank. Using jQuery, once the DOM is ready trigger a click event to the hidden link tag.
E.x.
HTML
<a id="affiliateLink" href="http://www.affiliatesite.com/asdad" target="_blank" style="display:none;">Visit Affiliate</a>
JavaScript
$(document).ready(function(){
$('#affiliateLink').trigger('click');
});
There's really no good way to do what you want without using iframes (ugh) or popups (double ugh). The other answers are how to use those techniques, but a possible 'better' solution would be to display a banner on your page when you detect the user hasn't been to your site in a while. The banner could say something along the lines of Click here for information about our awesome promotion, and it'd be easy to make it stand out. That way, you're not forcing popups (which no one will ever see), and while iframes aren't terrible from a user perspective, they don't really scream "modern web design".
Can I Stop or interrupt PHP script for particular time?
I would like to implement logic like below:
on mydomain.com/index.php there will be flash appear showing some intro of a product for suppose 20 sec. Once it complete, on same index.php page the home page of site will appear.
I am not aware about flash (action script).
Is it possible in PHP or Javascript ?
Usually "splash pages", as the're called, are made up of a seperate page.
In flash you can use the following code (Actionscript 3). Put it int the last frame, or use an event listener to redirecrect when the file is finished. The actual redirect looks like this:
getURL("http://www.woursecondpagehere.com", "_self")
Where you place it is up to you.
EDIT: I think that this is a reliable solution because this guarantees (if implemented correctly) that the page won't move until Flash is done. CSS and Javascript will work fine too.
There isn't a need to interrupt PHP in the scenario given. Though I think what you want is to load the rest of the HTML after a certain event occurs.
If thats the case then you can use AJAX to load the additional HTML from the server. Or you can use CSS to hide that content and show it after a certain point.
The META Refresh tag is probably not what you want since it will redirect the user after 20 seconds, regardless of how long it took to load your Flash file, then play it. Since the speed of the user's connection cannot be reliably predicted, you will probably end up with a poor user experience.
What you want to do is definitely possible but it will involve some interaction between the Flash object and the rest of your page. If you could do as Moshe suggested and simply have the Flash object redirect the user's browser to your actual home page with content on it, that would be easier.
If you insist on keeping everything on the same page, one way to do it is to call a Javascript function from the Flash object once it's finished playing. The function you call should be written to hide the Flash object and/or it's container and display the container () with all of your content that you're ready to show.
See this Codefidelity blog post for a quick tutorial on how to call JS functions from Flash.
Also, to clarify, you won't be interrupting or changing when your PHP script runs. That runs on the server before the page is created and sent back to the user's browser. All you need to do is structure the HTML/CSS of your page to have two DIVs: one with the Flash object in it and the other with all your normal page content. However, set the CSS to only show the DIV with the Flash object, then finally use Javascript to hide that DIV and show the one with the content in it.
Try this,
write the your flash (splash screen) <embede> code in index.html and simply use javascript redirect function with javascript timer functions to redirect to index.php where you actual content is there.
something like...
window.location = "http://your.domain.name/index.php"
or
location.href = "http://your.domain.name/index.php"
use setTimeout() to call redirect after specified time.