Run PHP page every hour automatically-without using Cron jobs - javascript

How can i run or refresh PHP page every 10 Minutes or an hour. so that add new record from one database to another i doesn't matter with using javascript but without using cron jobs.

You can depend on the traffic to your website if it's frequent enough. Drupal has something called poor man's cron. Basically you store the date-time that the cron job was last run. Each time a user visits a page on your website, you fetch the date-time, compare it with the current date-time to see if you need to run the cron job (in your case, see if an hour has passed). If the required amount of time has passed, then run your cron job, and store a new date-time. If your required amount of time hasn't passed, then do nothing.
This approach has a few caveats.
You really should be using cron, and not doing this. There's a reason it's called poor man's cron. If you're using some hosting service by someone else, their technical support should be able to help you setup a cron job. If the service has cPanel you can do it from cPanel. https://documentation.cpanel.net/display/ALD/Cron+Jobs
You are completely dependent on website traffic to trigger your cron job. You need to decide if it's OKay for your cron job to not be run for a period of time where users are not visiting your website.
You're slowing every request to your website down by a fetch to whatever persistent store is holding the last date-time the cron job was run.

with javascript you can use the setTimeout() function but the user will need to keep the page opened.
i.e
setTimeout(function(){ your_process(); }, 360000); // every hour

Related

Time based Code execution in Javascript PHP

I have a web application in PHP and Javascript where i want to apply a time based question generation. I have preset of code that will be executed at a specific time which will generate the question.
Now i am thinking that how can i apply this. Here are some the information regarding the web
Web is running on wamp server and there are almost 100 users used it regularly (every day). They login in the website two time between 9-11am and 2-6pm.
The question geneartion time should be at 4pm.
Now, i can make use SetTimeOut and Javascript Date object in order to acheive the desire functionality: Here
function checkVotingQuestionTime()
{
var currentTime = new Date();
if(currentTime.getHours() >= 16 && currentTime.getHours() < 23)//check the time from user OS
{
checkVoteQuestionGenerated();
}
}
function checkVoteQuestionGenerated(){
//query to php check question exists
//if no
//then generate question
}
And Register it at document ready event in main page(after login). This code will be open for all users.
$jq(document).ready(function() {
setInterval(checkVotingQuestionTime, 60000);
}
As it appear that, this is not feasible code, because
It will be run from every client (set timeout and server hit
regularly and if i also get time from server than it will more often hit on server)
A user can change the system time to run above code.
Multiple question can be generated if two clients simultaneously check that the question is not generated so generate the question.
So how can I control above limitations or is there any other proper solution available?
Having 100 clients poll the server every few minutes is not a big deal. Every time they open a page on your website that's in effect polling the server. So moving the date check to php on the server side is the best solution for what you have currently set up.
If you don't like the idea of having the client poll the server every now and then for aesthetic reasons (and I can appreciate that), for example you want the question to be refreshed at 16:00 on the dot, you could look into using a sockets. This, in effect, means the client is listening to any changes sent from the server, without the need to poll it, and updates when necessary.
Socket.io is easy to use, but not the simplest when it comes to interfacing with php. I don't know if you're familiar with Node.js but here's a small discussion some others had regarding sockets and php:
What is the best way to use websockets along with PHP and MySQL scripts?

How can I make a website update at a certain scheduled time?

I'm a beginner in the middle of trying to make his first website.
Right now, my website uses javascript to get weather information from Yahoo every time a page loads. Is there a way to set things up so it will only try to get information once a day at midnight- even when no one has the website open? Also, I'm hosted by Github pages.
Is there a way to set things up so it will only try to get information once a day at midnight- even when no one has the website open?
Yes you could create a cron task to fetch the information once per night, and then users could get that data from you. This would require the ability to run a task on your server.
Also, I'm hosted by Github pages.
So no, you can't use a cron task. Instead, you could cache the response in the client's cookies or local storage and have it expire in 24 hours. This way each client will check their local cache first, and only fetch if the cache is expired.
last dying breath option:
You could setup a crontask on your development machine to fetch the weather, write it to a file, then push a commit to github, and have it run that task once per night. Then users could fetch the static data from GH Pages.
This may be a bit overkill tho

Handling timers for many users in Nodejs

I'm building a web app in Node/Express such that users can set a timer so that a certain task gets executed or repeated. My question is how can I set timer for many users with an event triggered after deadline. I don't want to handle this on the client side beacuse users can close the browser. A good example of this is services such as pingdom.com that allow you to set timer and send a ping every X minutes to your server. How can this be acheived?
I hope I don't get many negatives for asking this and I'm also not asking you guys to write me the code. I simply want to know a robust strategy to solve this problem.
Here is what I thought about:
Save the endtime in db and using a cron job check every second to see if the time is up (This is not really good in my opinion since the query and all calculations might take more than 1 second)
Somehow assign a variable to setInterval and store them in a global list
Your web app would have a more responsive UI and your backend would scale better if each client held its own timer. Closing the browser wouldn't be a problem.
Persist the endtime in a DB in your server for the specific question.
On the client side, retrieve the endtime from the server, and use JavaScript to create a timer that would alert the user their time has expired.
On the server side, have an additional check that rejects answers if the answer is being submitted past endtime to prevent clients from cheating.

On site notification system

I was tasked to build a calendar and agenda library (I'm working on the CodeIgniter framework), that shows a calendar and the user has the possibility to set events on that calendar.
These events will have a notification system both email, and when the user is browsing the site, a popup.
My issue is how to approach the notification part when the user is on the site. The email is something that I already decided would be done trough a cronjob that will run every x minutes and check if there is any need to send a notification email.
The on site notification is something else.
How would I approach this? I just can't make a ajax request to the server every x seconds, since that would put an unnaceptable load on the system. (Of course when the user is eventually notified, a request must be made, to set the user as "remined" on the database).
I can't just depend on the user's date time, since he could be anywhere in the world and the time would be different.
How can I check that the user must be notified of a event, avoiding making repeated requests to the server? Would appreciate any input.
I could see using setTimeout to do this. Say a user visits your page $minutesTilDue minutes prior the reminder being due. Assuming jQuery/PHP:
$(function(){
setTimeout(function(){
showEventReminder(<?php json_encode($event) ?>);
}, <?php echo $minutesTilDue ?> * 60 * 1000);
});
Nothing too fancy, but depending on your requirements...
Cheers
Easily scalable notification systems use websockets, which today reach the majority of the users, even on mobile devices. Unfortunately, you'll need a websocket-enabled server - node, glassfish, whatever - this can't be done with standard PHP deployments (mod_php or fcgi). Server events (so called server push) are sent when they are generated, so once you have your websocket client-server pair, sending a reminder is just like sending an email.
Actually things are more complicated because most likely users won't be online at the exact time the reminder should pop up. I suggest a notification icon which is refreshed each time the user hits a page. This way your calendar system (suppose a cronjob) will simply update a flag for the user row in the DB, and when you build the page, you already know if there reminders (let's say, in the next 60 minutes) or not, and choose the icon accordingly. At this point, you have two choices:
sending the reminders to the client along with each and every request (it could be a waste of bandwidth, but I don't thing a JSON-encoded list of events is so heavyweight)
Download the reminders asynchronously on demand, ie only when the user hits the notification icon
This scenario lets you reuse the PHP environment and code, and doesn't require another host for the websocket server. I think this is the best solution, even if this doesn't fulfill your requirement of a truly popup reminder triggered by the server at the right time. BTW, if you send events with every request, your javascript can pop up when needed - you can use setTimeout() for this.

Display accurate local time on web site?

I've been asked to display the 'correct' time on our website which I frankly feel is rather pointless as 'correct' can be interpretted in such a variety of ways.
Our current method definately results in an inaccurate time as it uses a server control rendering JavaScript that runs onload using the datetime from the server as a parameter to create a clock object in JavaScript that finally renders on the page and then starts incrementing the clock. Between the server processing, network latency and client-side performance (there's plenty other stuff running onload) the clock ends up way off the actual server time and who knows compared to the client PC.
So to get the 'correct' time shown I could;
Use local PC time and pass new Date() to the JavaScript clock object. Pros: Should be as close to the PC clock as possible. Cons: Not sure how accurate the PC clock is let alone in which timezone.
Use web service for TCP request to NTP server to update clock on web page. Pros: If local PC also sync'd to NTP will be accurate and best possible match. Cons: Will have to handle all the timezone adjustments relative to our servers. If PC clock is out will still have mismatch.
Do I implement my own web service or use something like; Earth Tools or World Time Web Service (EDIT: link removed - now 404)
Here's a blog post from Jon Galloway on Atomic Clock Web Service which is pretty old and yet ranks high when I google and he doesn't reach a conclusion.
Hoepfully I can win the argument with management why syncing to our server clock (GMT) doesn't makes sense if your not in that timezone and why we even need to match a local PC.
Any angles on this I'm missing?
I needed to show the accurate time to clients in an auction web app. You can send the current server time with the page and have the javascript initialize right away without waiting for the rest of the page to load. So the, you only are dealing with network latency which in the worst case is not likely to be more than a couple of seconds.
After that, you're pretty darn close to accurate time. As long as your Javascript timer code is written properly, you're not going to far out of sync before the next page load. But I have seen a lot of bad JS clock code. (Hint: Date() good, setTimeout() bad.)
If you have an application that users are going to be sitting on for a long time, just refresh your time sync either by reloading the page or Ajax.
I wouldn't worry about time zones, just use UTC time so there is no confusion about what time things will happen.
First, be certain that your client is aware that Windows, Linux, and OSX all have built-in clocks that are almost always visible to the users (or made visible very easily). Also, be certain that your client is aware of physical clocks that are often located near any kiosks that might be setup to hide the built in clock from the operating system.
If you know that, and your client still wants a clock on your website, have your client define "correct" time, then implement the solution that matches their definition (both of your solutions seem like they would take care of the two most-likely definitions).
you can use geo targeting to know the physical location of a website visitor and in your database stored the (GMT - XX:XX) of the zone and then calculate the time based on the location of the request. that is going to save the long trip to any third party web service.
Another way you can implement it is using IP Geolocation. There are services that can tell you where your user is connecting from based on it's ip (usually including their timezone) and combining that information with your server's realtime clock you can show the user it's local time.
It's far from perfect, specially with corporate users that might seem to be connecting from somewhere they are not (I live in Argentina, but my work internet connection is trough my employeer that is an American company, so every website assumes I'm located in the US)
Handle time in UTC.
Have users tell you what zone they want to use.
If your users have persistent profiles, persist the choice.
Always display UTC and Local Time side by side and clearly labelled.
You can also display an arbitrary number of user specified zone clocks. Vista
does this and I remain surprised at how
handy it is.

Categories

Resources